https://i.sstatic.net/YbBSI.png
In my project using discord.js version 12.3.1, I am working on implementing a feature where users can react to a message and get assigned a role based on their reaction. For example, clicking on the 🔵 emoji gives you the 'BLUE' role. However, if you then click on 🟢, your 🔵 reaction is removed and you receive the 'GREEN' role instead.
The issue I'm facing is that adding the new role doesn't work consistently. Sometimes, even though the newly added role is not part of the collection of removed roles, it still gets instantly removed right after being added to the user.
Upon examining my code, here are the specific lines that I suspect might be causing the problem:
const addRole = guildRoles.find(role => role.name === colorRoles[emojiName])
// addRole: Role (e.g., with the name 'GREEN')
guildMember.roles.add(addRole)
const removeRoles = guildRoles.filter(role => Object.values(colorRoles).filter(color => color !== colorRoles[emojiName]).includes(role.name))
// removeRoles: Collection of Roles (e.g., with names 'BLUE', 'RED', 'YELLOW')
guildMember.roles.remove(removeRoles)
// At this point, 'GREEN' is added and removed; all other color roles are removed
You can find the full method along with more context on Pastebin
I'm struggling to understand why the newly added color role sometimes gets removed immediately and would greatly appreciate any advice on resolving this issue.