如果用户输入不存在的子命令,我如何显示消息?

时间:2021-05-20 17:17:53

标签: discord.py

所以我做了一个子命令,它只是发回一条消息。我发现如果用户输入一个不存在的命令,它仍然会显示来自子命令的消息。这听起来令人困惑,但这里有一个例子。

User: ;id
Bot: This command help you find the name of anyone in the server! 
User: ;id Slayer
Bot: Bob Miller

因此,在测试时,我发现如果用户发送诸如 ;id jgfjkag 之类的东西,机器人仍会发送 ;id 的原始消息,即 "此命令帮助您找到服务器中任何人的姓名!”。如果用户尝试使用不存在的子命令,我将如何让机器人发送特定消息?代码如下:

@commands.group()
  async def id(self, ctx):
    if ctx.invoked_subcommand is None:
     await ctx.send("This command help you find the name of anyone in the server! ")

  @id.command()
  async def Slayer(self, ctx):
    await ctx.send("Bob Miller")
  
  @id.command()
  async def Skel(self, ctx):
    await ctx.send("John Dove")

1 个答案:

答案 0 :(得分:2)

先检查 ctx.subcommand_passed

_type