Section titled IntentsIntents
Intents are an important part of establishing a WebSocket connection, as they define behaviour regarding gateway events and impact received data via the REST API.
Section titled UsageUsage
_10import { Client, GatewayIntentBits } from 'discord.js';_10_10const client = new Client({_10 intents: [GatewayIntentBits.Guilds],_10});
This is the most basic usage of intents for discord.js. By specifying GatewayIntentBits.Guilds
, we will receive gateway events regarding guilds. This includes receiving information about guilds as the bot starts up, such as the roles in the guild and the respective shard id.
It is recommended to set at least the GatewayIntentBits.Guilds
intent for discord.js.
Let's look at another example.
_10import { Client, GatewayIntentBits } from 'discord.js';_10_10const client = new Client({_10 intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildScheduledEvents],_10});
We've included an additional intent: GuildScheduledEvents
. This intent will allow us to receive gateway events regarding scheduled events.
Imagine a scenario where we have a moderation bot that needs to listen to bans occurring in the server. It would also need to listen to messages to moderate them. The following intents would be needed:
_10import { Client, GatewayIntentBits } from 'discord.js';_10_10const client = new Client({_10 intents: [_10 GatewayIntentBits.Guilds,_10 GatewayIntentBits.GuildModeration,_10 GatewayIntentBits.GuildMessages,_10 GatewayIntentBits.MessageContent,_10 ],_10});
The GatewayIntentBits.GuildModeration
intent allows us to listen for bans occurring in the server.
The last two intents are required to read messages. GatewayIntentBits.GuildMessages
allows us to receive gateway events for messages in a guild. However, we are unable to view the contents of the message as that is tied to a privileged intent: GatewayIntentBits.MessageContent
. That's why this gateway event is specified. Read on for more details about privileged intents.
The full list of GatewayIntentBits
one can specify can be found on the documentation. An explanation of what each intent does can be found on Discord's documentation.
Section titled Privileged intentsPrivileged intents
Some gateway events are considered privileged. Currently, these are:
GatewayIntentBits.GuildPresences
GatewayIntentBits.GuildMembers
GatewayIntentBits.MessageContent
To use these intents, you will need to enable them in the developer portal. If your bot is in over 75 guilds, you will need to verify your bot and request usage of your desired intents.
Carefully think if you need these gateway intents. They are opt-in so users across the platform can enjoy a higher level of privacy. Presences can expose some personal information, such as the games being played and overall online time. You might find that it isn't necessary for your bot to have this level of information about all guild members at all times.
Section titled Disallowed intentsDisallowed intents
Should you receive an error stating you are using disallowed intents, please review your developer dashboard settings for all privileged intents you use. Check the Discord API documentation for up-to-date information.