GuildMemberUpdate's oldMember doesn't have correct roles.

- [email protected] - Node v16.20.1 I'm trying to run some code only when a specific role is either added or removed. After just starting the bot; the oldMember will always only have 1 role (@ everyone). After this code has been run at least once (I assumed) the member will be cached and it can correctly determine the oldMember's roles.
const { Events } = require('discord.js');
require('dotenv').config();

module.exports = {
name: Events.GuildMemberUpdate,
async execute(oldMember, newMember) {
console.log(oldMember.roles.cache); //oldMember only has @everyone role
console.log(newMember.roles.cache); //newMember correctly has 3 or 4 different roles depending on whether the role was just added or removed.
if (
!(
oldMember.roles.resolve(process.env.GREETER_ROLE_ID) ||
newMember.roles.resolve(process.env.GREETER_ROLE_ID)
)
) {
console.log("Event doesn't concern greeter role"); // This gets run if the member isn't cached but the correct role was removed.
return;
} else {
console.log("Event concerns greeter role") //After removing the role and adding it back this will be run every time from now on
}
},
};
const { Events } = require('discord.js');
require('dotenv').config();

module.exports = {
name: Events.GuildMemberUpdate,
async execute(oldMember, newMember) {
console.log(oldMember.roles.cache); //oldMember only has @everyone role
console.log(newMember.roles.cache); //newMember correctly has 3 or 4 different roles depending on whether the role was just added or removed.
if (
!(
oldMember.roles.resolve(process.env.GREETER_ROLE_ID) ||
newMember.roles.resolve(process.env.GREETER_ROLE_ID)
)
) {
console.log("Event doesn't concern greeter role"); // This gets run if the member isn't cached but the correct role was removed.
return;
} else {
console.log("Event concerns greeter role") //After removing the role and adding it back this will be run every time from now on
}
},
};
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.GuildMember],
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.GuildMember],
});
20 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
Emily
EmilyOP17mo ago
this is the console output using ._roles. I know I'm not supposed to use that its just to have a screenshot-sized output haha
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
How would you suggest that I cache all the guild members on the server? Should I do that in my on(Events.ClientReady), or is there another smart way to do that? The server has several thousand people in it so would this be efficient?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
Perfect! How many members do you think the bot would have to oversee for this strategy to not work anymore? its currently at ~7000
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
I understand that but the strategy of caching every user when the bot starts might become too cumbersome
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
const { Events } = require('discord.js');
require('dotenv').config();

module.exports = {
name: Events.ClientReady,
once: true,
async execute(client) {
await client.guilds.resolve(process.env.DISCORD_GUILD_ID).members.fetch();
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};
const { Events } = require('discord.js');
require('dotenv').config();

module.exports = {
name: Events.ClientReady,
once: true,
async execute(client) {
await client.guilds.resolve(process.env.DISCORD_GUILD_ID).members.fetch();
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};
could you just sanity check this solution and I'll mark it as resolved
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
that would cache all guild information (including members) for all guilds the bot is a member of, as opposed to what I did which will cache specifically only members in that specific guild?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
yeah that's what im saying. I'm asking if your example will cache all guilds the bot is in and not just members in the specific guild like my example
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
I'm just trying to work out the specific difference between
client.guilds.resolve(process.env.DISCORD_GUILD_ID).members.fetch();
//and
client.guilds.cache.get();
client.guilds.resolve(process.env.DISCORD_GUILD_ID).members.fetch();
//and
client.guilds.cache.get();
which I think was your other example
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
ooooooh I think i see what you mean so there's no like real difference between the two
client.guilds.resolve(process.env.DISCORD_GUILD_ID).members.fetch();
//and
client.guilds.cache.get(process.env.DISCORD_GUILD_ID).members.fetch();
client.guilds.resolve(process.env.DISCORD_GUILD_ID).members.fetch();
//and
client.guilds.cache.get(process.env.DISCORD_GUILD_ID).members.fetch();
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Emily
EmilyOP17mo ago
alright perfect thanks a lot dorime
Want results from more Discord servers?
Add your server