Discord.py 如果用户放置不存在的子命令,如何显示消息?

Discord.py 如果用户放置不存在的子命令,如何显示消息?,discord.py,Discord.py,所以我做了一个子命令,只发送一条消息回来。我发现,如果用户键入一个不存在的命令,它仍然会显示来自子命令的消息。这听起来令人困惑,但这里有一个例子 User: ;id Bot: This command help you find the name of anyone in the server! User: ;id Slayer Bot: Bob Miller 因此,在测试时,我发现用户是否发送了类似的东西;id jgfjkagbot仍然为发送原始消息;id即“此命令可帮助您查找服务器中任何

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

User: ;id
Bot: This command help you find the name of anyone in the server! 
User: ;id Slayer
Bot: Bob Miller
因此,在测试时,我发现用户是否发送了类似的东西;id jgfjkagbot仍然为发送原始消息;id“此命令可帮助您查找服务器中任何人的姓名!”。如果用户尝试使用不存在的子命令,我将如何使bot发送特定消息?代码如下:

@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")
首先检查:

@commands.group()
异步定义id(自身,ctx):
如果ctx.subcommand_passed为None:
等待ctx.send(“此命令帮助您查找服务器中任何人的姓名!”)
elif ctx.u子命令为“无”:
wait ctx.send(f“子命令“{ctx.Subcommand_passed}”不存在。”)
首先检查:

@commands.group()
异步定义id(自身,ctx):
如果ctx.subcommand_passed为None:
等待ctx.send(“此命令帮助您查找服务器中任何人的姓名!”)
elif ctx.u子命令为“无”:
wait ctx.send(f“子命令“{ctx.Subcommand_passed}”不存在。”)

这是我最喜欢的方法:

@commands.group(调用\u而不调用\u command=True)
异步def组(自身、ctx):
wait ctx.send_help(ctx.command)#调用组的help命令
@group.command()
异步def子命令(self,ctx):
等待ctx.send(“您传递了一个有效的子命令!”)
invoke_without_command参数设置为true时,仅当未传递(有效)子命令时才会调用该命令。这意味着,如果调用了group()函数,则可以安全地假定它们没有传递子命令(或传递了无效的子命令)

我也喜欢使用,因为它会自动列出子命令。您可以将其替换为以下内容:

wait ctx.send(“您没有传递有效的子命令!”)
[编辑] 仔细阅读您的问题可以发现,子命令已经有了一些功能。这使其更加困难,但这将起作用:

@commands.group(调用\u而不调用\u command=True,忽略\u extra=False)
异步def组(自身、ctx):
等待ctx.send(“这是一个组命令!”)
@组错误
组上的异步定义错误(self、ctx、error):
如果isinstance(错误,commands.TooManyArguments):
等待ctx.send(“您传递了一个无效的子命令!”)
@group.command()
异步def子命令(self,ctx):
等待ctx.send(“您传递了一个有效的子命令!”)
下面是它的样子:

!group
Bot: This is a group command!
!group bla
Bot: You passed an invalid subcommand!
!group subcommand
Bot: You passed a VALID subcommand!

注意:ignore_额外字段引发commands.TooManyArguments错误,然后调用错误处理程序,允许bot发送错误消息。不幸的是,仍然会调用默认的on_命令\u错误挂钩。我建议忽略主错误处理程序中的
命令。TooManyArguments
错误来修复此问题。

这是我最喜欢的方法:

@commands.group(调用\u而不调用\u command=True)
异步def组(自身、ctx):
wait ctx.send_help(ctx.command)#调用组的help命令
@group.command()
异步def子命令(self,ctx):
等待ctx.send(“您传递了一个有效的子命令!”)
invoke_without_command参数设置为true时,仅当未传递(有效)子命令时才会调用该命令。这意味着,如果调用了group()函数,则可以安全地假定它们没有传递子命令(或传递了无效的子命令)

我也喜欢使用,因为它会自动列出子命令。您可以将其替换为以下内容:

wait ctx.send(“您没有传递有效的子命令!”)
[编辑] 仔细阅读您的问题可以发现,子命令已经有了一些功能。这使其更加困难,但这将起作用:

@commands.group(调用\u而不调用\u command=True,忽略\u extra=False)
异步def组(自身、ctx):
等待ctx.send(“这是一个组命令!”)
@组错误
组上的异步定义错误(self、ctx、error):
如果isinstance(错误,commands.TooManyArguments):
等待ctx.send(“您传递了一个无效的子命令!”)
@group.command()
异步def子命令(self,ctx):
等待ctx.send(“您传递了一个有效的子命令!”)
下面是它的样子:

!group
Bot: This is a group command!
!group bla
Bot: You passed an invalid subcommand!
!group subcommand
Bot: You passed a VALID subcommand!
注意:ignore_额外字段引发commands.TooManyArguments错误,然后调用错误处理程序,允许bot发送错误消息。不幸的是,仍然会调用默认的on_命令\u错误挂钩。我建议忽略主错误处理程序中的
命令.TooManyArguments
错误来修复此问题