简体   繁体   English

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

[英]Calling a command from another command discord.py

I'm using discord.py and wanted to call a command from another command.我正在使用discord.py并想从另一个命令调用命令。 There are many questions like that in stack overflow but the difference in mine is that I don't want the command that is going to be called to be available for the user to call.堆栈溢出中有很多类似的问题,但我的不同之处在于我不希望将要调用的命令可供用户调用。

For Example:例如:

Let's say that I have an animals category and have two commands in that category such as (jokes, pictures) as an example.假设我有一个动物类别,并且在该类别中有两个命令,例如(笑话、图片)。 Then if the command prefix was !那么如果命令前缀是! . .

The user would type !animals joke or !animals pictures .用户将键入!animals joke!animals pictures

This should return the desired results.这应该返回所需的结果。

I thought I could do that by:我想我可以通过以下方式做到这一点:

animals.py:动物.py:

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

jokes.py笑话.py

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

Now if the user types !animals joke it will work but they would then be able to type !joke and that would also work.现在,如果用户输入!animals joke它将起作用,但他们将能够输入!joke并且这也将起作用。 How could I only let the command be called if the category is also present.如果类别也存在,我怎么能只调用命令。

Thanks.谢谢。

You can create an animals Group and then have joke be a subcommand:您可以创建一个动物Group ,然后将joke作为子命令:

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

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

Another option would be adding a check on joke that is always false另一种选择是添加一个总是错误的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM