discord.py check if a user has a particular role

Mr Fuzzy

I'm trying to get this to check if the user has the role that's tagged or not.

discord.utils.get(ctx.guild.roles, name=rolename) and discord.utils.get(user.roles, name=rolename)

@bot.command()
@commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member, rolename: discord.Role):
    role = discord.utils.get(ctx.guild.roles, name=rolename)
    if(not role in user.roles):
        await user.add_roles(rolename)
        embed=discord.Embed(title=f"{user.name} Has been added to a role called: {rolename.name}", color=discord.Color.dark_purple())
        await ctx.send(embed=embed)
    else:
        await ctx.send(f"Hey {ctx.author.name}, {user.name} already has the role called: {rolename.name}")

No errors just keeps giving the role instead of checking if the user has the role or not.

Ecks Dee
@bot.command()
@commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member=None, rolename:discord.Role=None):
    if rolename in user.roles:
        await ctx.send("Person already has role")
    else:
        await user.add_roles(rolename)
        await ctx.send("Person doesn't have the role and it has been given to him/her")

Make sure the bot's role is higher than the role you're trying to give the user though because if It's not then you'll get error 50013 Missing Permissions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I check if a user has a role in discord.py?

How to check is an user has a role on discord

Check if user has role Discord.net

How can you check if a user has a particular role in a particular server

Discord.py: Check if user has role ID from list not working

How to check if member has role discord.py

Discord.py: Changing Nickname if user has specific role

How to check if a specific user has a role? Discord js

Discord.js how to check if user has a role on a specific server

To check if a mentioned user has the role or not in discord.js

How to check if a user has a role from another Discord server

How to check if a user has provided an argument discord.py

Discord.js-commando how would I check if a mentioned user has a role, like a muted role for example?

Discordjs check if a user has a role

Check if User has a certain role

discord.py | Add role to specific user

Discord.py add role to user

discord py give role to specific user id

If the user has this role, so display the message. How to do it? Discord.py

How can I check a specific role on triggerer user?(discord.py)

Discord.JS - Find if a user has a role

Discord.js if user has role?

How do I check if a user has a certain role discord.js v12?

Discord.js: How can you check if a specific user has a role?

How do I check if a user has a specific role? | discord.js v13

Discord bot fails to add a role to a user using discord.py

How to check if a user has a specific role in Meteor

How to check whether user has any role?

Check if User has Role - Parse Cloud Code