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!
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!
Share Improve this question edited Jul 14, 2020 at 12:19 zzz asked Jul 14, 2020 at 12:14 zzzzzz 711 gold badge1 silver badge6 bronze badges2 Answers
Reset to default 7I 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.
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.