-1

Alright, so I followed a small tutorial on how to make a discord bot and in the end I got this code:

require('dotenv').config();
const Discord = require("discord.js");
const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
    if (msg.content === "ping") {
        msg.reply("pong");
  } 
}) 
client.login(process.env.DISCORD_TOKEN);

Basically the bot must reply with "Pong" every time someone says "Ping"

After that, the tutorial said that to get the bot online I'd need to run the "node bot.js" command on a terminal, and I did, but the output was clearly an error and not what was shown in the tutorial (also the bot didn't go online)

throw new DiscordjsRangeError(ErrorCodes.BitFieldInvalid, bit);
    ^

RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.
    at IntentsBitField.resolve (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\BitField.js:172:11)
    at C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\BitField.js:167:54
    at Array.map (<anonymous>)
    at IntentsBitField.resolve (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\BitField.js:167:40)      
    at new BitField (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\BitField.js:33:38)
    at new IntentsBitField (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\IntentsBitField.js:9:1)      
    at Client._validateOptions (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\client\Client.js:494:25)      
    at new Client (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\client\Client.js:78:10)
    at Object.<anonymous> (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\bot.js:3:16)
    at Module._compile (node:internal/modules/cjs/loader:1254:14) {
  code: 'BitFieldInvalid'
}

Node.js v18.16.0

I am very confused and I'm sorry if for some this may look like a stupid question and/or mistake, but I just don't know what to do :/

Here is the tutorial link: https://www.xda-developers.com/how-to-create-discord-bot/

Faneca X2
  • 3
  • 3

2 Answers2

0

The error is: Invalid bitfield flag or number: GUILDS.

It seems like GUILDS is not the expected value for intents.

Reading the doc here they show how to add intents.

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
    ],
});

So for you:

const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
//to
const client = new Discord.Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]});
Salketer
  • 14,263
  • 2
  • 30
  • 58
  • By the way, in the tutorial you pointed to, we clearly see discordjs version 13 being used. While the command they suggest you use to install it would install the latest version which is 14. It is most likely that the features used by the tutorial have changed. – Salketer Jun 15 '23 at 07:45
  • Actually helpfull, thanks bro – Faneca X2 Jun 15 '23 at 09:56
0

I think changing this.

const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});

To this should fix it

const client = new Discord.Client({intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.MessageContent]});

Also be sure to enable these: intents

Edit: Also noticed the event is set on message, should be messageCreate

Javs
  • 49
  • 6