1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 22:23:13 +00:00
Lumi/modules/moderation/slowmode.py

94 lines
3.3 KiB
Python
Raw Normal View History

2024-08-29 08:48:14 +00:00
import contextlib
import discord
2024-08-29 09:40:35 +00:00
from discord import app_commands
2024-08-29 08:48:14 +00:00
from discord.ext import commands
from lib.const import CONST
from lib.exceptions import LumiException
from lib.format import format_duration_to_seconds
2024-08-29 08:48:14 +00:00
class Slowmode(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.command(
name="slowmode",
aliases=["sm"],
usage="slowmode <duration> <channel>",
)
@commands.has_permissions(manage_channels=True)
async def slowmode(
self,
ctx: commands.Context[commands.Bot],
2024-08-29 09:31:38 +00:00
arg1: str | None = None,
arg2: str | None = None,
2024-08-29 08:48:14 +00:00
) -> None:
2024-08-29 09:31:38 +00:00
channel, duration = None, None
2024-08-29 08:48:14 +00:00
for arg in (arg1, arg2):
2024-08-29 09:31:38 +00:00
if not channel and arg:
with contextlib.suppress(commands.BadArgument):
2024-08-29 08:48:14 +00:00
channel = await commands.TextChannelConverter().convert(ctx, arg)
2024-08-29 09:31:38 +00:00
continue
if arg:
with contextlib.suppress(LumiException):
duration = format_duration_to_seconds(arg)
2024-08-29 08:48:14 +00:00
if not channel:
await ctx.send(CONST.STRINGS["slowmode_channel_not_found"])
return
if duration is None:
2024-08-29 09:31:38 +00:00
current_slowmode = channel.slowmode_delay
await ctx.send(CONST.STRINGS["slowmode_current_value"].format(channel.mention, current_slowmode))
2024-08-29 08:48:14 +00:00
return
if duration < 0 or duration > 21600: # 21600 seconds = 6 hours (Discord's max slowmode)
2024-08-29 09:40:35 +00:00
await ctx.send(CONST.STRINGS["slowmode_invalid_duration"])
2024-08-29 08:48:14 +00:00
return
try:
await channel.edit(slowmode_delay=duration)
2024-08-29 09:31:38 +00:00
await ctx.send(CONST.STRINGS["slowmode_success"].format(duration, channel.mention))
except discord.Forbidden as error:
raise LumiException(CONST.STRINGS["slowmode_forbidden"]) from error
2024-08-29 08:48:14 +00:00
2024-08-29 09:40:35 +00:00
@app_commands.command(
name="slowmode",
description="Set or view the slowmode for a channel",
)
@app_commands.checks.has_permissions(manage_channels=True)
async def slowmode_slash(
self,
interaction: discord.Interaction,
channel: discord.TextChannel,
duration: str | None = None,
) -> None:
if duration is None:
current_slowmode = channel.slowmode_delay
await interaction.response.send_message(
CONST.STRINGS["slowmode_current_value"].format(channel.mention, current_slowmode),
)
return
try:
seconds = format_duration_to_seconds(duration)
except LumiException:
await interaction.response.send_message(CONST.STRINGS["slowmode_invalid_duration"], ephemeral=True)
return
if seconds < 0 or seconds > 21600: # 21600 seconds = 6 hours (Discord's max slowmode)
await interaction.response.send_message(CONST.STRINGS["slowmode_invalid_duration"], ephemeral=True)
return
try:
await channel.edit(slowmode_delay=seconds)
await interaction.response.send_message(CONST.STRINGS["slowmode_success"].format(seconds, channel.mention))
except discord.Forbidden:
await interaction.response.send_message(CONST.STRINGS["slowmode_forbidden"], ephemeral=True)
2024-08-29 08:48:14 +00:00
async def setup(bot: commands.Bot) -> None:
await bot.add_cog(Slowmode(bot))