Double
Double

Reputation: 23

discord.py trying to remove all roles from a user

I have a problem that I`m trying to remove all roles a user has for some kind of mute role but it gives me this error discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role

Here`s my code

@client.command(aliases=['m'])
@commands.has_permissions(kick_members = True)
async def mute(ctx,member : discord.Member):
    muteRole = ctx.guild.get_role(728203394673672333)
    for i in member.roles:
        await member.remove_roles(i)
    await member.add_roles(muteRole)
    await ctx.channel.purge(limit = 1)
    await ctx.send(str(member)+' has been muted!')

I know that this kind of questiion was alredy asked here: How to remove all roles at once (Discord.py 1.4.1). But it wasn`t answered and did not help me at all

Upvotes: 1

Views: 4661

Answers (1)

AbdurJ
AbdurJ

Reputation: 1033

The problem is that all users have an "invisible role", @everyone. You will see it show up if you try

for i in member.roles:
    print(i)

remove_roles is a high level function and it will try to remove @everyone, which is causing your error.

To clear all current roles from the user, you can do:

@client.command(aliases=['m'])
@commands.has_permissions(kick_members = True)
async def mute(ctx, member : discord.Member):
    muteRole = ctx.guild.get_role(775449115022589982)
    await member.edit(roles=[muteRole]) # Replaces all current roles with roles in list
    await ctx.channel.purge(limit = 1)
    await ctx.send(str(member)+' has been muted!')

await member.edit(roles=[]) Replaces all the current roles with the roles you have in the list. Leave the list empty to remove all roles from the user.

discord.Member.edit

Although if you want to do it with a for loop, you can use try

@client.command(aliases=['m'])
@commands.has_permissions(kick_members = True)
async def mute(ctx, member : discord.Member):
    muteRole = ctx.guild.get_role(775449115022589982)
    for i in member.roles:
        try:
            await member.remove_roles(i)
        except:
            print(f"Can't remove the role {i}")
    await member.add_roles(muteRole)
    await ctx.channel.purge(limit = 1)
    await ctx.send(str(member)+' has been muted!')

Upvotes: 6

Related Questions