Skip to content

Commit

Permalink
help command rework and invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
pieckenst committed Aug 26, 2024
1 parent 84c51cf commit 7c020fb
Show file tree
Hide file tree
Showing 3 changed files with 473 additions and 533 deletions.
151 changes: 97 additions & 54 deletions cogs/help.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
"""
The Help Menu Cog provides a help system for the Discord bot, allowing users to access information about the bot's commands and features.
The `Dropdown` class is a custom Discord UI Select component that allows users to select a category of commands to view help for. The `DropdownView` class is a custom Discord UI View that contains the `Dropdown` component.
The `PaginationView` class is a custom Discord UI View that allows users to navigate through multiple pages of help information, with a dropdown to select a category and buttons to move to the previous or next page.
The `Help` class is a custom Discord help command that provides the main functionality of the help system, including sending the initial help menu, sending help information for individual commands, and sending help information for specific cogs (command categories).
The `HelpEmbed` class is a custom Discord Embed class that is used to format the help information in an embedded message.
The `get_help` function is a helper function that is used to generate the help information for a specific cog.
"""

import discord
from discord.ext import commands
from discord import ButtonStyle, SelectOption
Expand All @@ -11,29 +25,21 @@ def __init__(self, options, bot):

async def callback(self, interaction: discord.Interaction):
label = self.values[0]
if label == "Close":
await interaction.response.edit_message(embed=discord.Embed(title="Help Closed"), view=None)
return

for cog in self.bot.cogs:
if label == cog:
await get_help(self, interaction, CogToPassAlong=cog)
return
if label == "Close":
embede = discord.Embed(
title=":books: Help System",
description=f"Welcome To {self.bot.user.name} Help System",
)
embede.set_footer(text="Use dropdown to select category")
await interaction.response.edit_message(embed=embede, view=None)


class DropdownView(discord.ui.View):
def __init__(self, bot):
super().__init__()
self.bot = bot
options = [
SelectOption(label=cog, value=cog)
for cog in bot.cogs
if cog.lower()
not in ["testingcog", "preferences", "calculator", "help", "workers", "jishaku", "listeners", "utils"]
]
options = [SelectOption(label=cog, value=cog) for cog in bot.cogs]
options.append(SelectOption(label="Close", value="Close"))
self.add_item(Dropdown(options, self.bot))

Expand All @@ -44,27 +50,14 @@ def __init__(self, embeds, bot):
self.embeds = embeds
self.current_page = 0
self.bot = bot

self.add_item(
Dropdown(
[
SelectOption(label=cog, value=cog)
for cog in bot.cogs
if cog.lower()
not in [
"testingcog",
"preferences",
"calculator",
"help",
"workers",
"jishaku",
"listeners",
"utils",
]
]
+ [SelectOption(label="Close", value="Close")],
[SelectOption(label=cog, value=cog) for cog in bot.cogs] + [SelectOption(label="Close", value="Close")],
self.bot,
)
)

self.add_item(Button(style=ButtonStyle.primary, label="◀", custom_id="previous"))
self.add_item(Button(style=ButtonStyle.primary, label="▶", custom_id="next"))

Expand All @@ -78,43 +71,60 @@ async def interaction_check(self, interaction: discord.Interaction) -> bool:
return True


class Help(commands.HelpCommand):
class Help(commands.Cog):
"The Help Menu Cog"

def __init__(self, bot):
super().__init__()
self.bot = bot
self.bot.help_command = MyHelp()


class MyHelp(commands.HelpCommand):
"The Help Menu Cog"

def __init__(self):
super().__init__(command_attrs={"help": "The help command for the bot"})

async def send(self, **kwargs):
"""a short cut to sending to get_destination"""
await self.get_destination().send(**kwargs)

async def send_bot_help(self, mapping):
embede = discord.Embed(
embed = discord.Embed(
title=":books: Help System",
description=f"Welcome To {self.bot.user.name} Help System",
description=f"Welcome To {self.context.bot.user.name} Help System",
)
embede.set_footer(text="Use dropdown to select category")
view = DropdownView(self.bot)
await self.context.send(embed=embede, view=view)
embed.set_footer(text="Use dropdown to select category")
view = DropdownView(self.context.bot)
await self.send(embed=embed, view=view)

async def send_command_help(self, command):
signature = f"/{command.name}"
if isinstance(command, discord.app_commands.Command):
signature += f" {' '.join([f'<{param.name}>' for param in command.parameters])}"
embed = HelpEmbed(title=signature, description=command.description or "No help found...")
try:
signature = f"/{command.name}"
if isinstance(command, discord.app_commands.Command):
signature += f" {' '.join([f'<{param.name}>' for param in command.parameters])}"

if cog := command.cog:
embed.add_field(name="Category", value=cog.qualified_name)
embed = HelpEmbed(title=signature, description=command.description or "No help found...")
if cog := command.cog:
embed.add_field(name="Category", value=cog.qualified_name)

embed.add_field(name="Usable", value="Yes")
embed.add_field(name="Usable", value="Yes")

if command._buckets and (cooldown := command._buckets._cooldown):
embed.add_field(
name="Cooldown",
value=f"{cooldown.rate} per {cooldown.per:.0f} seconds",
)
if command._buckets and (cooldown := command._buckets._cooldown):
embed.add_field(
name="Cooldown",
value=f"{cooldown.rate} per {cooldown.per:.0f} seconds",
)

await self.context.send(embed=embed)
await self.send(embed=embed)
except Exception as e:
await self.send(f"An error occurred while processing the command help: {e}")

async def send_cog_help(self, cog):
await get_help(self, self.context, cog.qualified_name)
try:
await get_help(self, self.context, cog.qualified_name)
except Exception as e:
await self.send(f"An error occurred while processing the cog help: {e}")


class HelpEmbed(discord.Embed):
Expand All @@ -128,6 +138,7 @@ async def get_help(self, interaction, CogToPassAlong):
cog = self.bot.get_cog(CogToPassAlong)
if not cog:
return

embeds = []
embed = discord.Embed(
title=f"{CogToPassAlong} - Commands",
Expand All @@ -136,8 +147,25 @@ async def get_help(self, interaction, CogToPassAlong):
embed.set_author(name="Help System")
commands_text = ""
for command in cog.get_commands():
if isinstance(command, discord.app_commands.Command):
command_text = f"『`/{command.name}`』: {command.description}\n"
if isinstance(command, (discord.app_commands.Command, commands.Command)):
command_text = (
f"『`/{command.name}`』: {command.description or command.help or 'No description available'}\n"
)
if len(commands_text) + len(command_text) > 1024:
embed.add_field(name="Commands", value=commands_text, inline=False)
embeds.append(embed)
embed = discord.Embed(
title=f"{CogToPassAlong} - Commands (Continued)",
description=cog.__doc__,
)
embed.set_author(name="Help System")
commands_text = command_text
else:
commands_text += command_text
elif isinstance(command, (discord.app_commands.Group, commands.Group)):
command_text = (
f"『`/{command.name}`』: {command.description or command.help or 'No description available'}\n"
)
if len(commands_text) + len(command_text) > 1024:
embed.add_field(name="Commands", value=commands_text, inline=False)
embeds.append(embed)
Expand All @@ -149,8 +177,23 @@ async def get_help(self, interaction, CogToPassAlong):
commands_text = command_text
else:
commands_text += command_text
for subcommand in command.commands:
subcommand_text = f" 『`/{command.name} {subcommand.name}`』: {subcommand.description or subcommand.help or 'No description available'}\n"
if len(commands_text) + len(subcommand_text) > 1024:
embed.add_field(name="Commands", value=commands_text, inline=False)
embeds.append(embed)
embed = discord.Embed(
title=f"{CogToPassAlong} - Commands (Continued)",
description=cog.__doc__,
)
embed.set_author(name="Help System")
commands_text = subcommand_text
else:
commands_text += subcommand_text

if commands_text:
embed.add_field(name="Commands", value=commands_text, inline=False)

embeds.append(embed)

if len(embeds) > 1:
Expand All @@ -160,5 +203,5 @@ async def get_help(self, interaction, CogToPassAlong):
await interaction.response.edit_message(embed=embeds[0])


def setup(bot):
bot.add_cog(Help(bot))
async def setup(bot):
await bot.add_cog(Help(bot))
Loading

0 comments on commit 7c020fb

Please sign in to comment.