如何解决discord.py-如何将命令添加到父命令
我想将命令作为参数分配给另一个命令。
我尝试过这样的事情:
@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.
所以,我的问题是我是否理解父参数错误?如果没有,如何将一个命令添加到另一个命令?
解决方法
没有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
将触发子命令。
更多信息和可选的kwarg可以在commands.group docs中找到。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。