KaanKaragozoglu
KaanKaragozoglu

Reputation: 35

How to remove role from list with Discord bot?

I have a username list like this:

['Username#0001', 'Username#0002'......]

And these users have "x" role.
I want to remove "x" role from these users with a bot command.
Any ideas?

Upvotes: 0

Views: 1070

Answers (2)

Deru
Deru

Reputation: 1173

I think you searched for this

We first get all the users in the guild. We iterate through the users and get all the roles the user has. When we have all the roles the user has we iterate through the roles. We then check if the name of the role is the same as the name of the role we want to remove. If it has the same name we remove it from the user.

role_to_remove = "NAME OF ROLE"
for user in ctx.guild.members:
    for role in user.roles:
        if role.name == role_to_remove:
            await user.remove_roles(role)

This code will check if any user in the server has the role you want to remove.

If you have a usernamelist in string format. You first want to convert them to member objects so you can do the thing described earlier. You can do this with using the following:

userlist = ['Username#0001', 'Username#0002'......]
new_userlist = []
for users in userlist:
    new_userlist.append(ctx.guild.get_member_named(users))
userlist = new_userlist

When you have converted the userlist replace ctx.guild.members with the new userlist.

Make sure to read the documentation:

remove roles

get member by name

get member by user id

Upvotes: 0

Harmon758
Harmon758

Reputation: 5157

If you only have the name of the role, you'd need to go through Guild.roles for the server in question to get the Role object, e.g. by using the get utility function. You'd then need to go similarly through Guild.members and get the Member object for each username and discriminator. Alternatively, Guild.get_member_named does this for you. Then, for each Member object, you can use Member.remove_roles and pass the Role object to it to remove the role from the member.

Upvotes: 1

Related Questions