Files
Syntrel/cogs/help.py

262 lines
9.8 KiB
Python
Raw Normal View History

2025-09-14 18:54:56 -04:00
import discord
2025-09-14 19:39:54 -04:00
from discord import app_commands
2025-09-14 18:54:56 -04:00
from discord.ext import commands
from discord.ext.commands import Context
class Help(commands.Cog, name="help"):
def __init__(self, bot) -> None:
self.bot = bot
async def category_autocomplete(
self,
interaction: discord.Interaction,
current: str,
) -> list[app_commands.Choice[str]]:
2025-11-02 23:32:52 -05:00
categories = [
"general",
"fun",
"moderation",
"owner",
"sidestore",
"idevice",
"melonx",
2025-11-22 14:18:35 -05:00
"livecontainer",
2025-11-02 23:32:52 -05:00
"media",
"miscellaneous",
"utilities",
"events",
]
suggestions = []
for category in categories:
if current.lower() in category.lower():
suggestions.append(
app_commands.Choice(
2025-11-02 23:32:52 -05:00
name=f"{category.capitalize()} Commands", value=category
)
)
2025-11-02 23:32:52 -05:00
return suggestions[:25]
2025-09-14 18:54:56 -04:00
@commands.hybrid_command(
name="help", description="List all commands the bot has loaded."
)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
@app_commands.allowed_installs(guilds=True, users=True)
2025-09-14 19:39:54 -04:00
@app_commands.describe(category="Choose a specific category to view its commands")
@app_commands.autocomplete(category=category_autocomplete)
2025-09-14 19:39:54 -04:00
async def help(self, context: Context, category: str = None) -> None:
2025-09-14 18:54:56 -04:00
category_mapping = {
"general": "general",
"fun": "fun",
"idevice": "idevice",
"melonx": "melonx",
2025-11-22 14:18:35 -05:00
"livecontainer": "livecontainer",
"media": "media",
2025-09-29 21:17:04 -04:00
"misc": "miscellaneous",
"miscellaneous": "miscellaneous",
"moderation": "moderation",
"sidestore": "sidestore",
2025-09-29 21:17:04 -04:00
"utils": "utilities",
"utilities": "utilities",
2025-11-02 18:57:40 -05:00
"events": "events",
2025-09-14 19:39:54 -04:00
"sync": "owner",
"logs": "owner",
"invite": "owner",
"load": "owner",
"unload": "owner",
"reload": "owner",
2025-09-14 19:39:54 -04:00
"shutdown": "owner",
"say": "owner",
}
2025-11-02 23:32:52 -05:00
2025-09-14 19:39:54 -04:00
category_descriptions = {
"general": "General commands",
2025-11-02 23:32:52 -05:00
"fun": "Fun commands",
2025-09-14 19:39:54 -04:00
"moderation": "Administration commands",
"owner": "Owner commands",
"sidestore": "SideStore troubleshooting commands",
"idevice": "idevice troubleshooting commands",
"melonx": "MeloNX troubleshooting commands",
2025-11-22 14:18:35 -05:00
"livecontainer": "LiveContainer troubleshooting commands",
"media": "Media commands",
"utilities": "Utility commands",
2025-11-02 18:57:40 -05:00
"miscellaneous": "Miscellaneous commands",
2025-11-02 23:32:52 -05:00
"events": "Events commands",
2025-09-14 18:54:56 -04:00
}
2025-11-02 23:32:52 -05:00
2025-09-14 19:39:54 -04:00
if category is None:
2025-11-02 23:32:52 -05:00
embed = discord.Embed(title="Help", color=0x7289DA)
embed.set_author(
name="Help", icon_url="https://yes.nighty.works/raw/T9mnBO.png"
2025-09-14 19:39:54 -04:00
)
2025-11-02 23:32:52 -05:00
standalone_commands = []
botinfo_cmd = self.bot.tree.get_command("botinfo")
if botinfo_cmd:
2025-11-02 23:32:52 -05:00
standalone_commands.append(
"**/botinfo** » Get information about this bot"
)
if standalone_commands:
embed.add_field(
2025-11-02 23:32:52 -05:00
name="", value="".join(standalone_commands) + "\n", inline=False
)
2025-11-02 23:32:52 -05:00
2025-09-14 19:39:54 -04:00
available_categories = set()
for cog_name in self.bot.cogs:
mapped_category = category_mapping.get(cog_name.lower())
if mapped_category:
available_categories.add(mapped_category)
2025-11-02 23:32:52 -05:00
2025-09-14 19:39:54 -04:00
category_list = []
for cat in sorted(available_categories):
2025-11-02 23:32:52 -05:00
description = category_descriptions.get(
cat, f"{cat.capitalize()} commands"
)
2025-09-14 19:39:54 -04:00
category_list.append(f"**/help {cat}** » {description}")
2025-11-02 23:32:52 -05:00
2025-09-14 19:39:54 -04:00
if category_list:
2025-11-02 23:32:52 -05:00
embed.add_field(name="", value="\n".join(category_list), inline=False)
2025-09-14 21:10:20 -04:00
if context.interaction:
2025-11-02 23:32:52 -05:00
await context.interaction.response.send_message(
embed=embed, ephemeral=True
)
2025-09-14 21:10:20 -04:00
else:
try:
await context.send(embed=embed)
except (discord.Forbidden, discord.HTTPException):
try:
await context.author.send(embed=embed)
except discord.Forbidden:
pass # User has DMs disabled
2025-09-14 19:39:54 -04:00
return
2025-11-02 23:32:52 -05:00
2025-09-14 19:39:54 -04:00
category = category.lower()
if category not in category_descriptions:
embed = discord.Embed(
title="Error",
description=f"Category '{category}' not found. Use `/help` to see available categories.",
2025-11-02 23:32:52 -05:00
color=0x7289DA,
2025-09-14 19:39:54 -04:00
)
2025-09-14 21:10:20 -04:00
if context.interaction:
2025-11-02 23:32:52 -05:00
await context.interaction.response.send_message(
embed=embed, ephemeral=True
)
2025-09-14 21:10:20 -04:00
else:
try:
await context.send(embed=embed)
except (discord.Forbidden, discord.HTTPException):
try:
await context.author.send(embed=embed)
except discord.Forbidden:
2025-11-02 23:32:52 -05:00
pass
2025-09-14 19:39:54 -04:00
return
2025-11-02 23:32:52 -05:00
2025-09-14 19:39:54 -04:00
commands_in_category = []
seen_names = set()
2025-09-14 18:54:56 -04:00
for cog_name in self.bot.cogs:
2025-09-14 19:39:54 -04:00
if category_mapping.get(cog_name.lower()) == category:
cog = self.bot.get_cog(cog_name)
if cog:
commands_list = cog.get_commands()
for command in commands_list:
2025-11-02 23:32:52 -05:00
has_prefix_subcommands = (
hasattr(command, "commands")
and len(getattr(command, "commands", [])) > 0
)
2025-10-01 09:25:41 -04:00
if has_prefix_subcommands:
continue
name = command.name
if name in seen_names:
continue
2025-11-02 23:32:52 -05:00
description = (
command.description.partition("\n")[0]
if command.description
else "No description available"
)
commands_in_category.append((name, description))
seen_names.add(name)
for app_command in self.bot.tree.get_commands():
bound_cog = getattr(app_command, "binding", None)
if bound_cog is None:
continue
bound_cog_name = getattr(bound_cog, "qualified_name", "").lower()
if category_mapping.get(bound_cog_name) != category:
continue
2025-11-02 23:32:52 -05:00
has_subcommands = (
hasattr(app_command, "commands")
and len(getattr(app_command, "commands", [])) > 0
)
2025-10-01 09:25:41 -04:00
if has_subcommands and category not in ["owner"]:
for subcommand in app_command.commands:
if subcommand.name in seen_names:
continue
2025-11-02 23:32:52 -05:00
sub_desc = (
subcommand.description.partition("\n")[0]
if getattr(subcommand, "description", None)
else "No description available"
)
commands_in_category.append(
(f"{app_command.name} {subcommand.name}", sub_desc)
)
seen_names.add(f"{app_command.name} {subcommand.name}")
2025-10-01 09:25:41 -04:00
else:
if app_command.name in seen_names:
continue
2025-11-02 23:32:52 -05:00
description = (
app_command.description.partition("\n")[0]
if getattr(app_command, "description", None)
else "No description available"
)
2025-10-01 09:25:41 -04:00
commands_in_category.append((app_command.name, description))
seen_names.add(app_command.name)
2025-11-02 23:32:52 -05:00
2025-09-14 19:39:54 -04:00
if not commands_in_category:
embed = discord.Embed(
title="Error",
description=f"No commands found in category '{category}'.",
2025-11-02 23:32:52 -05:00
color=0x7289DA,
2025-09-14 19:39:54 -04:00
)
2025-09-14 21:10:20 -04:00
if context.interaction:
2025-11-02 23:32:52 -05:00
await context.interaction.response.send_message(
embed=embed, ephemeral=True
)
2025-09-14 21:10:20 -04:00
else:
try:
await context.send(embed=embed)
except (discord.Forbidden, discord.HTTPException):
try:
await context.author.send(embed=embed)
except discord.Forbidden:
2025-11-02 23:32:52 -05:00
pass
2025-09-14 19:39:54 -04:00
return
2025-11-02 23:32:52 -05:00
embed = discord.Embed(title=f"/help » {category.lower()}", color=0x7289DA)
embed.set_author(
name="Help", icon_url="https://yes.nighty.works/raw/T9mnBO.png"
2025-09-14 19:39:54 -04:00
)
data = []
for command_name, description in sorted(commands_in_category):
data.append(f"**/{command_name}** » {description}")
2025-09-14 19:39:54 -04:00
help_text = "\n".join(data)
2025-11-02 23:32:52 -05:00
embed.add_field(name="", value=help_text, inline=False)
2025-09-14 21:10:20 -04:00
if context.interaction:
await context.interaction.response.send_message(embed=embed, ephemeral=True)
else:
try:
await context.send(embed=embed)
except (discord.Forbidden, discord.HTTPException):
try:
await context.author.send(embed=embed)
except discord.Forbidden:
2025-11-02 23:32:52 -05:00
pass
2025-09-14 18:54:56 -04:00
async def setup(bot) -> None:
2025-09-29 21:17:04 -04:00
await bot.add_cog(Help(bot))