'guildMemberUpdate - empty roles on oldMember after bot start
guildMemberUpdate is giving me the oldMember without roles when it's first changed. is there any solution so this doesn't come with an empty set of roles?
example: I have a user with Staff.
after starting the bot, if I add Partner it counts as 2 roles being added, since the oldMember only comes with the role everyone ('958859282139447347').
Any event changes made after this are correct.
Code:
export default function (oldMember: GuildMember, newMember: GuildMember) {
logger.debug(`Member was updated: ${oldMember.user.username} -> ${newMember.user.username}`);
console.log(oldMember.toJSON());
console.log(newMember.toJSON());
// list all changes between oldMember and newMember
const changes: string[] = [];
if (oldMember.roles.cache.size !== newMember.roles.cache.size) {
changes.push(`Roles: ${oldMember.roles.cache.size} -> ${newMember.roles.cache.size}`);
let removedRoles = oldMember.roles.cache.filter(role => !newMember.roles.cache.has(role.id));
removedRoles.forEach(role => {
changes.push(`Removed role: ${role.name}`);
});
let addedRoles = newMember.roles.cache.filter(role => !oldMember.roles.cache.has(role.id));
addedRoles.forEach(role => {
changes.push(`Added role: ${role.name}`);
});
}
console.log(changes);
}
comparing the before and after so I can log which roles were added/removed
my log:
guildId: '958859282139447347',
joinedTimestamp: null,
premiumSinceTimestamp: null,
nickname: null,
pending: false,
communicationDisabledUntilTimestamp: null,
userId: '842137717440512020',
avatar: null,
displayName: 'Crimson King',
roles: [ '958859282139447347' ],
avatarURL: null,
displayAvatarURL: 'https://cdn.discordapp.com/avatars/842137717440512020/6ef52c6c903d4a62a39931422c33a768.webp'
}
{
guildId: '958859282139447347',
joinedTimestamp: 1649158238716,
premiumSinceTimestamp: null,
nickname: null,
pending: false,
communicationDisabledUntilTimestamp: null,
userId: '842137717440512020',
avatar: null,
displayName: 'Crimson King',
roles: [ '960926353362935858', '962032445623001088', '958859282139447347' ],
avatarURL: null,
displayAvatarURL: 'https://cdn.discordapp.com/avatars/842137717440512020/6ef52c6c903d4a62a39931422c33a768.webp'
}
[ 'Roles: 1 -> 3', 'Added role: Staff', 'Added role: Partner' ]
My client already has intents and partials:
{
intents:
[
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_INTEGRATIONS,
Intents.FLAGS.GUILD_WEBHOOKS,
Intents.FLAGS.GUILD_INVITES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
Intents.FLAGS.GUILD_SCHEDULED_EVENTS,
],
partials:
[
'USER',
'CHANNEL',
'GUILD_MEMBER',
'MESSAGE',
'REACTION',
'GUILD_SCHEDULED_EVENT',
]
});
Can anyone help me?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
