diff options
| -rw-r--r-- | bot/cogs/slowmode.py | 25 | ||||
| -rw-r--r-- | bot/utils/time.py | 4 |
2 files changed, 19 insertions, 10 deletions
diff --git a/bot/cogs/slowmode.py b/bot/cogs/slowmode.py index ec5e9cc0d..0b9b64976 100644 --- a/bot/cogs/slowmode.py +++ b/bot/cogs/slowmode.py @@ -1,5 +1,6 @@ import logging from datetime import datetime +from typing import Optional from dateutil.relativedelta import relativedelta from discord import TextChannel @@ -13,6 +14,8 @@ from bot.utils import time log = logging.getLogger(__name__) +SLOWMODE_MAX_DELAY = 21600 # seconds + class Slowmode(Cog): """Commands for getting and setting slowmode delays of text channels.""" @@ -26,15 +29,14 @@ class Slowmode(Cog): await ctx.send_help(ctx.command) @slowmode_group.command(name='get', aliases=['g']) - async def get_slowmode(self, ctx: Context, channel: TextChannel) -> None: + async def get_slowmode(self, ctx: Context, channel: Optional[TextChannel] = None) -> None: """Get the slowmode delay for a given text channel.""" - delay = relativedelta(seconds=channel.slowmode_delay) + # Use the channel this command was invoked in if one was not given + if channel is None: + channel = ctx.channel - # Say "0 seconds" instead of "less than a second" - if channel.slowmode_delay == 0: - humanized_delay = '0 seconds' - else: - humanized_delay = time.humanize_delta(delay) + delay = relativedelta(seconds=channel.slowmode_delay) + humanized_delay = time.humanize_delta(delay) await ctx.send(f'The slowmode delay for {channel.mention} is {humanized_delay}.') @@ -48,7 +50,8 @@ class Slowmode(Cog): humanized_delay = time.humanize_delta(delay) - if 0 <= slowmode_delay <= 21600: + # Ensure the delay is within discord's limits + if slowmode_delay <= SLOWMODE_MAX_DELAY: log.info(f'{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}.') await channel.edit(slowmode_delay=slowmode_delay) @@ -67,8 +70,12 @@ class Slowmode(Cog): ) @slowmode_group.command(name='reset', aliases=['r']) - async def reset_slowmode(self, ctx: Context, channel: TextChannel) -> None: + async def reset_slowmode(self, ctx: Context, channel: Optional[TextChannel] = None) -> None: """Reset the slowmode delay for a given text channel to 0 seconds.""" + # Use the channel this command was invoked in if one was not given + if channel is None: + channel = ctx.channel + log.info(f'{ctx.author} reset the slowmode delay for #{channel} to 0 seconds.') await channel.edit(slowmode_delay=0) diff --git a/bot/utils/time.py b/bot/utils/time.py index 77060143c..47e49904b 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -20,7 +20,9 @@ def _stringify_time_unit(value: int, unit: str) -> str: >>> _stringify_time_unit(0, "minutes") "less than a minute" """ - if value == 1: + if unit == "seconds" and value == 0: + return "0 seconds" + elif value == 1: return f"{value} {unit[:-1]}" elif value == 0: return f"less than a {unit[:-1]}" |