如何显示命令检查命令



当您声明一个命令时,您知道@commands.has_permissions()是怎么回事吗?我正在尝试制作一个命令,显示对命令的检查,但我不确定如何继续。这是我到目前为止的代码:

@commands.command(aliases=['sp'])
async def showperms(self, ctx, *, command_name):
"""Shows the permissions needed for a command"""
com = self.bot.get_command(command_name)
print(com.checks)

当我为一个命令执行此操作时,我会得到以下输出:

[<function bot_has_permissions.<locals>.predicate at 0x7fd64b5ff940>, <function has_permissions.<locals>.predicate at 0x7fd64b5ff550>]

我相信它使用了Discord的权限整数,但几乎没有人知道如何使用这些整数。我不知道如何将其"翻译"为我可以发送的内容,例如manage_messagesadministrator.。我用来测试的命令有以下检查btw:

@commands.has_permissions(manage_channels=True)
@commands.bot_has_permissions(manage_channels=True)

谢谢!!

如果查看Command.checks属性的discord.py文档,您将看到以下内容:

一个谓词列表,用于验证是否可以使用给定的Context作为唯一参数来执行命令。

你问什么是谓词?函数编程HOWTO:内置函数,A.M.Kuchling:

"谓词是返回某个条件的真值的函数。">

因此,使用Command.checks返回的列表,您实际上不能做太多工作。

为了对函数进行每一次检查,您可能需要遍历包装函数的装饰器。我自己不知道如何做到这一点,但你可以在这个Stack Overflow问题中找到一个很好的例子。

最新更新