Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discord.js - remove a specific role from all members

I am trying to remove a role in my guild from many people. Yet, not everyone in the guild has that role, and there is a large number of them.

message.guild.members.cache.forEach(member => {
  member.roles.remove("12345678901234");
});

This code works, but it is not efficient. The above code is very slow. Do you have a better code? Thank you!

like image 551
zzz Avatar asked Jul 14 '20 12:07

zzz


People also ask

How do I delete auto assign roles in discord?

You can remove any role you place here whenever you want by clicking the red Remove button to the right of the role in the Autorole List. Now, ensure that the Dyno Bot has a higher role on your server than the auto-assigned role, or it will not work.


2 Answers

I would delete the role and then create it again.

const role = message.guild.roles.cache.get("RoleID");
message.guild.roles.create({
data: {
name: role.name,
color: role.color,
hoist: role.hoist,
position: role.position,
permissions: role.permissions,
mentionable: role.mentionable
}
})
role.delete('I had to.')

It works very quickly, much faster than manually grabbing each member and removing the role, especially with a big server.

like image 55
MakeHellTal Avatar answered Sep 30 '22 11:09

MakeHellTal


const Role = message.guild.roles.cache.get("RoleID");
Role.members.forEach((member, i) => { // Looping through the members of Role.
    setTimeout(() => {
        member.roles.remove(Role); // Removing the Role.
    }, i * 1000);
});

I assume your code is slow because of Discord's API limits. You have no delays in your code, which means your code will be executed immediately.

The code I provided will remove a role from a member every 1 one second.

If you want to remove the role immediately, you need to delete it. It will be removed from everyone.

like image 44
Jakye Avatar answered Sep 30 '22 11:09

Jakye