discord.py检查用户是否具有特定角色

时间:2019-08-26 21:00:39

标签: python discord discord.py-rewrite

我正在尝试获取它来检查用户是否具有被标记的角色。

discord.utils.get(ctx.guild.roles,name =角色名称) 和 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}")

没有错误只会继续赋予角色,而不是检查用户是否具有该角色。

2 个答案:

答案 0 :(得分: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")

请确保漫游器的角色比您要赋予用户的角色更高,因为如果不是,则会出现错误50013 Missing Permissions.

答案 1 :(得分:0)

这是解决方法

@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")```