'DiscordJS - Counting more than what he should [NODE-JS]
Here is my script:
let deleted_roles_counter = 0
console.log("Deleting roles...")
message.guild.roles.cache.forEach(roles => {
roles.delete()
.then(deleted_roles_counter++)
.catch(e => {e})
});
console.log(`Deleted ${deleted_roles_counter} roles`)
I have 3 roles, the bot role (which is not deletable), the @everyone role (which is not either) and the role : New Role (which is deletable)
Now as you can see on the .then their is a counter, but the counter count the 3 roles instead of just the role that have been deleted (New Role) [1]
I've also tried to put deleted_roles_counter - 1 in the catch but this don't more either
Would one of you have the idea for how to change this?
Solution 1:[1]
I think the best solution is to add if statement before you call the .delete() to check if the role is bot role or @everyone role.
let deleted_roles_counter = 0
console.log("Deleting roles...")
message.guild.roles.cache.forEach(roles => {
if(!roles.managed || roles.id !== guild.roles.everyone.id || roles.editable){
roles.delete()
.then(deleted_roles_counter++)
.catch(e => {e})
}
});
console.log(`Deleted ${deleted_roles_counter} roles`)
.managed returns a boolean whether or not the role is managed by an external service (bot), and the second one is self-explanatory, it checks if the role is @everyone
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
