You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Discord机器人命令支持两种角色权限?

解决Discord机器人多角色权限判断问题

错误原因分析

你之前的两种写法都存在逻辑或语法问题:

  • 第一种写法中name="🎉 Event Staff Team" and "other role"会被Python解析为取第二个字符串的值,实际只会查找名为"other role"的角色,无法实现多角色判断。
  • 第二种写法不仅漏写了get方法(应为discord.utils.get),还重复传递name参数,Python会保留最后一个name的值,最终也只能匹配单一角色。

正确实现方式

方法一:分别获取角色后用逻辑或判断

直接获取两个目标角色,判断用户是否拥有其中任意一个:

@bot.command()
async def modcmds(ctx):
    # 获取两个目标角色
    event_staff_role = discord.utils.get(ctx.guild.roles, name="🎉 Event Staff Team")
    other_role = discord.utils.get(ctx.guild.roles, name="你指定的另一角色名称")
    
    # 检查用户是否拥有其中任意一个角色
    if event_staff_role in ctx.author.roles or other_role in ctx.author.roles:
        embed = discord.Embed(title="Mod Commands:", description="~lssu (mentions Everyone)\nDescription: Announces a London SSU.\n\n~ssuvote (Mentions here)\nDescription: Starts a SSU vote.\n\nAbuse of these commands will result in punishment.")
        await ctx.reply(embed=embed)
        
        channel = bot.get_channel(1376165325950812222)
        log_embed = discord.Embed(title="Bot Command Log", description=f"The ~modcmds command was just executed by {ctx.author.mention}", color=discord.Color.from_rgb(0, 20, 245))
        await channel.send(embed=log_embed)

方法二:用any()函数批量检查角色

如果后续需要添加更多角色,这种方式更灵活,通过角色名称列表批量匹配:

@bot.command()
async def modcmds(ctx):
    # 定义允许执行命令的角色名称列表
    allowed_role_names = ["🎉 Event Staff Team", "你指定的另一角色名称"]
    
    # 遍历用户的所有角色,检查是否有名称匹配列表中的任意一项
    if any(role.name in allowed_role_names for role in ctx.author.roles):
        embed = discord.Embed(title="Mod Commands:", description="~lssu (mentions Everyone)\nDescription: Announces a London SSU.\n\n~ssuvote (Mentions here)\nDescription: Starts a SSU vote.\n\nAbuse of these commands will result in punishment.")
        await ctx.reply(embed=embed)
        
        channel = bot.get_channel(1376165325950812222)
        log_embed = discord.Embed(title="Bot Command Log", description=f"The ~modcmds command was just executed by {ctx.author.mention}", color=discord.Color.from_rgb(0, 20, 245))
        await channel.send(embed=log_embed)

内容的提问来源于stack exchange,提问作者Morgan Cain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 00:42:35