From 85b0dd9837d466eb44793019df5eeac5f5615bef Mon Sep 17 00:00:00 2001 From: Jey0 Date: Fri, 28 Jul 2023 21:20:42 +0200 Subject: [PATCH] Add draft version of a custom help command class and make the bot use it --- src/bot/custom_help_command.py | 65 ++++++++++++++++++++++++++++++++++ src/bot/root_pythia_bot.py | 11 ++++-- 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 src/bot/custom_help_command.py diff --git a/src/bot/custom_help_command.py b/src/bot/custom_help_command.py new file mode 100644 index 0000000..b917af8 --- /dev/null +++ b/src/bot/custom_help_command.py @@ -0,0 +1,65 @@ +import discord +from discord.ext.commands import HelpCommand + + +class RootPythiaHelpCommand(HelpCommand): + """ + Implementation of HelpCommand + + HelpCommand.send_bot_help(mapping) that gets called with help + HelpCommand.send_command_help(command) that gets called with help + HelpCommand.send_group_help(group) that gets called with help + HelpCommand.send_cog_help(cog) that gets called with help + """ + + def get_command_signature(self, command): + return '%s %s' % (command.qualified_name, command.signature) + + async def send_bot_help(self, mapping): + # color attribute sets color of the border on the left + embed = discord.Embed(title="Help", color=discord.Color.blurple()) + + for cog, cmds in mapping.items(): + # Commands a user can't use are filtered + filtered_cmds = await self.filter_commands(cmds, sort=True) + cmd_signatures = [self.get_command_signature(cmd) for cmd in filtered_cmds] + + if cmd_signatures: + cog_name = getattr(cog, "qualified_name", "No Category") + embed.add_field(name=cog_name, value="\n".join(cmd_signatures), inline=False) + + await self.get_destination().send(embed=embed) + + async def send_command_help(self, command): + embed = discord.Embed(title=self.get_command_signature(command), color=discord.Color.blurple()) + + if command.help: + embed.description = command.help + if alias := command.aliases: + embed.add_field(name="Aliases", value=", ".join(alias), inline=False) + + await self.get_destination().send(embed=embed) + + async def send_group_help(self, group): + embed = discord.Embed(title=self.get_command_signature(group), description=group.help, color=discord.Color.blurple()) + + if filtered_commands := await self.filter_commands(group.commands): + for command in filtered_commands: + embed.add_field(name=self.get_command_signature(command), value=command.help or "No Help Message Found... ") + + await self.get_destination().send(embed=embed) + + async def send_cog_help(self, cog): + embed = discord.Embed(title=cog.qualified_name or "No Category", description=cog.description, color=discord.Color.blurple()) + + if filtered_commands := await self.filter_commands(cog.get_commands()): + for command in filtered_commands: + embed.add_field(name=self.get_command_signature(command), value=command.help or "No Help Message Found... ", inline=False) + + await self.get_destination().send(embed=embed) + + async def send_error_message(self, error): + embed = discord.Embed(title="Error", description=error, color=discord.Color.red()) + channel = self.get_destination() + + await channel.send(embed=embed) diff --git a/src/bot/root_pythia_bot.py b/src/bot/root_pythia_bot.py index 58b8afe..937fe01 100644 --- a/src/bot/root_pythia_bot.py +++ b/src/bot/root_pythia_bot.py @@ -9,6 +9,7 @@ from api.rootme_api import RootMeAPIManager from api.rate_limiter import RateLimiter +from bot.custom_help_command import RootPythiaHelpCommand from bot.root_pythia_cogs import RootPythiaCommands from bot.dummy_db_manager import DummyDBManager @@ -34,19 +35,23 @@ def craft_intents(): # enable guild messages related events # More info: https://docs.pycord.dev/en/stable/api/data_classes.html#discord.Intents.guild_messages intents.guild_messages = True - + return intents ########### Create bot object ################# _DESCRIPTION = ( - "RootPythia is a Discord bot fetching RootMe API to notify everyone" + "RootPythia is a Discord bot fetching RootMe API to notify everyone " "when a user solves a new challenge!" ) _PREFIX = "!" _INTENTS = craft_intents() -BOT = commands.Bot(command_prefix=_PREFIX, description=_DESCRIPTION, intents=_INTENTS, help_command=commands.DefaultHelpCommand()) +BOT = commands.Bot( + command_prefix=_PREFIX, + description=_DESCRIPTION, + intents=_INTENTS, + help_command=RootPythiaHelpCommand()) # Create Bot own logger, each Cog will also have its own BOT.logger = logging.getLogger(__name__)