aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/converters.py48
-rw-r--r--bot/exts/moderation/modpings.py8
2 files changed, 52 insertions, 4 deletions
diff --git a/bot/converters.py b/bot/converters.py
index 559e759e1..7a5312467 100644
--- a/bot/converters.py
+++ b/bot/converters.py
@@ -574,6 +574,54 @@ class Infraction(Converter):
raise e
+class DayDuration(Converter):
+ """
+ Convert a string representing day time (hours and minutes) to an UTC datetime object.
+
+ The hours and mintues would be combined with UTC day, if no 'am' or 'pm' is passed with
+ the string, then it is assumed that the time is in 24 hour format.
+
+ The following formats are excepted:
+ - H:M
+ - H:M am/pm
+ - H am/pm
+ - H
+
+ where `H` represents Hours and `M` represents Minutes.
+ """
+
+ TIME_RE = re.compile(
+ r"^(1[0-2]|0?[1-9]):?([0-5][0-9])? ?([AaPp][Mm])$" # Twelve hour format
+ "|"
+ r"^([0-9]|0[0-9]|1[0-9]|2[0-4]):?([0-5][0-9])?$" # Twenty four hour format
+ )
+
+ async def convert(self, _ctx: Context, argument: str) -> datetime:
+ """Attempts to converting `argument` to an UTC datetime object."""
+ match = self.TIME_RE.fullmatch(argument).groups()
+ if not match:
+ raise BadArgument(f"`{argument}` is not a valid time duration string.")
+
+ hour_12, minute_12, meridiem, hour_24, minute_24 = match
+ time = None
+
+ if hour_12 and meridiem and minute_12:
+ time = datetime.strptime(f"{hour_12}:{minute_12} {meridiem}", "%I:%M %p")
+ elif hour_12 and meridiem:
+ time = datetime.strptime(f"{hour_12} {meridiem}", "%I %p")
+ elif hour_24 and minute_24:
+ time = datetime.strptime(f"{hour_24}:{minute_24}", "%H:%M")
+ else:
+ time = datetime.strptime(hour_24, "%H")
+
+ today = datetime.utcnow().date()
+ return time.replace(
+ year=today.year,
+ month=today.month,
+ day=today.day
+ )
+
+
if t.TYPE_CHECKING:
ValidDiscordServerInvite = dict # noqa: F811
ValidFilterListType = str # noqa: F811
diff --git a/bot/exts/moderation/modpings.py b/bot/exts/moderation/modpings.py
index 20a8c39d7..d214601de 100644
--- a/bot/exts/moderation/modpings.py
+++ b/bot/exts/moderation/modpings.py
@@ -3,13 +3,13 @@ import datetime
import arrow
from async_rediscache import RedisCache
-from dateutil.parser import isoparse, parse as dateutil_parse
+from dateutil.parser import isoparse
from discord import Embed, Member
from discord.ext.commands import Cog, Context, group, has_any_role
from bot.bot import Bot
from bot.constants import Colours, Emojis, Guild, Icons, MODERATION_ROLES, Roles
-from bot.converters import Expiry
+from bot.converters import DayDuration, Expiry
from bot.log import get_logger
from bot.utils import scheduling
from bot.utils.scheduling import Scheduler
@@ -199,9 +199,9 @@ class ModPings(Cog):
invoke_without_command=True
)
@has_any_role(*MODERATION_ROLES)
- async def schedule_modpings(self, ctx: Context, start: str, end: str) -> None:
+ async def schedule_modpings(self, ctx: Context, start: DayDuration, end: DayDuration) -> None:
"""Schedule modpings role to be added at <start> and removed at <end> everyday at UTC time!"""
- start, end = dateutil_parse(start), dateutil_parse(end)
+ print(start, end)
if end < start:
end += datetime.timedelta(days=1)