Skip to content

Commit

Permalink
Add draft version of a custom help command class and make the bot use it
Browse files Browse the repository at this point in the history
  • Loading branch information
J3y0 committed Jul 30, 2023
1 parent 715ac03 commit 85b0dd9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
65 changes: 65 additions & 0 deletions src/bot/custom_help_command.py
Original file line number Diff line number Diff line change
@@ -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 <prefix>help
HelpCommand.send_command_help(command) that gets called with <prefix>help <command>
HelpCommand.send_group_help(group) that gets called with <prefix>help <group>
HelpCommand.send_cog_help(cog) that gets called with <prefix>help <cog>
"""

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)
11 changes: 8 additions & 3 deletions src/bot/root_pythia_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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__)
Expand Down

0 comments on commit 85b0dd9

Please sign in to comment.