diff options
Diffstat (limited to 'bot/converters.py')
-rw-r--r-- | bot/converters.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/bot/converters.py b/bot/converters.py index 91f30ac5e..30ea7ca0f 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -1,6 +1,8 @@ import logging +from datetime import datetime from ssl import CertificateError +import dateparser import discord from aiohttp import ClientConnectorError from discord.ext.commands import BadArgument, Context, Converter @@ -151,3 +153,22 @@ class TagContentConverter(Converter): raise BadArgument("Tag contents should not be empty, or filled with whitespace.") return tag_content + + +class ExpirationDate(Converter): + DATEPARSER_SETTINGS = { + 'PREFER_DATES_FROM': 'future', + 'TIMEZONE': 'UTC', + 'TO_TIMEZONE': 'UTC' + } + + async def convert(self, ctx, expiration_string: str): + expiry = dateparser.parse(expiration_string, settings=self.DATEPARSER_SETTINGS) + if expiry is None: + raise BadArgument(f"Failed to parse expiration date from `{expiration_string}`") + + now = datetime.utcnow() + if expiry < now: + expiry = now + (now - expiry) + + return expiry |