【问题标题】:Is there any way for my discord.py bot to check check for whether a user has a certain permission to use a command?我的 discord.py 机器人有什么方法可以检查用户是否具有使用命令的特定权限?
【发布时间】:2021-09-14 19:24:05
【问题描述】:

例如,我的机器人有服务器设置,目前,任何可以发送消息的用户都可以编辑设置。如果没有Manage Server 权限的用户尝试编辑权限,我很乐意向the one the Dank Memer Bot sends 发送类似的消息。我四处搜索,似乎您只能看到您自己的机器人拥有哪些权限,但我不确定。我有办法做到这一点吗?谢谢。

【问题讨论】:

  • 我在 Google 上搜索了“python discord.py 检查用户权限”,第一个结果是 codegrepper.com/code-examples/python/… 似乎是你要找的。​​span>
  • @TheLazyScripter 我已经看过了,但我相信不幸的是,它只检查机器人本身是否具有某些权限,而不是检查某个用户是否具有某些权限。 :(
  • 在 discord.py 文档中,查看 Member.rolesMember.permissionsRole.permissions。只要您对成员有一些引用(如果您在公会中回复他们的消息,您应该这样做),您可以引用这些来检查必要的权限。
  • @Stephen 非常感谢!我现在明白了!

标签: python permissions discord discord.py bots


【解决方案1】:

显然我刚刚做了

# check if someone has certain permissions
def hasPerms(member, perms):
  for role in reversed(member.roles):
    if not perms.is_subset(role.permissions):
        return False
  return True

它有效。

【讨论】:

    【解决方案2】:

    对于命令,

    from discord.ext import commands
    
    @commands.has_permissions(manage_server=True)
    

    您可以将manage_server 更改为您想要的任何权限。这个Discord API

    然后你可以做一个错误处理程序。所以如果用户没有权限执行这个命令,它会发送消息。

    @command.error
    async def command_error(ctx, error):
        if isinstance(error, MissingPermissions):
            await ctx.send('You do not have permission to do this command!')
    

    必须command 更改为您希望Manage Server 启用的任何命令。

    【讨论】:

      猜你喜欢
      • 2019-04-04
      • 2011-01-30
      • 1970-01-01
      • 2019-11-16
      • 2019-03-06
      • 2021-10-10
      • 1970-01-01
      • 2023-02-20
      • 2021-03-31
      相关资源
      最近更新 更多