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.
69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
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))
|