如何为每个命令创建自定义检查签名?

时间:2019-05-08 07:39:59

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

对于我的帮助命令,我希望为该命令的每一项检查添加一个文本,以使用户知道使用该命令时的必要条件。

这是一些草图代码,以说明我在想什么

from discord.ext.commands import (
    is_owner, has_permissions, guild_only, is_nsfw, has_role,
    command, check,
)

def custom_check(x):

    def predicate(ctx):
        return x in ctx.message.content

    rt = check(predicate)
    rt.args = [x]
    return rt



@command(name = "abc")
@has_role(1234)
@custom_check("1234567890")
async def test1(ctx, a:int):
    print(f"test1 passed. {a}")



@command(name = "def")
@is_owner()
@has_permissions(manage_roles = True)
@has_role(5678)
async def test2(ctx, x:bool):
    print(f"test2 passed. {x}")



check_to_text_dict = {
    is_owner.id: "you must be owner to use this command",
    has_permissions.id: "you must have the following permissions to use this command: {args}",
    guild_only.id: "you must only use this command whilst in a server",
    is_nsfw.id: "you must only use this command in nsfw channels",
    has_role.id: "you must have the following role to use this command: {args[0]}",
    custom_check.id: "you must have the following content in your message for whatever reason: {args[0]}",
}



def get_check_signature(command):
    rt = list()
    for check in command.checks:
        rt.append(check_to_text_dict[check.id].format(args = check.args)) # nonsense

    return "\n".join(rt) if rt else "no rulez!"



print(get_check_signature(test1))
print()
print(get_check_signature(test2))

当然,我在那里写的一切都是废话。没有检查ID或检查保存给定参数的事情。

那我该怎么做?

0 个答案:

没有答案