【问题标题】:How can i check if a user has a specific role如何检查用户是否具有特定角色
【发布时间】:2022-02-27 21:43:27
【问题描述】:
@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。 有人可以帮我吗?

【问题讨论】:

  • 请澄清您的确切问题并提供一小段相关的代码。

标签: python discord.py user-roles


【解决方案1】:

你也许可以这样做:

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

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

   (insert code needed)

老实说,我对您想在这里做什么感到非常困惑(这就是为什么我添加了“需要插入代码)部分)所以下次尝试指定您需要帮助的内容,并提供一小段代码想要专注。

【讨论】:

  • 哦,对不起,是的,我会努力改进的。谢谢你的帮助!我试试看
【解决方案2】:

要检查 discord.py 中的角色,如果您在命令中,请执行以下操作:

role = ctx.guild.get_role(role id here) #by id
role = discord.utils.get(guild.roles, name="name here") #by name

if role in ctx.author.roles:

    #do stuff

如果没有找到,这两个函数都不返回

【讨论】:

    猜你喜欢
    • 2010-12-22
    • 2019-06-16
    • 2021-10-25
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2018-11-06
    相关资源
    最近更新 更多