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 toolkit•4mo 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 docs•4mo 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
Mark•4mo ago
use this to add/remove the roles in one api request
Nikki 🌺
Nikki 🌺•4mo ago
I cannot use set for my use
Mark
Mark•4mo ago
why not?
Nikki 🌺
Nikki 🌺•4mo ago
Should have mentioned that in my post
Mark
Mark•4mo ago
please explain
Nikki 🌺
Nikki 🌺•4mo 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
Mark•4mo ago
yes, it sets the member's roles to what you provide in the array
Nikki 🌺
Nikki 🌺•4mo ago
I don't want to set all the member's roles though I just want to add and remove some roles
Mark
Mark•4mo 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 🌺•4mo 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
Mark•4mo ago
i don't believe it'll throw an error but lemme see if i can get some clarification for you
Nikki 🌺
Nikki 🌺•4mo 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