我想将命令作为参数分配给另一个命令。
我试过这样的方法:
@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 child2,也不会发生任何事情。
但是,如果我只编写!child1或!child2,则相应的消息将由机器人发送。
内置的!help命令还显示,child1和child2也没有分配给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.
所以,我的问题是我理解了父参数错误吗?如果不是,如何将命令添加到另一个命令中?
发布于 2020-09-26 05:56:52
没有parent
参数!parents
属性只是一个attribute
,它返回该命令分配给的所有父级。这些东西称为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
将触发子命令。
更多信息&可选的kwargs可以在commands.group文档中找到。
https://stackoverflow.com/questions/64077917
复制相似问题