【问题标题】:How do i Define MissingPermissions for a discord bot python 3.6我如何为不和谐的机器人 python 3.6 定义 MissingPermissions
【发布时间】:2019-03-21 06:13:05
【问题描述】:
@warn.error
async def kick_error(error, ctx):
  if isinstance(error, MissingPermissions):
      text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
      await bot.send_message(ctx.message.channel, text)

它运行,但是当我使用警告命令时,它打印出 MissingPermissions 未定义 如何定义?

eError: name 'MissingPermissions' is not defined

【问题讨论】:

    标签: python python-3.x python-3.6 discord discord.py


    【解决方案1】:

    异步分支没有定义MissingPermissions。相反,has_permissions 将引发CheckFailure

    如果你想编写自己的has_permissions,而确实会引发一个独特的错误,你可以自己继承CheckFailure

    from discord.ext.commands import CheckFailure, check
    
    class MissingPermissions(CheckFailure): pass
    
    def has_permissions(**perms):
        def predicate(ctx):
            msg = ctx.message
            ch = msg.channel
            permissions = ch.permissions_for(msg.author)
            if all(getattr(permissions, perm, None) == value for perm, value in perms.items()):
                return True
            raise MissingPermissions() 
        return check(predicate)
    

    这与重写 has_permissions 不完全一样,其中包括 MissingPermissions 对象中缺少的权限,但它应该足够接近以模拟相同的控制流

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2017-04-26
      • 2021-10-30
      • 2021-05-27
      • 2021-07-15
      • 2021-06-02
      • 2022-10-08
      • 2020-07-17
      • 2021-03-29
      相关资源
      最近更新 更多