Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove multiple roles from all members in a guild discord.py

I'm attempting to have self-assigned roles that users can get in order to have access to specific voice/text channels and I want to be able to remove said roles from all who have it with a single command (so for when I work on adding more features to said roles, I can ensure no one really interferes).

Currently, I'm working with this so far, it's meant to remove all self-assigned roles from the person who typed out the command, for now I've made it only for admins, but it ain't really working hahaha.

@bot.command()
@commands.has_permissions(ban_members=True)
async def swipe(ctx):
    member = ctx.message.author
    role1 = get(member.guild.roles, name = "Minecraft")
    role2 = get(member.guild.roles, name = "CS:GO")
    role3 = get(member.guild.roles, name = "Valorant")
    role4 = get(member.guild.roles, name = "PUBG")
    role5 = get(member.guild.roles, name = "TF2")
    role6 = get(member.guild.roles, name = "COD")
    await member.remove_roles(role1, role2, role3, role4, role5, role6)
    await ctx.send(f'Removed **all** experimental roles.')

So to sum it up, I'm trying to create a command that allows admins to be able to remove certain roles (Minecraft, CS:GO, Valorant, PUBG, TF2, COD) from everyone in the server who has said role with a single command (swipe). All suggestions and ideas are welcome!

Thank you in advance!

like image 327
Diamond Avatar asked Jan 21 '26 09:01

Diamond


1 Answers

Assuming get() is being imported from discord.utils, this would remove all specified roles from the user that executes the commmand:

@bot.command()
@commands.has_permissions(ban_members=True)
async def swipe(ctx):
    member = ctx.message.author
    role_names = ("Minecraft", "CS:GO", "Valorant", "PUBG", "TF2", "COD")
    roles = tuple(get(ctx.guild.roles, name=n) for n in role_names)
    await member.remove_roles(*roles)
    await ctx.send(f'Removed **all** experimental roles.')

And this would remove all specified roles from all members in the server that have it:

@bot.command()
@commands.has_permissions(ban_members=True)
async def swipe(ctx):
    role_names = ("Minecraft", "CS:GO", "Valorant", "PUBG", "TF2", "COD")
    roles = tuple(get(ctx.guild.roles, name=n) for n in role_names)
    for m in ctx.guild.members:
        try:
            await member.remove_roles(*roles)
        except:
            print(f"Couldn't remove roles from {m}")
    await ctx.send(f'Removed **all** experimental roles.')

The try/except is there for the users that the bot won't be able to remove the roles from. This is most like because the bot doesn't have enough permissions to do it or one of the roles is higher up than the bot's highest role in the role hierarchy.


References:

  • *args usage
  • Member.remove_roles()
  • utils.get()
like image 162
Diggy. Avatar answered Jan 22 '26 23:01

Diggy.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!