[英]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.