refactor(general): commands into GroupCog

Migrated general commands (ping, uptime, botinfo, serverinfo, feedback) into a single GroupCog in cogs/general/__init__.py for better organization and maintainability. Converted individual command files to export command functions instead of Cogs. Updated bot.py to load the new general extension. Renamed help.py for consistency.
This commit is contained in:
neoarz
2025-09-28 22:53:25 -04:00
parent e7ee97074b
commit 72cdd9b403
8 changed files with 81 additions and 80 deletions

59
cogs/general/__init__.py Normal file
View File

@@ -0,0 +1,59 @@
import discord
from discord.ext import commands
from discord.ext.commands import Context
from .ping import ping_command
from .uptime import uptime_command
from .botinfo import botinfo_command
from .serverinfo import serverinfo_command
from .feedback import feedback_command
class General(commands.GroupCog, name="general"):
def __init__(self, bot) -> None:
self.bot = bot
super().__init__()
@commands.hybrid_command(
name="ping",
description="Check if the bot is alive.",
)
async def ping(self, context):
return await ping_command()(self, context)
@commands.hybrid_command(
name="uptime",
description="Check how long the bot has been running.",
)
async def uptime(self, context):
return await uptime_command()(self, context)
@commands.hybrid_command(
name="botinfo",
description="Get some useful (or not) information about the bot.",
)
async def botinfo(self, context):
return await botinfo_command()(self, context)
@commands.hybrid_command(
name="serverinfo",
description="Get some useful (or not) information about the server.",
)
async def serverinfo(self, context):
return await serverinfo_command()(self, context)
@commands.hybrid_command(
name="feedback",
description="Submit a feedback for the owners of the bot"
)
async def feedback(self, context):
return await feedback_command()(self, context)
async def setup(bot) -> None:
cog = General(bot)
await bot.add_cog(cog)
bot.logger.info("Loaded extension 'general.ping'")
bot.logger.info("Loaded extension 'general.uptime'")
bot.logger.info("Loaded extension 'general.botinfo'")
bot.logger.info("Loaded extension 'general.serverinfo'")
bot.logger.info("Loaded extension 'general.feedback'")

View File

@@ -1,23 +1,13 @@
import platform
import discord
from discord.ext import commands
from discord.ext.commands import Context
class BotInfo(commands.Cog, name="botinfo"):
def __init__(self, bot) -> None:
self.bot = bot
def botinfo_command():
@commands.hybrid_command(
name="botinfo",
description="Get some useful (or not) information about the bot.",
)
async def botinfo(self, context: Context) -> None:
"""
Get some useful (or not) information about the bot.
:param context: The hybrid command context.
"""
async def botinfo(self, context):
embed = discord.Embed(
title="Syntrel Discord Bot",
color=0x7289DA,
@@ -36,6 +26,5 @@ class BotInfo(commands.Cog, name="botinfo"):
await context.interaction.response.send_message(embed=embed, ephemeral=True)
else:
await context.send(embed=embed)
async def setup(bot) -> None:
await bot.add_cog(BotInfo(bot))
return botinfo

View File

@@ -2,7 +2,6 @@ import discord
from discord import app_commands
from discord.ext import commands
class FeedbackForm(discord.ui.Modal, title="Feeedback"):
feedback = discord.ui.TextInput(
label="What do you think about this bot?",
@@ -17,20 +16,11 @@ class FeedbackForm(discord.ui.Modal, title="Feeedback"):
self.answer = str(self.feedback)
self.stop()
class Feedback(commands.Cog, name="feedback"):
def __init__(self, bot) -> None:
self.bot = bot
def feedback_command():
@app_commands.command(
name="feedback", description="Submit a feedback for the owners of the bot"
)
async def feedback(self, interaction: discord.Interaction) -> None:
"""
Submit a feedback for the owners of the bot.
:param interaction: The application command interaction.
"""
async def feedback(self, interaction: discord.Interaction):
feedback_form = FeedbackForm()
await interaction.response.send_modal(feedback_form)
@@ -53,7 +43,5 @@ class Feedback(commands.Cog, name="feedback"):
color=0x7289DA,
).set_author(name="Feedback System", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
)
async def setup(bot) -> None:
await bot.add_cog(Feedback(bot))
return feedback

View File

@@ -1,224 +0,0 @@
import discord
from discord import app_commands
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]]:
categories = ["general", "fun", "moderation", "owner", "sidestore", "idevice", "miscellaneous", "utilities"]
suggestions = []
for category in categories:
if current.lower() in category.lower():
suggestions.append(
app_commands.Choice(
name=f"{category.capitalize()} Commands",
value=category
)
)
return suggestions[:25]
@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)
@app_commands.describe(category="Choose a specific category to view its commands")
@app_commands.autocomplete(category=category_autocomplete)
async def help(self, context: Context, category: str = None) -> None:
category_mapping = {
# General Commands
"help": "general",
"botinfo": "general",
"serverinfo": "general",
"ping": "general",
"feedback": "general",
"uptime": "general",
# "context_menus": "general",
# Fun Commands
"fun": "fun",
# Moderation Commands
"kick": "moderation",
"ban": "moderation",
"nick": "moderation",
"purge": "moderation",
"hackban": "moderation",
"warnings": "moderation",
"archive": "moderation",
# SideStore Commands
"sidestore": "sidestore",
"refresh": "sidestore",
"code": "sidestore",
"crash": "sidestore",
"pairing": "sidestore",
"server": "sidestore",
"half": "sidestore",
"sparse": "sidestore",
"afc": "sidestore",
"udid": "sidestore",
# idevice Commands
"idevice": "idevice",
"noapps": "idevice",
"errorcodes": "idevice",
"developermode": "idevice",
"mountddi": "idevice",
# Owner Commands
"sync": "owner",
"cog_management": "owner",
"shutdown": "owner",
"say": "owner",
"invite": "owner",
"logs": "owner",
# Utilities Commands
"translate": "utilities",
# Miscellaneous Commands
"keanu": "miscellaneous",
"labubu": "miscellaneous",
"piracy": "miscellaneous",
"tryitandsee": "miscellaneous",
"rr": "miscellaneous",
}
category_descriptions = {
"general": "General commands",
"fun": "Fun commands",
"moderation": "Administration commands",
"owner": "Owner commands",
"sidestore": "SideStore troubleshooting commands",
"idevice": "idevice troubleshooting commands",
"utilities": "Utility commands",
"miscellaneous": "Miscellaneous commands"
}
if category is None:
embed = discord.Embed(
title="Help",
color=0x7289DA
)
embed.set_author(name="Help", icon_url="https://yes.nighty.works/raw/T9mnBO.png")
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)
category_list = []
for cat in sorted(available_categories):
description = category_descriptions.get(cat, f"{cat.capitalize()} commands")
category_list.append(f"**/help {cat}** » {description}")
if category_list:
embed.add_field(
name="",
value="\n".join(category_list),
inline=False
)
if context.interaction:
await context.interaction.response.send_message(embed=embed, ephemeral=True)
else:
await context.author.send(embed=embed)
return
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.",
color=0x7289DA
)
if context.interaction:
await context.interaction.response.send_message(embed=embed, ephemeral=True)
else:
await context.author.send(embed=embed)
return
commands_in_category = []
seen_names = set()
for cog_name in self.bot.cogs:
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:
name = command.name
if name in seen_names:
continue
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
if app_command.name in seen_names:
continue
description = app_command.description.partition("\n")[0] if getattr(app_command, "description", None) else "No description available"
commands_in_category.append((app_command.name, description))
seen_names.add(app_command.name)
if hasattr(app_command, 'commands') and category == "fun":
for subcommand in app_command.commands:
if subcommand.name in seen_names:
continue
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}")
if not commands_in_category:
embed = discord.Embed(
title="Error",
description=f"No commands found in category '{category}'.",
color=0x7289DA
)
if context.interaction:
await context.interaction.response.send_message(embed=embed, ephemeral=True)
else:
await context.author.send(embed=embed)
return
embed = discord.Embed(
title=f"/help » {category.lower()}",
color=0x7289DA
)
embed.set_author(name="Help", icon_url="https://yes.nighty.works/raw/T9mnBO.png")
data = []
for command_name, description in sorted(commands_in_category):
data.append(f"**/{command_name}** » {description}")
help_text = "\n".join(data)
embed.add_field(
name="",
value=help_text,
inline=False
)
if context.interaction:
await context.interaction.response.send_message(embed=embed, ephemeral=True)
else:
await context.author.send(embed=embed)
async def setup(bot) -> None:
await bot.add_cog(Help(bot))

View File

@@ -1,22 +1,12 @@
import discord
from discord.ext import commands
from discord.ext.commands import Context
class Ping(commands.Cog, name="ping"):
def __init__(self, bot) -> None:
self.bot = bot
def ping_command():
@commands.hybrid_command(
name="ping",
description="Check if the bot is alive.",
)
async def ping(self, context: Context) -> None:
"""
Check if the bot is alive.
:param context: The hybrid command context.
"""
async def ping(self, context):
embed = discord.Embed(
title="🏓 Pong!",
description=f"The bot latency is {round(self.bot.latency * 1000)}ms.",
@@ -31,7 +21,5 @@ class Ping(commands.Cog, name="ping"):
await inter.followup.send(embed=embed, ephemeral=True)
else:
await context.send(embed=embed)
async def setup(bot) -> None:
await bot.add_cog(Ping(bot))
return ping

View File

@@ -1,22 +1,13 @@
import discord
from discord.ext import commands
from discord.ext.commands import Context
class ServerInfo(commands.Cog, name="serverinfo"):
def __init__(self, bot) -> None:
self.bot = bot
def serverinfo_command():
@commands.hybrid_command(
name="serverinfo",
description="Get some useful (or not) information about the server.",
)
@commands.guild_only() # This decorator ensures the command only works in servers
async def serverinfo(self, context: Context) -> None:
"""
Get some useful (or not) information about the server.
:param context: The hybrid command context.
"""
# Additional check (though @commands.guild_only() should handle this)
@commands.guild_only()
async def serverinfo(self, context):
if context.guild is None:
await context.send("This command can only be used in a server, not in DMs!")
return
@@ -53,6 +44,5 @@ class ServerInfo(commands.Cog, name="serverinfo"):
await context.interaction.response.send_message(embed=embed, ephemeral=True)
else:
await context.send(embed=embed)
async def setup(bot) -> None:
await bot.add_cog(ServerInfo(bot))
return serverinfo

View File

@@ -1,7 +1,5 @@
import discord
from discord.ext import commands
from discord.ext.commands import Context
class UptimeView(discord.ui.View):
def __init__(self, bot):
@@ -18,21 +16,12 @@ class UptimeView(discord.ui.View):
embed.set_author(name="Uptime", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
await interaction.response.edit_message(embed=embed, view=self)
class Uptime(commands.Cog, name="uptime"):
def __init__(self, bot) -> None:
self.bot = bot
def uptime_command():
@commands.hybrid_command(
name="uptime",
description="Check how long the bot has been running.",
)
async def uptime(self, context: Context) -> None:
"""
Check how long the bot has been running.
:param context: The hybrid command context.
"""
async def uptime(self, context):
embed = discord.Embed(
title="Bot Uptime",
description=f"The bot has been running for **{self.bot.get_uptime()}**",
@@ -48,7 +37,5 @@ class Uptime(commands.Cog, name="uptime"):
await inter.followup.send(embed=embed, view=view, ephemeral=True)
else:
await context.send(embed=embed, view=view)
async def setup(bot) -> None:
await bot.add_cog(Uptime(bot))
return uptime