如何为不和谐的bot python 3.6定义MissingPermissions

时间:2018-10-16 16:30:07

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

@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

1 个答案:

答案 0 :(得分:0)

异步分支未定义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对象中缺少的权限,但它应该足够接近以模仿相同的控制流