My code has been working just fine for weeks, but a few events and functions have randomly stopped working!
Firstly, my guildMemberAdd
, guildMemberRemove
, and guildMemberUpdate
events simply stopped doing anything. No errors are appearing, and when I debugged my code, I realized the event wasn't even being emitted when the corresponding action took place.
// const client = new Discord.Client();
client.on('guildMemberAdd', (member) => // not triggering!
client.channels.cache.get('channel-id').send(`${member.tag} joined!`); // not sending!
Secondly, when trying to get a member from the GuildMemberManager
cache, it always returns undefined
:
const member = message.guild.members.cache.get(targetID); // undefined
When I then tried to display every member in that guild's member cache, it only showed me and my bot instead of the usual 100+ members.
Then I tried to fetch every member in my guild using GuildMemberManager.fetch()
:
const members = await message.guild.members.fetch();
But I got this error:
[GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time.
Again, I'm sure my syntax is correct, as it's been working perfectly for a while, and I haven't updated anything recently that would affect this code.
Maintaining a stateful application can be difficult when it comes to the amount of data you're expected to process, especially at scale. Gateway Intents are a system to help you lower that computational burden.
Gateway intents allow you to pick and choose what events you choose to "subscribe" to so that you don't have to use up storage on events you're not using. This feature was introduced by Discord in 2020 and was supported by discord.js in v12.
Some intents are defined as "Privileged" due to the sensitive nature of the data. Those intents are:
GUILD_PRESENCES
GUILD_MEMBERS
As of October 27th 2020, these intents have been turned off by default.
GUILD_PRESENCES
Guild.memberCount
returns count as of readyPresences
do not trigger (presenceUpdate
)Presence
data returns null
or undefined
GuildMembers
appear to the bot as offline.client.login()
times out if you specified the fetchAllMembers
option in your ClientOptions
GUILD_MEMBERS
GuildMemberManager.fetch()
and UserManager.fetch()
methods time outGuildMembers
do not trigger (guildMemberAdd
, guildMemberRemove
, guildMemberUpdate
, guildMemberSpeaking
, and guildMembersChunk
)Through the Discord Developer Portal:
Firstly, you must manually enable the intents from the Discord Developer site. Go to applications, select your app, and find the "bot" tab on the sidebar. Then you can scroll down until you see this:
As shown in the screenshot, your bot will require verification if in more than 75 guilds.
If your bot is verified:
Once your bot is verified, you won't be able to manually flip the intent switches in the Developer Portal. To request whitelisted access to an additional privileged gateway intent for a verified bot, please send our support team a ticket here! Make sure to include your bot's ID, which intents you're requesting, a basic description of your use case for the requested intent, and screenshots or video of that use case in action (or code snippets, if not user facing!).
Through the discord.js module:
Once you have either/both intents checked off, you just have to enable them through discord.js. The discord.js intents guide thoroughly explains how to do this, but I will paraphrase it here.
You don't need to go through these steps if you want every intent. Discord enables all intents (except these two, obviously) by default. As long as you checked off both intents in the Developer Portal, you can stop here if you don't care about blocking any other intents. If you do, just remember intents are only supported by discord.js v12+, so you might have to upgrade.
One of the ClientOptions
(ClientOptions
is a typedef of potential options to pass when creating your client) is ws
(yet another typedef of potential websocket options). In there, you'll find the intents
property.
intents
accepts a IntentsResolvable
, which can be a string or an array of strings of an intent(s) (such as 'GUILD_PRESENCES'
. All available intents), a bitfield (a number corresponding to an intent(s)), an instance of the Intents
class.
Examples:
// using a string
const client = new Discord.Client({ ws: { intents: 'GUILD_PRESENCES' }});
// using an array
const client = new Discord.Client({ ws: { intents: ['GUILD_PRESENCES', 'GUILD_MEMBERS'] }});
// using a bitfield value
const client = new Discord.Client({ ws: { intents: 32509 }));
// using Intents class
const client = new Discord.Client({ ws: { intents: Discord.Intents.PRIVILEDGED }});
const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});
To fix this issue, go to:
Discord Developer Portal > Applications > Your Application > Bot > Check both/either intent(s) (screenshot above)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With