Better date parsing to be future-only

This commit is contained in:
Zeva Rose 2022-03-25 12:48:20 -06:00
parent 9f65213ea6
commit 0729bce295

View file

@ -2,10 +2,12 @@
import asyncio import asyncio
import logging import logging
import re import re
from datetime import datetime
from typing import List from typing import List
from bson import ObjectId from bson import ObjectId
from dateparser import parse from dateparser import parse
from dateparser_data.settings import default_parsers
from dis_snek import InteractionContext, Scale, Snake from dis_snek import InteractionContext, Scale, Snake
from dis_snek.client.utils.misc_utils import get from dis_snek.client.utils.misc_utils import get
from dis_snek.models.discord.channel import GuildChannel from dis_snek.models.discord.channel import GuildChannel
@ -96,12 +98,27 @@ class RemindmeCog(Scale):
await response.send("Hey, you should probably make this readable", ephemeral=True) await response.send("Hey, you should probably make this readable", ephemeral=True)
return return
settings = { base_settings = {
"PREFER_DATES_FROM": "future", "PREFER_DATES_FROM": "future",
"TIMEZONE": "UTC", "TIMEZONE": "UTC",
"RETURN_AS_TIMEZONE_AWARE": False, "RETURN_AS_TIMEZONE_AWARE": False,
} }
remind_at = parse(delay, settings=settings) rt_settings = base_settings.copy()
rt_settings["PARSERS"] = [
x for x in default_parsers if x not in ["absolute-time", "timestamp"]
]
rt_remind_at = parse(delay, settings=rt_settings)
at_settings = base_settings.copy()
at_settings["PARSERS"] = [x for x in default_parsers if x != "relative-time"]
at_remind_at = parse(delay, settings=at_settings)
if rt_remind_at and at_remind_at:
remind_at = max(rt_remind_at, at_remind_at)
else:
remind_at = rt_remind_at or at_remind_at
if not remind_at: if not remind_at:
self.logger.debug(f"Failed to parse delay: {delay}") self.logger.debug(f"Failed to parse delay: {delay}")
await response.send( await response.send(
@ -109,6 +126,15 @@ class RemindmeCog(Scale):
) )
return return
if remind_at < datetime.utcnow():
await response.send(
f"`{delay}` is in the past. Past reminders aren't allowed", ephemeral=True
)
return
elif remind_at < datetime.utcnow():
pass
r = Reminder( r = Reminder(
user=ctx.author.id, user=ctx.author.id,
channel=ctx.channel.id, channel=ctx.channel.id,