【问题标题】:In discord.py, How to allow a command from users with multiple roles在 discord.py 中,如何允许来自具有多个角色的用户的命令
【发布时间】:2021-10-01 22:36:19
【问题描述】:

我有一个不和谐的机器人,它通过以下命令检查用户角色:

@commands.has_role("Lab Demonstrator")

但是,在多台服务器上,每台服务器的角色设置略有不同,我想检查他们是否拥有“实验室演示者”或“员工”或“教师”权限

我试过了 @commands.has_role("Lab Demonstrator" OR "Staff")

并没有成功。

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    使用commands.has_any_role 装饰器:

    @commands.has_any_role("Lab Demonstrator", "Staff", "Teacher")
    async def ...
    

    【讨论】:

      【解决方案2】:

      如果您仅检查角色以确保成员具有权限,那么通常这不是一个好的做法。您可以改为使用

      from discord.ext.commands import has_permissions
      ...
      @client.command(name='whatever')
      @has_permissions(administrator=True) #or whichever permissions you want here
      

      例如manage_webhooksban_memberscreate_instant_invite。即使角色名称或相关服务器发生更改,这也可确保权限的一致性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-08
        • 2020-10-16
        • 2021-02-07
        • 2022-01-09
        • 2012-10-09
        • 2019-04-04
        相关资源
        最近更新 更多