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.
This commit is contained in:
parent
542be94a0f
commit
c5588084ae
68
cogs/lockdown_cog.py
Normal file
68
cogs/lockdown_cog.py
Normal file
@ -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))
|
7
main.py
7
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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user