fulvia/modules/remind.py
2020-01-13 13:12:36 -05:00

83 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Reminds of things.
"""
import os
import re
from datetime import datetime, timedelta
import config
import module
regex = (
"(?=\d+[ywdhms])"
"(\d+y)?"
"(\d+w)?"
"(\d+d)?"
"(\d+h)?"
"(\d+m)?"
"(\d+s)?"
)
shorthand = {
'y': 'years',
'w': 'weeks',
'd': 'days',
'h': 'hours',
'm': 'minutes',
's': 'seconds',
}
@module.commands('remind')
@module.example('.remind 3h45m Go to class')
def remind(bot, trigger):
"""Gives you a reminder in the given amount of time."""
if len(trigger.args) == 1:
return bot.msg("Missing arguments for reminder command.")
reg = re.search(regex, trigger.args[1])
if not reg:
return bot.reply("I didn't understand that.")
args = {shorthand[g[-1]]: int(g[:-1]) for g in reg.groups() if g}
delta = timedelta(**args)
dt = datetime.now() + delta
reminder = ' '.join(trigger.args[2:])
args = (trigger.channel, trigger.nick, reminder)
bot.scheduler.add_task(announce_reminder, dt, args)
bot.reply("Okay, will remind at", dt.strftime('[%Y-%m-%d %H:%M:%S]'))
@module.commands('at')
@module.example('.at 2012-12-21 18:00:00 End the world.')
def at(bot, trigger):
"""
Gives you a reminder at the given time and date. Datetime must be in
YYYY-MM-DD HH:MM:SS format. Only the bot's timezone is used. If
YYYY-MM-DD is excluded, it is assumed to be today's date.
"""
if len(trigger.args) < 2:
return bot.msg("Missing arguments for reminder command.")
if ':' in trigger.args[1]:
at_time = datetime.now().strftime('%Y-%m-%d') + ' ' + trigger.args[1]
reminder = ' '.join(trigger.args[2:])
else:
at_time = ' '.join(trigger.args[1:3])
reminder = ' '.join(trigger.args[3:])
try:
dt = datetime.strptime(at_time, '%Y-%m-%d %H:%M:%S')
except ValueError:
return bot.msg("Datetime improperly formatted.")
args = (trigger.channel, trigger.nick, reminder)
bot.scheduler.add_task(announce_reminder, dt, args)
bot.reply("Okay, will remind at", dt.strftime('[%Y-%m-%d %H:%M:%S]'))
def announce_reminder(bot, channel, remindee, reminder):
"""Announce the reminder."""
if reminder:
msg = f"{remindee}: {reminder}"
else:
msg = f"{remindee}!"
bot.msg(channel, msg)