Better date parsing to be future-only
This commit is contained in:
parent
9f65213ea6
commit
0729bce295
1 changed files with 28 additions and 2 deletions
|
@ -2,10 +2,12 @@
|
|||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
from bson import ObjectId
|
||||
from dateparser import parse
|
||||
from dateparser_data.settings import default_parsers
|
||||
from dis_snek import InteractionContext, Scale, Snake
|
||||
from dis_snek.client.utils.misc_utils import get
|
||||
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)
|
||||
return
|
||||
|
||||
settings = {
|
||||
base_settings = {
|
||||
"PREFER_DATES_FROM": "future",
|
||||
"TIMEZONE": "UTC",
|
||||
"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:
|
||||
self.logger.debug(f"Failed to parse delay: {delay}")
|
||||
await response.send(
|
||||
|
@ -109,6 +126,15 @@ class RemindmeCog(Scale):
|
|||
)
|
||||
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(
|
||||
user=ctx.author.id,
|
||||
channel=ctx.channel.id,
|
||||
|
|
Loading…
Add table
Reference in a new issue