python子命令齿轮文件

时间:2018-09-27 05:46:39

标签: python-3.x discord.py

您好,我的子命令在首页中一切正常。但是,如果我将命令移至齿轮文件子命令不起作用,则我添加了self, ctxself.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")

1 个答案:

答案 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")