feat: Refactor lockdown commands to use app_commands and improve interaction handling

This commit is contained in:
Slipstream 2025-05-27 21:06:48 -06:00
parent c5588084ae
commit b803380573
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -1,68 +1,64 @@
import discord
from discord.ext import commands
from discord import app_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):
lockdown = app_commands.Group(name="lockdown", description="Lockdown commands")
@lockdown.command(name="channel")
@app_commands.describe(channel="The channel to lock down", time="Duration of lockdown in seconds")
@app_commands.checks.has_permissions(manage_channels=True)
async def channel_lockdown(self, interaction: discord.Interaction, channel: discord.TextChannel = None, time: int = None):
"""Locks down a channel."""
channel = channel or ctx.channel
overwrite = channel.overwrites_for(ctx.guild.default_role)
channel = channel or interaction.channel
overwrite = channel.overwrites_for(interaction.guild.default_role)
if overwrite.send_messages is False:
await ctx.send("Channel is already locked down.")
await interaction.response.send_message("Channel is already locked down.", ephemeral=True)
return
overwrite.send_messages = False
await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
await ctx.send(f"Channel {channel.mention} locked down.")
await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
await interaction.response.send_message(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.")
await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
await interaction.followup.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):
@lockdown.command(name="server")
@app_commands.describe(time="Duration of server lockdown in seconds")
@app_commands.checks.has_permissions(administrator=True)
async def server_lockdown(self, interaction: discord.Interaction, time: int = None):
"""Locks down the entire server."""
for channel in ctx.guild.text_channels:
overwrite = channel.overwrites_for(ctx.guild.default_role)
await interaction.response.defer() # Defer the response as this might take time
for channel in interaction.guild.text_channels:
overwrite = channel.overwrites_for(interaction.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 channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
await ctx.send("Server locked down.")
await interaction.followup.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)
for channel in interaction.guild.text_channels:
overwrite = channel.overwrites_for(interaction.guild.default_role)
overwrite.send_messages = None
await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
await ctx.send("Server lockdown lifted.")
await interaction.followup.send("Server lockdown lifted.")
async def setup(bot):
await bot.add_cog(LockdownCog(bot))
await bot.tree.sync()