您好,我的子命令在首页中一切正常。但是,如果我将命令移至齿轮文件子命令不起作用,则我添加了self, ctx
和self.bot
,但仍然无法理解我在哪里做错了。
@commands.group(pass_context=True)
async def first(self, ctx):
if ctx.invoked_subcommand is None:
await self.bot.say("Ping 1")
@first.group(pass_context=True)
async def second(self, ctx):
if ctx.invoked_subcommand is second:
await self.bot.say("Ping 2")
@second.command(pass_context=True)
async def third(self, ctx):
await self.bot.say("Ping 3")
答案 0 :(得分:1)
second
的正文时未定义 second
。此外,invoked_subcommand
在这里将始终为second
,即使您也调用third
。
您应该将invoke_without_command
属性传递给group
装饰器。
@commands.group(pass_context=True, invoke_without_command=True)
async def first(self, ctx):
await self.bot.say("Ping 1")
@first.group(pass_context=True, invoke_without_command=True)
async def second(self, ctx):
await self.bot.say("Ping 2")
@second.command(pass_context=True)
async def third(self, ctx):
await self.bot.say("Ping 3")
编辑:
在反思时,这可能是对它的过度考虑。您只需要通过该类的方法来解析second
:
class MyCog:
@commands.group(pass_context=True)
async def first(self, ctx):
if ctx.invoked_subcommand is None:
await self.bot.say("Ping 1")
@first.group(pass_context=True)
async def second(self, ctx):
if ctx.invoked_subcommand is MyCog.second: # Possibly self.second instead, but I'm not sure.
await self.bot.say("Ping 2")
@second.command(pass_context=True)
async def third(self, ctx):
await self.bot.say("Ping 3")