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

2024-06-14 10:46:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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命令


Tags: 命令titleembeddescriptionallcommandsmemberctx
2条回答

在代码中,您执行了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中的任何一个,则会发送调节命令

您正在查看列表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中)

相关问题 更多 >