@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
答案 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
对象中缺少的权限,但它应该足够接近以模仿相同的控制流