0

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.

G. Anderson
  • 5,815
  • 2
  • 14
  • 21
Mr Fuzzy
  • 121
  • 2
  • 6
  • I've edited your question to have a more descriptive title. For best results in the future, "Not sure how to do this" tells potential users nothing about the problem – G. Anderson Aug 26 '19 at 21:19

2 Answers2

0
@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.

Ecks Dee
  • 455
  • 1
  • 6
  • 15
0

This is the fix

@commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member=None, rolename:discord.Role=None):
    if rolename not in user.roles:
        await user.add_roles(rolename)
        await ctx.send("Person doesn't have the role and it has been given to him/her")
    else:
        await ctx.send("Person already has role")```
Mr Fuzzy
  • 121
  • 2
  • 6