【问题标题】:How do I show the command checks for a command?如何显示命令检查命令?
【发布时间】:2021-04-20 02:04:59
【问题描述】:

当你声明一个命令时,你知道那些@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. 我用来测试的命令有这些检查顺便说一句:

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

谢谢!!

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    如果您查看discord.py documentation 中的Command.checks attribute,您会看到:

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

    你问什么是谓词? Functional Programming HOWTO: Built-In Functions, A. M. Kuchling:

    “谓词是返回某个条件的真值的函数。”

    所以,对于Command.checks 返回的列表,你真的做不了什么。

    要对函数进行每项检查,您可能必须遍历包装函数的装饰器。我自己不知道该怎么做,但你可以在 this Stack Overflow question 中找到一个很好的例子。

    【讨论】:

    • 哦,好吧。生病调查那个问题。谢谢!!
    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2013-09-23
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多