pythondiscord.pyuser-roles

How can i check if a user has a specific role


    role7 = discord.utils.get(guild.roles, name="Moderator")

    if role7 in user.roles:
        await ctx.send(...)
    else:
        await ctx.send(...)

I have this Code and i cant figure out how to check if a user has a role. I looked up on the Internet but it isnt working. i also tried Member.roles. Can someone help me?


Solution

  • To check for a role in discord.py you do the following if you're in a command:

    role = ctx.guild.get_role(role id here) #by id
    role = discord.utils.get(guild.roles, name="name here") #by name
    
    if role in ctx.author.roles:
    
        #do stuff
    

    Both of these funcs return none if not found