reminder: compat with older Python

This commit is contained in:
dece 2023-08-29 14:28:01 +02:00
parent f2e65c0341
commit d46e3b933a

View file

@ -1,6 +1,7 @@
import datetime import datetime
import re import re
import string import string
from typing import Optional
from apscheduler.triggers.date import DateTrigger from apscheduler.triggers.date import DateTrigger
@ -44,7 +45,7 @@ class ReminderPlugin(Plugin):
return True return True
# self.bot.scheduler.add_job(, 'cron', hour='1', minute='52') # self.bot.scheduler.add_job(, 'cron', hour='1', minute='52')
def parse_time(self, time: str) -> dict[str, int] | None: def parse_time(self, time: str) -> Optional[dict[str, int]]:
"""Parse a time request string. """Parse a time request string.
Return a dict with day/hour/minute/second set as integers if specified. Return a dict with day/hour/minute/second set as integers if specified.
@ -80,7 +81,12 @@ class ReminderPlugin(Plugin):
if mode == self.config["at_word"]: if mode == self.config["at_word"]:
if "day" in time: # "day" is not supported in at mode. if "day" in time: # "day" is not supported in at mode.
del time["day"] del time["day"]
when = now.replace(**time) when = datetime.datetime.today().replace(
hour=time.get("hour", 0),
minute=time.get("minute", 0),
second=time.get("second", 0),
microsecond=0
)
elif mode == self.config["in_word"]: elif mode == self.config["in_word"]:
time = { k + "s": v for k, v in time.items() } # Use plural names. time = { k + "s": v for k, v in time.items() } # Use plural names.
when = now + datetime.timedelta(**time) when = now + datetime.timedelta(**time)
@ -99,6 +105,7 @@ class ReminderPlugin(Plugin):
trigger=DateTrigger(when), trigger=DateTrigger(when),
args=(reminder, sender, target) args=(reminder, sender, target)
) )
self.bot.log_d(f"Scheduled for {when}, time was {time}.")
self.bot.say(target, self.config["done"]) self.bot.say(target, self.config["done"])
async def remind(self, reminder: str, username: str, target: str) -> None: async def remind(self, reminder: str, username: str, target: str) -> None: