Search code examples
pythondiscorddiscord.pycommandbots

Discord.py remove all roles command


I made a remove all roles command but when I use it the bot says I don't have permission to remove roles When it does what do i do

@bot.command()
@commands.has_permissions(manage_roles=True)
async def strip(ctx, member: discord.Member):
    try:
        await member.edit(roles=[])
        await ctx.send(f"All roles have been removed from {member.display_name}.")
    except discord.Forbidden:
        await ctx.send("I don't have permission to remove roles.")
    except discord.HTTPException:
        await ctx.send("Failed to remove roles.")

Solution

  • The problem is when you use member.edit then set the roles list to nothing, than your command tries to remove all roles from a member includeing @everyone.

    So I have two solutions for you: First:

    @bot.command()
    @commands.has_permissions(manage_roles=True)
    async def strip(ctx:commands.Context,member:discord.Member):
        try:
            for i in member.roles:
                try:
                    await member.remove_roles(i)
                except:
                    print(f"Can't remove the role {i}")
            await ctx.send(f"Roles have been removed from {member.mention}")
        except discord.Forbidden:
            await ctx.send("You don't have permission")
        except discord.HTTPException:
            await ctx.send("Command failed")
    

    Second (similar to yours):

    @bot.command()
    @commands.has_permissions(manage_roles=True)
    async def strip2(ctx:commands.Context, member: discord.Member):
        everyone = ctx.guild.default_role
        try:
            await member.edit(roles=[everyone])
            await ctx.send(f"All roles have been removed from {member.display_name}.")
        except discord.Forbidden:
            await ctx.send("I don't have permission to remove roles.")
        except discord.HTTPException:
            await ctx.send("Failed to remove roles.")
    

    In the second method if you try it on a bot it wont work, because bots have roles assigned to them which you can't remove. What you should do then is to define that role like this:

    guild=ctx.guild
    role = guild.get_role(ROLE_ID)
    

    Then paste in the role to the list after or before everyone:

    await member.edit(roles=[everyone,role]
    

    Be aware, because if you leave it like this it will assign the bots role to any member you use the command on, so do this only while testing than remove the role from the list!