From a78788adcab855cb0d2880cc82aac6b332576271 Mon Sep 17 00:00:00 2001 From: ZRunner Date: Wed, 22 Nov 2023 12:54:09 -0500 Subject: [PATCH] refact(help): better call to default help command --- fcts/help_cmd.py | 45 +-------------------------------------------- libs/bot_classes.py | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 47 deletions(-) diff --git a/fcts/help_cmd.py b/fcts/help_cmd.py index 6f4263f7c..c17f92c61 100644 --- a/fcts/help_cmd.py +++ b/fcts/help_cmd.py @@ -60,10 +60,7 @@ async def help_cmd(self, ctx: MyContext, *args: str): pass except Exception as err: self.bot.dispatch("error", err, ctx) - if len(args) == 0: - await self._default_help_command(ctx) - else: - await self._default_help_command(ctx, args) + await ctx.send_super_help(" ".join(args) if args else None) async def help_command(self, ctx: MyContext, command_arg: list[str]): """Main command for the creation of the help message @@ -108,46 +105,6 @@ async def _detect_category_from_args(self, ctx: MyContext, args: list[str]) -> O return category_id return None - async def _default_help_command(self, ctx: MyContext, command: str = None): - default_help = commands.DefaultHelpCommand() - default_help.context = ctx - default_help._command_impl = self.help_cmd - # General help - if command is None: - mapping = default_help.get_bot_mapping() - return await default_help.send_bot_help(mapping) - # Check if it's a cog - cog = self.bot.get_cog(" ".join(command)) - if cog is not None: - return await default_help.send_cog_help(cog) - # If it's not a cog then it's a command. - # Since we want to have detailed errors when someone - # passes an invalid subcommand, we need to walk through - # the command group chain ourselves. - maybe_coro = discord.utils.maybe_coroutine - keys = command - cmd = self.bot.all_commands.get(keys[0]) - if cmd is None: - string = await maybe_coro(default_help.command_not_found, default_help.remove_mentions(keys[0])) - return await default_help.send_error_message(string) - - for key in keys[1:]: - try: - found = cmd.all_commands.get(key) - except AttributeError: - string = await maybe_coro(default_help.subcommand_not_found, cmd, default_help.remove_mentions(key)) - return await default_help.send_error_message(string) - else: - if found is None: - string = await maybe_coro(default_help.subcommand_not_found, cmd, default_help.remove_mentions(key)) - return await default_help.send_error_message(string) - cmd = found - - if isinstance(cmd, commands.Group): - return await default_help.send_group_help(cmd) - else: - return await default_help.send_command_help(cmd) - async def setup(bot): await bot.add_cog(Help(bot)) diff --git a/libs/bot_classes.py b/libs/bot_classes.py index 57d32a953..64e7256d8 100644 --- a/libs/bot_classes.py +++ b/libs/bot_classes.py @@ -4,7 +4,7 @@ import time from io import BytesIO from json import dumps -from typing import (TYPE_CHECKING, Awaitable, Callable, Literal, Optional, +from typing import (TYPE_CHECKING, Any, Awaitable, Callable, Literal, Optional, Union, overload) import aiohttp @@ -88,11 +88,25 @@ async def send(self, *args, json: Union[dict, list, None]=None, **kwargs) -> Opt kwargs["file"] = file return await super().send(*args, **kwargs) - async def send_help(self, command: Union[str, commands.Command]): + async def send_help(self, *args: Any): """Send the help message of the given command""" - cmd_arg = command.split(' ') if isinstance(command, str) else command.qualified_name.split(' ') + # command: Union[str, commands.Command] + if len(args) == 1 and isinstance(args[0], commands.Command): + cmd_arg = args[0].qualified_name.split(' ') + elif len(args) == 1 and isinstance(args[0], str): + cmd_arg = args[0].split(' ') + elif all(isinstance(arg, str) for arg in args): + cmd_arg = args + else: + raise ValueError(args) await self.bot.get_command("help")(self, *cmd_arg) + async def send_super_help(self, entity: Union[str, commands.Command, commands.Cog, None]=None): + "Use the default help command" + if entity: + return await super().send_help(entity) + return await super().send_help() + # pylint: disable=too-many-instance-attributes class Axobot(commands.bot.AutoShardedBot): """Bot class, with everything needed to run it"""