![](/img/trans.png)
[英]How to terminate an async function in discord.py from another command
[英]Calling a command from another command discord.py
我正在使用discord.py
并想从另一个命令调用命令。 堆栈溢出中有很多类似的问题,但我的不同之处在于我不希望将要调用的命令可供用户调用。
例如:
假设我有一个动物类别,并且在该类别中有两个命令,例如(笑话、图片)。 那么如果命令前缀是!
.
用户将键入!animals joke
或!animals pictures
。
这应该返回所需的结果。
我想我可以通过以下方式做到这一点:
动物.py:
@commands.command
async def animals(self, ctx, com_name):
await ctx.invoke(self.bot.get_command(com_name))
笑话.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
检查
fail = commands.check(lambda ctx: False)
@fail
@commands.command()
async def joke(self, ctx):
await ctx.send('a random joke')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.