'Help'命令,该命令使用此功能自动为commands.group子命令生成帮助

时间:2020-08-24 17:04:32

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

我尝试制作帮助命令,但是我不知道如何为命令组创建帮助命令,这是主要命令与组分开的地方,我尝试做help modmail channel modmail是组,而channel是子命令,它只是说该命令不存在 此功能:

请注意,我并不是在问如何使用commands.group,而是在问我如何使用我的函数来创建 commands.group()上的子命令帮助

    async def cmdhelp(self, ctx, command):
        params = []
        for key, value in command.params.items():
            if key not in ("self", "ctx"):
                params.append(f"[{key}]" if "NoneType" in str(value) else f"<{key}>")
        params = " ".join(params)
        alias = ", ".join(command.aliases)
        commandhelp = command.help
        commanddesc = command.description
        if not command.help:
            commandhelp = "`None`"
        if not command.description:
            commanddesc = "`None`"
        if not command.aliases:
            alias = "`None`"
        embed = discord.Embed(title=f"Help for {command}",
                              colour=0x59FFE7,
                              description=f"**Description:**\n{commandhelp}\n**Usage:**\n`{command} {params}`\n**Aliases:**\n`{alias}`\n**Permission:**\n`{commanddesc}`")
        await ctx.send(embed=embed)

    @commands.group(invoke_without_command=True)
    async def help(self, ctx, *,cmd=None):
        if cmd is None:
           # lets say this is a embed with a help category list
        else:
            if (command := get(self.bot.commands, name=cmd)):
                await self.cmdhelp(ctx, command)

            else:
                await ctx.send("That command does not exist.")

示例: The example of help command I'm trying to make 如果看到的话,它可以与普通命令help modmail一起使用,但是如何为modmail组的子命令设置?这是help modmail channel

1 个答案:

答案 0 :(得分:1)

首先,这不是使用子命令的正确方法。这是使用它们的方法:

client.remove_command("help")

@client.group()
async def main_cmd(ctx):
    print("main command")

@main_cmd.command()
async def sub_cmd(ctx):
    print("subcommand")

不和谐地说main_cmd只会打印"main command",而不和谐地说main_cmd sub_cmd则会先打印"main command",然后再"subcommand"

如果您不希望在调用子命令时运行原始命令,请使用ctx.invoked_subcommand

client.remove_command("help")

@client.group()
async def main_cmd(ctx):
    if ctx.invoked_subcommand != None:
        return
    print("main command")

@main_cmd.command()
async def sub_cmd(ctx):
    print("subcommand")

编辑(问题编辑后): 要为漫游器中的每个命令创建“命令”(实际上不是命令),请使用bot.commands

client.remove_command("help")

@client.command(description = "THIS IS THE MESSAGE THAT WILL APPEAR IN THE SPECIFIC HELP COMMAND")
async def a_command(ctx):
    pass #look at the decorator

@client.command()
async def help(ctx, cmd = None):
    if cmd == None:
        await ctx.send("DEFAULT HELP")
    elif cmd in (cmds := {command.name: command for command in client.commands}):
        await ctx.send("cmd: " + cmds[cmd].description)
    else:
        await ctx.send("command not found")

这会为机器人中的每个命令创建“子命令”。