diff --git a/lib/format.py b/lib/format.py index c6b8d7e..c505c1b 100644 --- a/lib/format.py +++ b/lib/format.py @@ -109,8 +109,11 @@ def get_invoked_name(ctx: commands.Context[commands.Bot]) -> str | None: def format_duration_to_seconds(duration: str) -> int: """ - Formats a duration in seconds to a human-readable string. + Converts a duration string to seconds. If the input is just an integer, it returns that integer as seconds. """ + if duration.isdigit(): + return int(duration) + parsed_duration: int = parse(duration) # type: ignore if isinstance(parsed_duration, int): diff --git a/locales/strings.en-US.json b/locales/strings.en-US.json index 168a2c2..7d0c2c7 100644 --- a/locales/strings.en-US.json +++ b/locales/strings.en-US.json @@ -252,13 +252,11 @@ "ping_footer": "Latency: {0}ms", "ping_pong": "pong!", "ping_uptime": "I've been online since .", - "slowmode_author": "Slowmode", "slowmode_channel_not_found": "Channel not found.", - "slowmode_duration_not_found": "Please provide a duration in seconds for the slowmode.", - "slowmode_footer": "Use 0 to disable slowmode", + "slowmode_current_value": "The current slowmode for {0} is **{1}s**.", "slowmode_forbidden": "I don't have permission to change the slowmode in that channel.", "slowmode_invalid_duration": "Slowmode duration must be between 0 and 21600 seconds.", - "slowmode_success": "Slowmode set to {0} seconds in {1}.", + "slowmode_success": "Slowmode set to **{0}s** in {1}.", "stats_blackjack": "\ud83c\udccf | You've played **{0}** games of BlackJack, betting a total of **${1}**. You won **{2}** of those games with a total payout of **${3}**.", "stats_slots": "\ud83c\udfb0 | You've played **{0}** games of Slots, betting a total of **${1}**. Your total payout was **${2}**.", "sync_author": "Synced Commands", diff --git a/modules/moderation/slowmode.py b/modules/moderation/slowmode.py index a257b82..4610378 100644 --- a/modules/moderation/slowmode.py +++ b/modules/moderation/slowmode.py @@ -6,7 +6,6 @@ from discord.ext import commands from lib.const import CONST from lib.exceptions import LumiException from lib.format import format_duration_to_seconds -from ui.embeds import Builder class Slowmode(commands.Cog): @@ -22,55 +21,37 @@ class Slowmode(commands.Cog): async def slowmode( self, ctx: commands.Context[commands.Bot], - arg1: str, - arg2: str, + arg1: str | None = None, + arg2: str | None = None, ) -> None: - # define actual channel & duration - channel: discord.TextChannel | None = None - duration: int | None = None + channel, duration = None, None for arg in (arg1, arg2): - if not channel: - try: + if not channel and arg: + with contextlib.suppress(commands.BadArgument): channel = await commands.TextChannelConverter().convert(ctx, arg) - except commands.BadArgument: - with contextlib.suppress(LumiException): - duration = format_duration_to_seconds(arg) - else: + continue + if arg: with contextlib.suppress(LumiException): duration = format_duration_to_seconds(arg) - if not channel: await ctx.send(CONST.STRINGS["slowmode_channel_not_found"]) return if duration is None: - await ctx.send(CONST.STRINGS["slowmode_duration_not_found"]) + current_slowmode = channel.slowmode_delay + await ctx.send(CONST.STRINGS["slowmode_current_value"].format(channel.mention, current_slowmode)) return if duration < 0 or duration > 21600: # 21600 seconds = 6 hours (Discord's max slowmode) - await ctx.send("Slowmode duration must be between 0 and 21600 seconds.") + await ctx.send(CONST.STRINGS["slowmode_duration_error"]) return try: await channel.edit(slowmode_delay=duration) - embed = Builder.create_embed( - theme="success", - user_name=ctx.author.name, - author_text=CONST.STRINGS["slowmode_author"], - description=CONST.STRINGS["slowmode_success"].format(duration, channel.mention), - footer_text=CONST.STRINGS["slowmode_footer"], - ) - except discord.Forbidden: - embed = Builder.create_embed( - theme="error", - user_name=ctx.author.name, - author_text=CONST.STRINGS["slowmode_author"], - description=CONST.STRINGS["slowmode_forbidden"], - footer_text=CONST.STRINGS["slowmode_footer"], - ) - - await ctx.send(embed=embed) + await ctx.send(CONST.STRINGS["slowmode_success"].format(duration, channel.mention)) + except discord.Forbidden as error: + raise LumiException(CONST.STRINGS["slowmode_forbidden"]) from error async def setup(bot: commands.Bot) -> None: