Issue adding and removing roles in same command

I have a select menu command where you select the roles you want to add/remove from a list when roles are selected it checks if the user has the roles already, what roles didn't get elected and removes the unselected roles and add the selected roles. this issue I am having is when I combine the roles.add and the roles.remove the member roles remain unchanged and no error is given.
try {
if (rolesToAdd.length >= 1 && rolesToRemove.length >= 1) {
await interaction.member.roles.remove(rolesToRemove);
await interaction.member.roles.add(rolesToAdd);
}
if (rolesToAdd.length >= 1) {
console.log('add roles');
await interaction.member.roles.add(rolesToAdd).catch(console.error);
}
if (rolesToRemove.length >= 1) {
console.log('remove roles');
await interaction.member.roles.remove(rolesToRemove).catch(console.error);
}
}
catch (err) {return console.log(err);}
try {
if (rolesToAdd.length >= 1 && rolesToRemove.length >= 1) {
await interaction.member.roles.remove(rolesToRemove);
await interaction.member.roles.add(rolesToAdd);
}
if (rolesToAdd.length >= 1) {
console.log('add roles');
await interaction.member.roles.add(rolesToAdd).catch(console.error);
}
if (rolesToRemove.length >= 1) {
console.log('remove roles');
await interaction.member.roles.remove(rolesToRemove).catch(console.error);
}
}
catch (err) {return console.log(err);}
5 Replies
d.js toolkit
d.js toolkit17mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Fisban Regner
Fisban RegnerOP17mo ago
node v18.16.0
Shaurya
Shaurya17mo ago
obviously it'll be unchanged because you removed and then added the role again oh
Fisban Regner
Fisban RegnerOP17mo ago
full code doesnt this remove roles that are not set though I didn't think to do it that way shouldn't be hard to modify my code because i already do half the process. thank you
Want results from more Discord servers?
Add your server