0
votes

I've got a verification bot for my server with linked roles. I've currently got it to remove all roles manually one by one, but of course this is inefficient and it only works for about 5/6 roles before stopping for a few seconds and continuing. What I'd like to try is some sort of discUser.removeRoles kind of thing, if that's possible.

Or is there a way to only try removing a role if the person has it? My code just does discuser.removeRole for every binded rank.

UPDATE

I got a notification about this question, so wanted to update it with a new solution for anyone else who finds this:

  • Create a table of your role ids. (e.g var giveThese = [])
  • guildMember.roles.add(giveThese,"Reason / Description"

For removing, you can replace roles.add with roles.remove

3

3 Answers

0
votes

The Discord API does not have a way to remove multiple roles from a user at once, so neither does Discord.js.

However, you can check if the user has the role before removing it:

if (discuser.roles.has(roleID)) discuser.removeRole(roleID)

// For v12 (latest version):
if (discuser.roles.cache.has(roleID)) discuser.roles.remove(roleID)

Even if your bot stops doing anything for a bit when removing multiple roles, this is likely because Discord.js automatically handles rate limiting and waits until your client can send another request.

0
votes

From what I understand from the question you're looping through every member of the guild and removing the role from each one of them.

To me, the most efficient way to do it is to take from the role the list of the members that have it (with Role.members) and then looping through that list.

You can do something like this:

let roleID = '1234...'

let role = guild.roles.fetch(roleID).then(role => {
  role.members.forEach(member => member.roles.remove(roleID))
})

This is the most efficient way I can think for doing that since Discord currently has no way of "bulk removing" roles from users.

0
votes

I was able to figure out a solution by looping over an external json file that holds all of the roles' data.

In the main file where you are trying to remove the roles, use a for in loop that loops over the json file containing all of the role names. Within the loop put the remove role method in there.

Here's an example of the .json file:

[
    {
        "role_name": "very slightly red",
        "color": "#ffcccc",
        "color_tier": "vs"
    },
    {
        "role_name": "very slightly orange",
        "color": "#ffedcc",
        "color_tier": "vs"
    },
    {
        "role_name": "very slightly yellow",
        "color": "#ffffcc",
        "color_tier": "vs"
    }
]

And here's the code that removes the roles in bulk:

const vs_json = require("../vs_colors.json");


for (var key in vs_json) {
    if (vs_json.hasOwnProperty(key)) {
      console.log(
        key +
          " -> " +
          vs_json[key].role_name
      );
      memberData.roles.remove(
        getRole(
          vs_json[key].role_name
        )
      );
    }
  }