malving
malving

Reputation: 33

What is wrong in this Python code (discord.py)?

@client.command(name="delete_role", pass_context=True)
async def delete_role(ctx):
    for role in ctx.guild.roles:
        if role.name != "Team" or "Team(OLD)" or "Admin" or "Админ" or "Rythm" or "Toxic Bot" or "@everyone":
            await role.delete()
            await ctx.send(f"deleted {role}")

I was trying to delete all roles on my server, but those which I choose.

Upvotes: 0

Views: 54

Answers (1)

Joac
Joac

Reputation: 490

Given you actual code, the expresion that you have on the if statement will always evaluate to True, because all non empty strings are evaluated to True in python.

>>> bool("")
False
>>> bool("a")
True

You can change your code to something like this

PRESERVE_USERS = [
    "Team", 
    "Team(OLD)", 
    "Admin", 
    "Админ", 
    "Rythm", 
    "Toxic Bot", 
    "@everyone",
]


@client.command(name="delete_role", pass_context=True)
async def delete_role(ctx):
    for role in ctx.guild.roles:
        if role.name not in PRESERVE_USERS:  
            await role.delete()
            await ctx.send(f"deleted {role}")

Upvotes: 3

Related Questions