From c5588084ae784de41c114f8fd2ca6932ad7c112f Mon Sep 17 00:00:00 2001 From: Slipstream Date: Tue, 27 May 2025 21:04:32 -0600 Subject: [PATCH] feat: Load lockdown cog and handle potential errors This commit adds the loading of the lockdown cog in main.py. It also includes error handling to catch and log any exceptions that may occur during the loading process, improving the bot's robustness. --- cogs/lockdown_cog.py | 68 ++++++++++++++++++++++++++++++++++++++++++++ main.py | 7 +++++ 2 files changed, 75 insertions(+) create mode 100644 cogs/lockdown_cog.py diff --git a/cogs/lockdown_cog.py b/cogs/lockdown_cog.py new file mode 100644 index 0000000..acb5c61 --- /dev/null +++ b/cogs/lockdown_cog.py @@ -0,0 +1,68 @@ +import discord +from discord.ext import commands +import asyncio + +class LockdownCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.command(name="channel_lockdown") + @commands.has_permissions(manage_channels=True) + async def channel_lockdown(self, ctx, channel: discord.TextChannel = None, time=None): + """Locks down a channel.""" + channel = channel or ctx.channel + overwrite = channel.overwrites_for(ctx.guild.default_role) + if overwrite.send_messages is False: + await ctx.send("Channel is already locked down.") + return + + overwrite.send_messages = False + await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite) + await ctx.send(f"Channel {channel.mention} locked down.") + + if time: + try: + time = int(time) + except ValueError: + await ctx.send("Time must be an integer (seconds).") + return + + await asyncio.sleep(time) + + overwrite.send_messages = None + await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite) + await ctx.send(f"Channel {channel.mention} lockdown lifted.") + + @commands.command(name="server_lockdown") + @commands.has_permissions(administrator=True) + async def server_lockdown(self, ctx, time=None): + """Locks down the entire server.""" + for channel in ctx.guild.text_channels: + overwrite = channel.overwrites_for(ctx.guild.default_role) + if overwrite.send_messages is False: + continue + + overwrite.send_messages = False + await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite) + + await ctx.send("Server locked down.") + + if time: + try: + time = int(time) + except ValueError: + await ctx.send("Time must be an integer (seconds).") + return + + await asyncio.sleep(time) + + for channel in ctx.guild.text_channels: + overwrite = channel.overwrites_for(ctx.guild.default_role) + overwrite.send_messages = None + await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite) + + await ctx.send("Server lockdown lifted.") + + +async def setup(bot): + await bot.add_cog(LockdownCog(bot)) diff --git a/main.py b/main.py index 6c8a38d..d3d951e 100644 --- a/main.py +++ b/main.py @@ -129,6 +129,13 @@ class MyBot(commands.Bot): await load_all_cogs(self, skip_cogs=self.ai_cogs_to_skip) log.info(f"Cogs loaded in setup_hook. Skipped: {self.ai_cogs_to_skip or 'None'}") + # Load the lockdown cog + try: + await self.load_extension("cogs.lockdown_cog") + log.info("Lockdown cog loaded successfully.") + except Exception as e: + log.exception(f"Failed to load lockdown cog: {e}") + # --- Share GurtCog, ModLogCog, and bot instance with the sync API --- try: gurt_cog = self.get_cog("Gurt")