This question already has answers here:
Discord.js: Invalid bitfield flag or number: GUILDS
(3 answers)
Closed 4 months ago.
i am trying to make a discord bot but when i use this code i get an error. please help, as i do not understand!
EDIT: i just attached the error, you're welcome!
code:
const Discord = require('discord.js');
const client = new Discord.Client({
allowedMentions: {
parse: ['users', 'roles'],
repliedUser: true
},
intents: [
"GUILDS",
"GUILD_MESSAGES",
"GUILD_PRESENCES",
"GUILD_MEMBERS",
"GUILD_MESSAGE_REACTIONS"
],
});
client.on('ready', () => {
console.log('I am ready')
});
client.login("TOKEN")
error is:
C:\Users\me\OneDrive\Desktop\code\js\my bot\node_modules\discord.js\src\util\BitField.js:168
throw new RangeError(ErrorCodes.BitFieldInvalid, bit);
^
RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.
at Function.resolve (C:\Users\me\OneDrive\Desktop\code\js\my bot\node_modules\discord.js\src\util\BitField.js:168:11)
at C:\Users\me\OneDrive\Desktop\code\js\my bot\node_modules\discord.js\src\util\BitField.js:163:54
at Array.map (<anonymous>)
at Function.resolve (C:\Users\me\OneDrive\Desktop\code\js\my bot\node_modules\discord.js\src\util\BitField.js:163:40)
at Client._validateOptions (C:\Users\me\OneDrive\Desktop\code\js\my bot\node_modules\discord.js\src\client\Client.js:481:41)
at new Client (C:\Users\me\OneDrive\Desktop\code\js\my bot\node_modules\discord.js\src\client\Client.js:78:10)
at Object.<anonymous> (C:\Users\me\OneDrive\Desktop\code\js\my bot\index.js:3:16)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32) {
[Symbol(code)]: 11
}
In discord.js v14, intent flags are available from GatewayIntentBits.
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
],
});
List of changes:
v12/v13
v14
GUILDS
GatewayIntentBits.Guilds
GUILD_BANS
GatewayIntentBits.GuildBans
GUILD_EMOJIS_AND_STICKERS
GatewayIntentBits.GuildEmojisAndStickers
GUILD_INTEGRATIONS
GatewayIntentBits.GuildIntegrations
GUILD_INVITES
GatewayIntentBits.GuildInvites
GUILD_MEMBERS
GatewayIntentBits.GuildMembers
GUILD_MESSAGE_REACTIONS
GatewayIntentBits.GuildMessageReactions
GUILD_MESSAGE_TYPING
GatewayIntentBits.GuildMessageTyping
GUILD_MESSAGES
GatewayIntentBits.GuildMessages
GUILD_PRESENCES
GatewayIntentBits.GuildPresences
GUILD_SCHEDULED_EVENTS
GatewayIntentBits.GuildScheduledEvents
GUILD_VOICE_STATES
GatewayIntentBits.GuildVoiceStates
GUILD_WEBHOOKS
GatewayIntentBits.GuildWebhooks
DIRECT_MESSAGES
GatewayIntentBits.DirectMessages
DIRECT_MESSAGE_TYPING
GatewayIntentBits.DirectMessageTyping
DIRECT_MESSAGE_REACTIONS
GatewayIntentBits.DirectMessageReactions
N/A
GatewayIntentBits.MessageContent
They're PascalCase in the newest version. So Guilds not GUILDS
Related
I'm having my first go at making a Discord bot. The code is very basic, just a bot that logs its own tag into the console upon startup:
const Discord = require("discord.js");
const TOKEN = "REDACTED"
const client = new Discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES"]
})
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`)
})
client.login(TOKEN)
Upon typing node index.js into the VSCode terminal, however, I am given the following error:
PS C:\Users\15055\Documents\Alt Formatter> node index.js
C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:168
throw new RangeError(ErrorCodes.BitFieldInvalid, bit);
^
RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.
at IntentsBitField.resolve (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:168:11)
at C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:163:54
at Array.map (<anonymous>)
at IntentsBitField.resolve (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:163:40)
at Client._validateOptions (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\client\Client.js:481:41)
at new Client (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\client\Client.js:78:10)
at Object.<anonymous> (C:\Users\15055\Documents\Alt Formatter\index.js:9:16)
at Module._compile (node:internal/modules/cjs/loader:1112:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
at Module.load (node:internal/modules/cjs/loader:988:32) {
[Symbol(code)]: 11
}
Node.js v18.4.0
I have installed discord.js with the command npm i discord.js so I'm using v14.
In discord.js v14, intent flags are available from GatewayIntentBits.
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
})
List of changes:
v12/v13
v14
GUILDS
GatewayIntentBits.Guilds
GUILD_BANS
GatewayIntentBits.GuildBans
GUILD_EMOJIS_AND_STICKERS
GatewayIntentBits.GuildEmojisAndStickers
GUILD_INTEGRATIONS
GatewayIntentBits.GuildIntegrations
GUILD_INVITES
GatewayIntentBits.GuildInvites
GUILD_MEMBERS
GatewayIntentBits.GuildMembers
GUILD_MESSAGE_REACTIONS
GatewayIntentBits.GuildMessageReactions
GUILD_MESSAGE_TYPING
GatewayIntentBits.GuildMessageTyping
GUILD_MESSAGES
GatewayIntentBits.GuildMessages
GUILD_PRESENCES
GatewayIntentBits.GuildPresences
GUILD_SCHEDULED_EVENTS
GatewayIntentBits.GuildScheduledEvents
GUILD_VOICE_STATES
GatewayIntentBits.GuildVoiceStates
GUILD_WEBHOOKS
GatewayIntentBits.GuildWebhooks
DIRECT_MESSAGES
GatewayIntentBits.DirectMessages
DIRECT_MESSAGE_TYPING
GatewayIntentBits.DirectMessageTyping
DIRECT_MESSAGE_REACTIONS
GatewayIntentBits.DirectMessageReactions
N/A
GatewayIntentBits.MessageContent
the solution I've found based on the documentation is :
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]});
Hope it helps
Based on the documentation, you can't use strings for intent names, but you import them from the package, so in your case something like
const Discord = require('discord.js');
const client = new Discord.Client({ intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
]});
should do the trick.
I'm having my first go at making a Discord bot. The code is very basic, just a bot that logs its own tag into the console upon startup:
const Discord = require("discord.js");
const TOKEN = "REDACTED"
const client = new Discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES"]
})
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`)
})
client.login(TOKEN)
Upon typing node index.js into the VSCode terminal, however, I am given the following error:
PS C:\Users\15055\Documents\Alt Formatter> node index.js
C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:168
throw new RangeError(ErrorCodes.BitFieldInvalid, bit);
^
RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.
at IntentsBitField.resolve (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:168:11)
at C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:163:54
at Array.map (<anonymous>)
at IntentsBitField.resolve (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:163:40)
at Client._validateOptions (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\client\Client.js:481:41)
at new Client (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\client\Client.js:78:10)
at Object.<anonymous> (C:\Users\15055\Documents\Alt Formatter\index.js:9:16)
at Module._compile (node:internal/modules/cjs/loader:1112:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
at Module.load (node:internal/modules/cjs/loader:988:32) {
[Symbol(code)]: 11
}
Node.js v18.4.0
I have installed discord.js with the command npm i discord.js so I'm using v14.
In discord.js v14, intent flags are available from GatewayIntentBits.
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
})
List of changes:
v12/v13
v14
GUILDS
GatewayIntentBits.Guilds
GUILD_BANS
GatewayIntentBits.GuildBans
GUILD_EMOJIS_AND_STICKERS
GatewayIntentBits.GuildEmojisAndStickers
GUILD_INTEGRATIONS
GatewayIntentBits.GuildIntegrations
GUILD_INVITES
GatewayIntentBits.GuildInvites
GUILD_MEMBERS
GatewayIntentBits.GuildMembers
GUILD_MESSAGE_REACTIONS
GatewayIntentBits.GuildMessageReactions
GUILD_MESSAGE_TYPING
GatewayIntentBits.GuildMessageTyping
GUILD_MESSAGES
GatewayIntentBits.GuildMessages
GUILD_PRESENCES
GatewayIntentBits.GuildPresences
GUILD_SCHEDULED_EVENTS
GatewayIntentBits.GuildScheduledEvents
GUILD_VOICE_STATES
GatewayIntentBits.GuildVoiceStates
GUILD_WEBHOOKS
GatewayIntentBits.GuildWebhooks
DIRECT_MESSAGES
GatewayIntentBits.DirectMessages
DIRECT_MESSAGE_TYPING
GatewayIntentBits.DirectMessageTyping
DIRECT_MESSAGE_REACTIONS
GatewayIntentBits.DirectMessageReactions
N/A
GatewayIntentBits.MessageContent
the solution I've found based on the documentation is :
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]});
Hope it helps
Based on the documentation, you can't use strings for intent names, but you import them from the package, so in your case something like
const Discord = require('discord.js');
const client = new Discord.Client({ intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
]});
should do the trick.
I tried to code a bot with the new discord.js version but when I try to log in, it's prompting me this error
/home/max/Schreibtisch/Discord Bots/Perplex/node_modules/discord.js/src/util/Util.js:279
if (!Object.hasOwn(given, key) || given[key] === undefined) {
^
TypeError: Object.hasOwn is not a function
at mergeDefault (/home/max/Schreibtisch/Discord Bots/Perplex/node_modules/discord.js/src/util/Util.js:279:17)
at new BaseClient (/home/max/Schreibtisch/Discord Bots/Perplex/node_modules/discord.js/src/client/BaseClient.js:25:20)
at new Client (/home/max/Schreibtisch/Discord Bots/Perplex/node_modules/discord.js/src/client/Client.js:43:5)
at Object.<anonymous> (/home/max/Schreibtisch/Discord Bots/Perplex/src/bot.js:26:16)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Function.Module._load (node:internal/modules/cjs/loader:828:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
this is my bot code
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
});
client.login('token');
I don't think there is an error in this code snippet. If this is your src/bot.js file then it seems okay.
However, the error says "Object.hasOwn is not a function" and Object.hasOwn is only available since node.js v16.9.0.
Discord.js v14 requires node.js 16.9 or higher, so make sure you're up to date. To check your node version, use node -v in your terminal or command prompt, and if it's older than this, update it!
For more breaking changes, you can check out this answer: Discord.js v13 code breaks when upgrading to v14
I have 2 bots that I'm bringing back from deprecated, one works fine, the other one keeps giving me an error for client missing intents. Thing is I put the intents in there, like the other bot but it just won't work and I don't know why.
A helping hand please?
The bash:
Administrator#ServRemu MINGW64 /c/Bots/Rin-master
$ node index.js
C:\Bots\Rin-master\node_modules\discord.js\src\client\Client.js:544
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (C:\Bots\Rin-master\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:544:13)
at new Client (C:\Bots\Rin-master\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:73:10)
at Object.<anonymous> (C:\Bots\Rin-master\index.js:6:23)
←[90m at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m
}
and here's the whole index.js file:
require('dotenv').config();
const Discord = require('discord.js');
const config = require('./config');
const fs = require('fs');
const Enmap = require("enmap");
const discordClient = new Discord.Client();
const { Client, Intents } = require('discord.js');
const client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_MEMBERS,
Discord.Intents.FLAGS.GUILD_PRESENCES,
]
});
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
const event = require(`./events/${file}`);
let eventName = file.split(".")[0];
discordClient.on(eventName, event.bind(null, discordClient));
});
});
discordClient.commands = new Enmap();
fs.readdir("./commands/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./commands/${file}`);
let commandName = file.split(".")[0];
discordClient.commands.set(commandName, props);
});
});
discordClient.login(config.discordApiToken);
This question already has answers here:
How do I fix CLIENT_MISSING_INTENTS error?
(5 answers)
Closed 1 year ago.
I am programming a Discord bot. When I try to run a node, I get this in the CMD window:
C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\Client.js:544
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for
the Client.
at Client._validateOptions (C:\Users\utente\Desktop\bouncerBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:544:13)
at new Client (C:\Users\utente\Desktop\bouncerBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:73:10)
at Object. (C:\Users\utente\Desktop\bouncerBot\main.js:2:16) ←[90m at
Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m ←[90m
at Object.Module._extensions..js
(internal/modules/cjs/loader.js:1101:10)←[39m ←[90m at Module.load
(internal/modules/cjs/loader.js:937:32)←[39m ←[90m at
Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m
←[90m at Function.executeUserEntryPoint [as runMain]
(internal/modules/run_main.js:76:12)←[39m ←[90m at
internal/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m } \
C:\Users\utente\Desktop\bouncerBot>node .
C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\Client.js:544
throw new TypeError('CLIENT_MISSING_INTENTS');
^ \
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for
the Client.
at Client._validateOptions (C:\Users\utente\Desktop\bouncerBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:544:13)
at new Client (C:\Users\utente\Desktop\bouncerBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:73:10)
at Object. (C:\Users\utente\Desktop\bouncerBot\main.js:3:16) ←[90m at
Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m ←[90m
at Object.Module._extensions..js
(internal/modules/cjs/loader.js:1101:10)←[39m ←[90m at Module.load
(internal/modules/cjs/loader.js:937:32)←[39m ←[90m at
Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m
←[90m at Function.executeUserEntryPoint [as runMain]
(internal/modules/run_main.js:76:12)←[39m ←[90m at
internal/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m
I tried to search online for solutions, but I didn't find anything. I also activated the following potions in the bot settings, but nothing changes:
This is my code, even if it is not the cause of the error:
File Main.js
const Discord = require('discord.js');
const fs = require('fs');
const client = new Discord.Client();
let rawdata = fs.readFileSync('config.json');
let config = JSON.parse(rawdata);
const TOKEN = config.botToken
const prefix = config.prefix
client.login(TOKEN)
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot)
return;
const args = message.content.slice(prefix.lenght).split(/ +/)
const command = args[1].toLowerCase()
console.log(args)
// Command test!
})
client.once('ready', () => {
console.log("Discord bot online")
});
File config.json:
{
"botToken":"",
"prefix":"!pgc"
}
I also tried to update the code as follows:
const Discord = require('discord.js');
const fs = require('fs');
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
let rawdata = fs.readFileSync('config.json');
let config = JSON.parse(rawdata);
const TOKEN = config.botToken
const prefix = config.prefix
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot)
return;
const args = message.content.slice(prefix.lenght).split(/ +/)
const command = args[1].toLowerCase()
console.log(args)
// Command test!
})
client.once('ready', () => {
console.log("Discord bot online")
});
client.login(TOKEN)
But I still get an error:
(node:12216) UnhandledPromiseRejectionWarning: ReferenceError:
AbortController is not defined
at RequestHandler.execute (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\rest\RequestHandler.js:172:15)
at RequestHandler.execute (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\rest\RequestHandler.js:176:19)
at RequestHandler.push (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\rest\RequestHandler.js:50:25)
at async WebSocketManager.connect (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:128:9)
at async Client.login (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\Client.js:245:7)
(Use node --trace-warnings ... to show where the warning was
created) (node:12216) UnhandledPromiseRejectionWarning: Unhandled
promise rejection. This error originated either by throwing inside of
an async function without a catch block, or by rejecting a promise
which was not handled with .catch(). To terminate the node process on
unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict (see
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 2) (node:12216) [DEP0018] DeprecationWarning: Unhandled
promise rejections are deprecated. In the future, promise rejections
that are not handled will terminate the Node.js process with a
non-zero exit code.
First of all, never show a token of your bot, or try to change it later.
Second of all:
Try using this. Since the new updated version of discord.js, like version ^13.0, you have to specify client intents:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
And obviously, put client.login(TOKEN) at the very bottom.
For more updates required from new version of discord.js, see What's new.