New to Discord.js, the guildMemberAdd event seems to be working perfectly fine, but it does not detect when a member leaves the guild. How can I fix this?
const { token, guildID, memberChannelID } = require('./config.json');
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_PRESENCES] });
const guild = client.guilds.cache.get(guildID)
const prefix = '?';
client.on('messageCreate', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping') {
message.reply('pong!');
} else if (command === 'help') {
message.reply('**Commands: ** ?ping, ?membercount');
} else if (command === 'membercount') {
message.reply(`There are ${message.guild.memberCount} members in this server!`);
}
});
client.on('guildMemberAdd', async (member) => {
client.channels.cache.get(memberChannelID).setName(`Total Members: ${member.guild.memberCount.toLocaleString()}`)
console.log('Updating Member Count')
})
client.on('guildMemberRemove', async (member) => {
client.channels.cache.get(memberChannelID).setName(`Total Members: ${member.guild.memberCount.toLocaleString()}`)
console.log('Updating Member Count')
})
client.once('ready', () => {
console.log("EGLX BOT IS ONLINE")
})
client.login(token);
If the guild member was not cached when the guildMemberRemove event is supposed to be emitted, then the issue is probably because you lack the necessary partials, an opt-in feature allowing events to emit on uncached structures. Most likely, you would need the 'GUILD_MEMBER' partial for the event to emit although you may also need the 'USER' one if the first doesn't work alone.