discord.py - 如何向 parent-command 添加命令

discord.py - How do I add a command to a parent-command

我想将命令作为参数分配给另一个命令。

我试过这样的事情:

@bot.command()
async def the_parrent():
    pass

@bot.command(parent=the_parrent)
async def child1(ctx):
    await ctx.send("This is child 1")

@bot.command(parent=the_parrent)
async def child2(ctx):
    await ctx.send("And this is child 2")

现在写的时候 !the_parrent 什么都没有按预期发生,但如果我写 !the_parrent child1 或者 !the_parrent 孩子 2 什么也没有发生。

但如果我只写!child1!child2,则相应的消息由bot发送。

内置的 !help 命令还显示 child1child2 未分配也 the_parrent:

​No Category:
 child1      
 child2      
 help        Shows this message
 the_parrent 
 
Type !help command for more info on a command.
You can also type !help category for more info on a category.

那么,我的问题是我对 parent-parameter 的理解有误吗?如果没有,如何将一个命令添加到另一个命令?

没有parent参数! parents 属性只是一个 attribute,returns 该命令分配给的所有父项。这些东西叫做 command groups 而不是 parents,你应该像这样创建你的“父命令”:

@bot.group()
async def parent_command(ctx):
    pass

通过给它 bot.group() 装饰器。

之后,您可以使用 @parent_command.command() 而不是 @bot_command 来为其分配子命令。

@parent_command.command()
async def subcommand(ctx):
    await ctx.send("This is child 1.")

您可以选择是否始终希望调用父命令,或者仅在没有找到子命令时通过向其添加 ìnvoke_without_command=True kwarg 来选择。

@bot.group(invoke_without_command=True)
async def parent_command(ctx):
    pass

这样,!parent_command!parent_command somethingsomething会触发父命令,!parent_command subcommand会触发子命令。

可以在 commands.group docs 中找到更多信息和可选的 kwargs。