Find what role was added/removed with guildMemberUpdate

Hi, I am trying to detect when a role is removed from a user. I currently have the following code:
client.on("guildMemberUpdate", async (old, new_) => {
if(old.roles.cache.size !== new_.roles.cache.size) { // roles have been updated
let currentRole = null
old.roles.cache.forEach((role: Role) => {
if (!new_.roles.cache.has(role.id)) currentRole = role.id
})
if (currentRole) { // the currentRole has been REMOVED
console.log(currentRole.name+" has been removed.")
}
}
})
client.on("guildMemberUpdate", async (old, new_) => {
if(old.roles.cache.size !== new_.roles.cache.size) { // roles have been updated
let currentRole = null
old.roles.cache.forEach((role: Role) => {
if (!new_.roles.cache.has(role.id)) currentRole = role.id
})
if (currentRole) { // the currentRole has been REMOVED
console.log(currentRole.name+" has been removed.")
}
}
})
I thought it would work, and in theory it does, however it doesn't work when the member's roles aren't in the cache (which is quite possible in my use case) so I was wondering if there's any way to detect what role has been removed given that the old's roles may not be cached. Thanks!
4 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
d.js docs
d.js docs17mo ago
guide Popular Topics: Miscellaneous - How do I check which role was added/removed and for which member? read moremethod Collection#difference() The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
treble/luna
treble/luna17mo ago
ah
SpecialSauce
SpecialSauce17mo ago
Checking the audit logs for modified roles is the only way I would think would work.
Want results from more Discord servers?
Add your server