从另一个命令调用命令 discord.py

Calling a command from another command discord.py

我正在使用 discord.py 并想从另一个命令调用一个命令。 stack overflow 中有很多这样的问题,但我的不同之处在于我不希望将要调用的命令可供用户调用。

例如:

假设我有一个动物类别,并且在该类别中有两个命令,例如(笑话,图片)。那么如果命令前缀是 !.

用户将键入 !animals joke!animals pictures

这应该 return 想要的结果。

我认为我可以通过以下方式做到这一点:

animals.py:

@commands.command
async def animals(self, ctx, com_name):
    await ctx.invoke(self.bot.get_command(com_name))

jokes.py

@commands.command
async def joke(self, ctx):
   await ctx.send('a random joke')

现在,如果用户键入 !animals joke,它将起作用,但他们随后可以键入 !joke,这也起作用。如果类别也存在,我怎么能让命令被调用。

谢谢。

您可以创建一个动物 Group 然后让 joke 成为一个子命令:

@commands.group()
async def animals(self, ctx):
    pass

@animals.command()
async def joke(self, ctx):
   await ctx.send('a random joke')

另一种选择是在 joke 上添加一个 check,它始终为 false

fail = commands.check(lambda ctx: False)

@fail
@commands.command()
async def joke(self, ctx):
   await ctx.send('a random joke')