From 72cdd9b403b9cb173379af2a11ae15fda0d7f845 Mon Sep 17 00:00:00 2001 From: neoarz Date: Sun, 28 Sep 2025 22:53:25 -0400 Subject: [PATCH] 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. --- bot.py | 2 +- cogs/general/__init__.py | 59 ++++++++++++++++++++++++++++++++++++++ cogs/general/botinfo.py | 19 +++--------- cogs/general/feedback.py | 20 +++---------- cogs/general/ping.py | 20 +++---------- cogs/general/serverinfo.py | 20 ++++--------- cogs/general/uptime.py | 21 +++----------- cogs/{general => }/help.py | 0 8 files changed, 81 insertions(+), 80 deletions(-) create mode 100644 cogs/general/__init__.py rename cogs/{general => }/help.py (100%) diff --git a/bot.py b/bot.py index 9f1f3d5..fb9603e 100644 --- a/bot.py +++ b/bot.py @@ -90,7 +90,7 @@ class DiscordBot(commands.Bot): if os.path.exists(init_file): try: await self.load_extension(f"cogs.{folder}") - if folder != "fun": + if folder not in ["fun", "general"]: self.logger.info(f"Loaded extension '{folder}'") except Exception as e: exception = f"{type(e).__name__}: {e}" diff --git a/cogs/general/__init__.py b/cogs/general/__init__.py new file mode 100644 index 0000000..22716d0 --- /dev/null +++ b/cogs/general/__init__.py @@ -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'") diff --git a/cogs/general/botinfo.py b/cogs/general/botinfo.py index 4f49368..0cb2fd5 100644 --- a/cogs/general/botinfo.py +++ b/cogs/general/botinfo.py @@ -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)) \ No newline at end of file + + return botinfo \ No newline at end of file diff --git a/cogs/general/feedback.py b/cogs/general/feedback.py index 2a31971..98b6b7f 100644 --- a/cogs/general/feedback.py +++ b/cogs/general/feedback.py @@ -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)) \ No newline at end of file + + return feedback \ No newline at end of file diff --git a/cogs/general/ping.py b/cogs/general/ping.py index 0d97c7b..7d3ec13 100644 --- a/cogs/general/ping.py +++ b/cogs/general/ping.py @@ -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 diff --git a/cogs/general/serverinfo.py b/cogs/general/serverinfo.py index f0cf8a6..1d30497 100644 --- a/cogs/general/serverinfo.py +++ b/cogs/general/serverinfo.py @@ -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)) \ No newline at end of file + + return serverinfo \ No newline at end of file diff --git a/cogs/general/uptime.py b/cogs/general/uptime.py index 654bd3b..b3aee27 100644 --- a/cogs/general/uptime.py +++ b/cogs/general/uptime.py @@ -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 diff --git a/cogs/general/help.py b/cogs/help.py similarity index 100% rename from cogs/general/help.py rename to cogs/help.py