Do you know why the “message sent” is displayed only with the first solution?
const Discord = require("discord.js");
const config = require("./config.json");
const intents = new Discord.Intents(32767);
const client = new Discord.Client({ intents });
client.on("ready", () => {
console.log("bot is ready");
});
client.on("messageCreate", (message) => {
console.log("message sent");
});
client.login(config.token);
And not with this? (That is the example code on the discord.js documentation.)
const config = require("./config.json");
const { Client, Intents } = require("discord.js");
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.on("ready", () => {
console.log("bot is ready");
});
client.on("messageCreate", (message) => {
console.log("message sent");
});
client.login(config.token);
The bot is ready in both solutions, but I don’t get why only in the first one when I send a message in the server the bot detects it, maybe because I don’t know what does mean “32767”.
Advertisement
Answer
The number 32767
means ALL_INTENTS
. The Intents
class extends a BitField
. Meaning that you can represent all the intents you want via a single number by filling in specific bits of the bitfield.
According to Discord Developer Portal, this is how each flag is represented by a bit shift.
const ALL_INTENTS =
(1 << 0) + // GUILDS
(1 << 1) + // GUILD_MEMBERS
(1 << 2) + // GUILD_BANS
(1 << 3) + // GUILD_EMOJIS_AND_STICKERS
(1 << 4) + // GUILD_INTEGRATIONS
(1 << 5) + // GUILD_WEBHOOKS
(1 << 6) + // GUILD_INVITES
(1 << 7) + // GUILD_VOICE_STATES
(1 << 8) + // GUILD_PRESENCES
(1 << 9) + // GUILD_MESSAGES
(1 << 10) + // GUILD_MESSAGE_REACTIONS
(1 << 11) + // GUILD_MESSAGE_TYPING
(1 << 12) + // DIRECT_MESSAGES
(1 << 13) + // DIRECT_MESSAGE_REACTIONS
(1 << 14); // DIRECT_MESSAGE_TYPING
// Outputs 32767
console.log(ALL_INTENTS);
Do you know why the “message sent” is displayed only with the first solution?
Because in the second solution, you are missing GUILD_MESSAGES
intent to receive the messageCreate
event.