Skip to content

Commit

Permalink
refact(help): better call to default help command
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRunner committed Nov 22, 2023
1 parent 31f3267 commit a78788a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 47 deletions.
45 changes: 1 addition & 44 deletions fcts/help_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
20 changes: 17 additions & 3 deletions libs/bot_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit a78788a

Please sign in to comment.