Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I check if a user has a specific role in discord

This should check if the specific person does or doesn't have the mute role

    @bot.command(pass_context=True)
    @commands.has_role("Admin")
    async def unmute(ctx, user: discord.Member):
        role = discord.utils.find(lambda r: r.name == 'Member', 
    ctx.message.server.roles)
        if user.has_role(role):
            await bot.say("{} is not muted".format(user))
        else:
            await bot.add_roles(user, role)

This error is thrown

Command raised an exception: AttributeError: 'Member' object has no attribute 'has_role'

I don't know how to do it so i would really appreciate every help I can get

like image 592
Deniz Ascherl Avatar asked Feb 23 '19 20:02

Deniz Ascherl


People also ask

How do I check if a user has a role?

If you have the role ID , you can check if the . roles Collection on a GuildMember object includes it, using .has() . Should you not know the ID and want to check for something like a "Mod" role, you can use . some() .


1 Answers

Member does not have a .has_role() method, you can however get a list of all their roles using .roles.

To see if a user has a given role we can use role in user.roles.

    @bot.command(pass_context=True)
    @commands.has_role("Admin")
    async def unmute(ctx, user: discord.Member):
        role = discord.utils.find(lambda r: r.name == 'Member', ctx.message.guild.roles)
        if role in user.roles:
            await bot.say("{} is not muted".format(user))
        else:
            await bot.add_roles(user, role)

Docs for reference: https://discordpy.readthedocs.io/en/latest/api.html#member

Note: ctx.message.guild.roles use to be ctx.message.server.roles. Updated due to API change.

like image 150
ktzr Avatar answered Oct 07 '22 10:10

ktzr