Removing and adding roles at the same time

Hello, I am trying to remove certain roles and then add other roles to a member once a command is used. The issue is that if you remove some roles and then add different roles, no roles will be removed from a member. This is an issue with asynchronous code. I have tried putting addRole() into a chained then() but that does not solve the issue. The only workaround I could find was using setTimeout() which seems like a very wonky way to handle this. Is there any other workaround I could use?
14 Replies
d.js toolkit
d.js toolkit7mo 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! - Marked as resolved by OP
d.js docs
d.js docs7mo ago
:method: GuildMemberRoleManager#set @14.15.3 Sets the roles applied to the member.
// Set the member's roles to a single role
guildMember.roles.set(['391156570408615936'])
.then(console.log)
.catch(console.error);
// Set the member's roles to a single role
guildMember.roles.set(['391156570408615936'])
.then(console.log)
.catch(console.error);
Mark
Mark7mo ago
use this to add/remove the roles in one api request
Nikki 🌺
Nikki 🌺OP7mo ago
I cannot use set for my use
Mark
Mark7mo ago
why not?
Nikki 🌺
Nikki 🌺OP7mo ago
Should have mentioned that in my post
Mark
Mark7mo ago
please explain
Nikki 🌺
Nikki 🌺OP7mo ago
Wouldn't setting the roles overwrite roles the member already has? I'd have to get the members roles and then use set with those and my changes. What if a role is above the bot's highest role? Will it just ignore that role or how is this handled?
Mark
Mark7mo ago
yes, it sets the member's roles to what you provide in the array
Nikki 🌺
Nikki 🌺OP7mo ago
I don't want to set all the member's roles though I just want to add and remove some roles
Mark
Mark7mo ago
so take the member's original roles, make an array, then add/remove the roles you want to add/remove, then pass the modified array to set()
Nikki 🌺
Nikki 🌺OP7mo ago
My question remains
"What if a role is above the bot's highest role? Will it just ignore that role or how is this handled?"
Would be annoying if the entire request would error without adding/removing any roles just because one of the roles the member already had is not accessible by the bot
Mark
Mark7mo ago
i don't believe it'll throw an error but lemme see if i can get some clarification for you
Nikki 🌺
Nikki 🌺OP7mo ago
Alright Just tested that out and it seems to have worked It does indeed just ignore roles the member already had before the set So I just set the member's roles to their existing roles, filter out the roles that should be removed and add the new roles
Want results from more Discord servers?
Add your server