From c90075741c57ae1aa6922a5f2b9920f3e01a4ec5 Mon Sep 17 00:00:00 2001 From: ZRunner Date: Wed, 22 Nov 2023 13:06:42 -0500 Subject: [PATCH] refact(help): unknown command error --- fcts/help_cmd.py | 42 ++++++++++++++++++++---------------------- lang/help/en.json | 2 +- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/fcts/help_cmd.py b/fcts/help_cmd.py index c17f92c61..34ebac702 100644 --- a/fcts/help_cmd.py +++ b/fcts/help_cmd.py @@ -69,30 +69,11 @@ async def help_command(self, ctx: MyContext, command_arg: list[str]): if category_id := await self._detect_category_from_args(ctx, command_arg): await help_category_command(self, ctx, category_id) return - # if user entered a root command/group name - if len(command_arg) == 1: - name = command_arg[0] - command = self.bot.all_commands.get(name) - if command is None: - await ctx.send(await self.bot._(ctx.channel, "help.cmd-not-found", cmd=name)) - return + # if user entered a command / subcommand name + if command := self.bot.get_command(" ".join(command_arg)): await help_text_cmd_command(self, ctx, command) return - # if user entered a subcommand name - name = command_arg[0] - command = self.bot.all_commands.get(name) - if command is None: - await ctx.send(await self.bot._(ctx.channel, "help.cmd-not-found", cmd=name)) - return - for key in command_arg[1:]: - if not isinstance(command, commands.Group): - await ctx.send(await self.bot._(ctx.channel, "help.no-subcmd", cmd=command.name)) - return - command = command.all_commands.get(key) - if command is None: - await ctx.send(await self.bot._(ctx.channel, "help.subcmd-not-found", name=key)) - return - await help_text_cmd_command(self, ctx, command) + await self._send_error_unknown_command(ctx, command_arg) async def _detect_category_from_args(self, ctx: MyContext, args: list[str]) -> Optional[str]: """Detect the category from the arguments passed to the help command""" @@ -105,6 +86,23 @@ async def _detect_category_from_args(self, ctx: MyContext, args: list[str]) -> O return category_id return None + async def _send_error_unknown_command(self, ctx: MyContext, args: list[str]): + """Send a meaningful error message if the (sub)command is not found""" + if len(args) == 0: + return # should not happen + if len(args) == 1: + await ctx.send(await self.bot._(ctx.channel, "help.cmd-not-found", cmd=args[0])) + return + parent, last_arg = args[:-1], args[-1] + if cmd := self.bot.get_command(" ".join(parent)): + if isinstance(cmd, commands.Group): + await ctx.send(await self.bot._(ctx.channel, "help.subcmd-not-found", name=last_arg)) + return + cmd_mention = await self.bot.get_command_mention(cmd.qualified_name) + await ctx.send(await self.bot._(ctx.channel, "help.no-subcmd", cmd=cmd_mention)) + return + await self._send_error_unknown_command(ctx, parent) + async def setup(bot): await bot.add_cog(Help(bot)) diff --git a/lang/help/en.json b/lang/help/en.json index 05c1952b8..bf4078a46 100644 --- a/lang/help/en.json +++ b/lang/help/en.json @@ -162,7 +162,7 @@ }, "no-desc-cmd": "No description for this command", "no-desc-cog": "No description for this cog.", - "no-subcmd": "The command `%{cmd}` has no subcommand", + "no-subcmd": "The command %{cmd} has no subcommand", "not-enabled": ":warning: This command is disabled", "subcmd-not-found": "This command has no subcommand named \"%{name}\"", "subcmds": "Subcommands",