Python 3.x discord.py-如何将命令添加到父命令
我想将命令作为参数分配给另一个命令 我试过这样的方法:Python 3.x discord.py-如何将命令添加到父命令,python-3.x,command,discord.py,Python 3.x,Command,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 c
@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")
@bot.group()
async def parent_command(ctx):
pass
现在当我写作的时候
!相关方
没有什么事情像预期的那样发生,但是如果我写
!家长子女1
或
!相关儿童2
什么也没发生
但如果我只写!儿童1或!child2,相应的消息由bot发送
内在的!help命令还显示child1和child2也未分配给相关人员:
所以,我的问题是,我是否理解父参数是错误的?如果没有,我如何将一个命令添加到另一个命令中?没有
父项
参数!parents
属性只是一个属性
,它返回该命令分配给的所有父级。这些东西称为命令组
,而不是父命令
,您应该像这样创建“父命令”:
@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")
@bot.group()
async def parent_command(ctx):
pass
通过给它指定bot.group()
decorator
之后,您可以使用@parent\u command.command()
而不是@bot\u 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
这样,!父命令
和!父命令something something
将触发父命令,并且!父命令子命令
将触发该子命令
更多信息和可选KWARG可在中找到