Discord.py如何测试成员在角色字典中是否具有特定角色?

时间:2020-09-27 10:03:21

标签: python discord discord.py discord.py-rewrite

AdminRoles = ["Moderation","Administration","Emperor"]
@client.command()
async def Commands(ctx):
    member = ctx.author
    if AdminRoles in member.roles:
        ShowCommand = discord.Embed(
            title = "Moderation Commands",
            description = "All commands",
            colour = discord.Colour.red()
        )
        await ctx.send(embed = ShowCommand)
    else:
        ShowCommand = discord.Embed(
            title = "Member Commands",
            description = "All commands",
            colour = discord.Colour.red()
        )
        await ctx.send(embed = ShowCommand)

我会修复上面的代码,因为当我键入命令时,它会一直显示正常的播放器命令,并且应该显示Mod命令。

2 个答案:

答案 0 :(得分:0)

您正在查看列表AdminRoles是否在member.roles内,整个列表为:

if ["a","b","m"] in members.roles:

但是您希望AdminRoles中的一项是在members.role内部,所以您需要类似以下内容:

test = [e for e in AdminRoles if e in members.roles]
if len(test) > 0:
    doTheRightModeratorThing()
else:
    doTheRightCommonerThing()

(这最后检查adminRoles中的角色中是否至少有一个在member.roles中)

答案 1 :(得分:0)

在您的代码中,您完成了if AdminRoles in member.roles:。这意味着if成员拥有所有AdminRoles。因此,您可以像这样更改代码:

AdminRoles = ["Moderation","Administration","Emperor"]
@client.command()
async def Commands(ctx):
    member = ctx.author
    for role in member.roles:
        if role.name in AdminRoles:
            ShowCommand = discord.Embed(
                title = "Moderation Commands",
                description = "All commands",
                colour = discord.Colour.red()
            )
            await ctx.send(embed = ShowCommand)
            return
    ShowCommand = discord.Embed(
        title = "Member Commands",
        description = "All commands",
        colour = discord.Colour.red()
    )
    await ctx.send(embed = ShowCommand)

在此代码中,如果成员具有AdminRoles中的任何一个,则将发送审核命令。