为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

catspeake 2023-09-12 16:58:11
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 回答

?
杨__羊羊

TA贡献1943条经验 获得超7个赞

在您的代码中,您做到了if AdminRoles in member.roles:。这意味着如果成员拥有所有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,则将发送审核命令。


查看完整回答
反对 回复 2023-09-12
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

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


查看完整回答
反对 回复 2023-09-12
  • 2 回答
  • 0 关注
  • 58 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信