aboutsummaryrefslogtreecommitdiffstats
path: root/bot/converters.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--bot/converters.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/bot/converters.py b/bot/converters.py
index 3522a32aa..b31ef76eb 100644
--- a/bot/converters.py
+++ b/bot/converters.py
@@ -573,6 +573,55 @@ class Infraction(Converter):
raise e
+class DayDuration(Converter):
+ """
+ Convert a string representing day time (hours and minutes) to a UTC datetime object.
+
+ The hours and minutes 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 accepted:
+ - H:M
+ - H:Mam/pm
+ - HMam/pm
+ - Ham/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 convert `argument` to a UTC datetime object."""
+ match = self.TIME_RE.fullmatch(argument)
+ if not match:
+ raise BadArgument(f"`{argument}` is not a valid time duration string.")
+
+ hour_12, minute_12, meridiem, hour_24, minute_24 = match.groups()
+ 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
@@ -591,6 +640,7 @@ if t.TYPE_CHECKING:
UnambiguousUser = discord.User # noqa: F811
UnambiguousMember = discord.Member # noqa: F811
Infraction = t.Optional[dict] # noqa: F811
+ DayDuration = datetime # noqa: F811
Expiry = t.Union[Duration, ISODateTime]
MemberOrUser = t.Union[discord.Member, discord.User]