Search code examples
pythondiscorddiscord.pywhitelist

How to whitelist roles from being banned


I asked this question before, but due to my next to zero experience with stack-overflow it messed it up. So i will rewrite the question with the anwser so people looking for this can have it right away.

So my question was, how can i whitelist role from being banned. Now later, with more experience with discord.py i can tell you the anwser listed down below is the way to do it.

The working code: Credit to CaptAngryEyes

@bot.command()
async def ban(ctx, member : discord.Member):
    whitelisted_roles = [728345678923456, 923478569378456, 8923475627893456] # Put the role IDs here
    for role in member.roles:
        if role.id in whitelisted_roles:
            await ctx.send("You can't ban this user! He is a moderator!")
            return
        else:
            # Ban code goes here...
            pass

Solution

  • You can iterate through the roles of the user and check if he has a role in a list. I use the role IDs since it can't be changed.

    @bot.command()
    async def ban(ctx, member : discord.Member):
        whitelisted_roles = [728345678923456, 923478569378456, 8923475627893456] # Put the role IDs here
        for role in member.roles:
            if role.id in whitelisted_roles:
                await ctx.send("You can't ban this user! He is a moderator!")
                return
            else:
                # Ban code goes here...
                pass
    

    If he has a "whitelisted" role we just return nothing and the command ends.