Discord.py how to delete all roles in server

Viewed 915

I'm trying to delete all roles in my discord server, but it takes a huge amount of time. So I decided to do this task with discord.py bot, but I'm getting this error:

discord.errors.HTTPException: 400 Bad Request (error code: 50028): Invalid Role

Here's my code:

@client.command()
async def delroles(ctx):
 for role in ctx.guild.roles:  
     await role.delete()
1 Answers

The problem is that all users have an "invisible role" called @everyone, which is impossible to remove.

Do:

async def delroles(ctx):
 for role in ctx.guild.roles:  
     try:  
        await role.delete()
     except:
        await ctx.send(f"Cannot delete {role.name}")
Related