forfow
forfow

Reputation: 13

How to check if a user has a specific role in a list of roles? (discord.py)

When try to run this code nothing happens. no error aswell btw. I got a list of roles to get this to work but it didn't.

rolelist = [822428355554312212,
            822728446059741218,
            823814683973779488]

check = ''

if rolelist in user.roles:
    check += 'Bot Developer'

Upvotes: 1

Views: 2381

Answers (2)

dank tagg
dank tagg

Reputation: 259

There's also another way. You can use the built in.

Link to the docs: https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.has_any_role

Example from the docs:

@bot.command()
@commands.has_any_role('Library Devs', 'Moderators', 492212595072434186)
async def cool(ctx):
    await ctx.send('You are cool indeed')

Upvotes: 0

Makiyu
Makiyu

Reputation: 421

You can do

if any(role.id in rolelist for role in user.roles):
   ...

Well, what does it do?

Per iteration, it gets a role from a user, gets the ID, and checks if the ID is in the rolelist array. If the role id is in the array, it stops the loop and executes the function it's supposed to do inside the if statement.

Have a good day!

Upvotes: 1

Related Questions