如何检查用户是否具有特定角色

时间:2021-03-23 14:42:18

标签: python discord.py user-roles

@bot.command()
async def help(ctx):
    Log = open("logs/log.md", "a")
    Log.write(f"\n {ctx.message.author} used (help) ")

    color = int(0xecec28)
    guild = ctx.guild

    CommandReplyHelpPerms = open("helpCmd/CommandReplyHelpPermsNeeded.md", "r")
    CommandReplyHelpPerms2 = open("helpCmd/CommandReplyHelpPermsNeeded2.md", "r")
    CommandReplyHelpPerms3 = open("helpCmd/CommandReplyHelpPermsNeeded3.md", "r")

    role5 = discord.utils.get(guild.roles, name="PERM-mod")
    role6 = discord.utils.get(guild.roles, name="PERM-admin")
    role7 = discord.utils.get(guild.roles, name="Moderator")

    if not role5:
        role5 = await guild.create_role(name="PERM-mod", colour=discord.Colour(color))
    if not role6:
        role6 = await guild.create_role(name="PERM-admin", colour=discord.Colour(color))

    CommandReplyHelpNoPerms = open("helpCmd/CommandReplyHelpNoPerms.md", "r")

    embed2 = discord.Embed(title="Help", description=f"Zeigt alle Befehle mit der zugehörigen Syntax an",colour=0xecec28)
    embed2.add_field(name="Help", value="Befehle  MIT benötigten Berechtigungen", inline=False)
    embed2.add_field(name="Allgemeine Befehle", value=CommandReplyHelpPerms.read(), inline=False)
    embed2.add_field(name="Mute/Warn/Softban", value=CommandReplyHelpPerms2.read(), inline=False)
    embed2.add_field(name="ModRoles/Permissions", value=CommandReplyHelpPerms3.read(), inline=False)

    embed = discord.Embed(title="Help", description=f"Zeigt alle Befehle mit der zugehörigen Syntax an",
                          colour=0xecec28)
    embed.add_field(name="Befehle OHNE benötigte Berechtigungen", value=CommandReplyHelpNoPerms.read(),
                    inline=False)
    if role7 in user.roles:
        await ctx.send(embed=embed2)
    else:
        await ctx.send(embed=embed)
    Log.write(f" : (help) executed successfully")
    Log.close()

我有这个代码,但我不知道如何检查用户是否有角色。我在互联网上查了一下,但它不起作用。我也试过Member.roles。 有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

你也许可以这样做:

role = (insert role id here) #specifys the role

if role in ctx.message.author.roles: #checks if member has role

   (insert code needed)

老实说,我很困惑你想在这里做什么(这就是为什么我添加了“需要插入代码”部分)所以下次尝试指定你需要帮助的内容,并给出一小段代码想要专注。