roles.remove() removes unintended roles when used adjacently to roles.add()
See original GitHub issueWhich package is this bug report for?
discord.js
Issue description
In one sentence, roles.remove()
exhibits incorrect behavior when preceded by roles.add()
, specifically when removing an array. More specifically, it removes role(s) that were just .add()
ed (but not passed to the remove
method call). Please see the code below for a few demonstrated/commented cases.
I set this at medium priority seeing as it is moderately problematic and a genuine occurrence of incorrect behavior.
Code sample
import { Client, GuildMember, Intents } from "discord.js";
const botToken = TOKEN_HERE;
const guildId = "970176225781878794";
const userId = "253338867950813194";
const roleIdToAdd = "970176225781878799";
// Does not contain roleIdToAdd
const roleIdsToRemove = [
"970176225781878795",
"970176225781878796",
"970176225781878797",
"970176225781878798",
];
const onReady = async (client: Client) => {
const guild = await client.guilds.fetch(guildId);
const user = await guild.members.fetch(userId);
// Change to run different cases
const caseId: number = 2;
switch (caseId) {
case 1:
await case1(user);
return;
case 2:
await case2(user);
return;
}
};
// Add single, remove array
// THIS DOES NOT WORK - Results in roleIdToAdd being added THEN REMOVED
const case1 = async (user: GuildMember) => {
console.log(`Adding: ${roleIdToAdd}`);
await user.roles.add(roleIdToAdd);
console.log(`Removing: ${roleIdsToRemove.join(", ")}`);
await user.roles.remove(roleIdsToRemove);
};
// Add single, remove one by one
// THIS WORKS - Results in roleIdToAdd being added
const case2 = async (user: GuildMember) => {
console.log(`Adding: ${roleIdToAdd}`);
await user.roles.add(roleIdToAdd);
for (const roleIdToRemove of roleIdsToRemove) {
console.log(`Removing: ${roleIdToRemove}`);
await user.roles.remove(roleIdToRemove);
}
};
const run = async () => {
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.on("ready", (x) => onReady(x));
await client.login(botToken);
};
run().catch((e) => {
console.log(e);
});
Package version
13.8.0
Node.js / Typescript versions
Node: 16.15.1 Typescript: 4.5.5
Operating system
Windows 10
Priority this issue should have
Medium (should be fixed soon)
Which partials do you have configured?
No Partials
Which gateway intents are you subscribing to?
Guilds
I have tested this issue on a development release
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8 (6 by maintainers)
Top Results From Across the Web
node.js - Discord.JS able to add role but unable to remove role?
I've been recently pondering this piece of code for the longest time. I am able to add the role, but the second I...
Read more >Install or Uninstall Roles, Role Services, or Features
To remove roles and features by using the Uninstall-WindowsFeature cmdlet. Do one of the following to open a Windows PowerShell session with ...
Read more >All Functions - Appian Documentation
This table is a searchable listing of all Appian functions, ... within your expressions to manipulate, insert, and/or select values from arrays. remove()....
Read more >How to Add a Row or Column to a Table in Excel | CustomGuide
How to Add Rows and Columns to Excel Tables. ... You can also remove unwanted table rows and columns by deleting them. Select...
Read more >XPath and XQuery Functions and Operators 3.1 - W3C
These functions and operators are defined for use in [XML Path ... That is, calling fn:string() is equivalent to calling fn:string(.) .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ideally, you would just take the current role ids in an Array, add/remove the ones as desired, then call
GuildMember#roles#set
yourself (single call).For a 100% success rate you can also do just 1 call and use .set, you’d just have to compute the resulting roles yourself