Emily
Emily
DIAdiscord.js - Imagine an app
Created by Emily on 7/19/2023 in #djs-questions
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],
});
46 replies