fulvia/modules/remind.py

83 lines
2.1 KiB
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/usr/bin/env python3
"""
Reminds of things.
"""
import os
import re
2020-01-13 13:12:36 -05:00
from datetime import datetime, timedelta
2018-03-16 03:13:43 -04:00
2019-10-08 12:39:13 -04:00
import config
2020-01-13 13:12:36 -05:00
import module
2018-03-16 03:13:43 -04:00
2019-06-28 08:06:39 -04:00
regex = (
"(?=\d+[ywdhms])"
2020-01-13 13:12:36 -05:00
"(\d+y)?"
"(\d+w)?"
"(\d+d)?"
"(\d+h)?"
"(\d+m)?"
"(\d+s)?"
2019-06-28 08:06:39 -04:00
)
2020-01-13 13:12:36 -05:00
shorthand = {
'y': 'years',
'w': 'weeks',
'd': 'days',
'h': 'hours',
'm': 'minutes',
's': 'seconds',
}
@module.commands('remind')
@module.example('.remind 3h45m Go to class')
2018-03-16 03:13:43 -04:00
def remind(bot, trigger):
"""Gives you a reminder in the given amount of time."""
2020-01-07 18:58:19 -05:00
if len(trigger.args) == 1:
2018-05-25 15:21:18 -04:00
return bot.msg("Missing arguments for reminder command.")
2018-03-16 03:13:43 -04:00
2020-01-13 13:12:36 -05:00
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)
2020-01-14 15:02:55 -05:00
bot.reply("Okay, will remind at " + dt.strftime('[%Y-%m-%d %H:%M:%S]'))
2018-03-16 03:13:43 -04:00
2020-01-13 13:12:36 -05:00
@module.commands('at')
@module.example('.at 2012-12-21 18:00:00 End the world.')
2018-03-16 03:13:43 -04:00
def at(bot, trigger):
"""
2020-01-07 18:58:19 -05:00
Gives you a reminder at the given time and date. Datetime must be in
2020-01-13 13:12:36 -05:00
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.
2018-03-16 03:13:43 -04:00
"""
2020-01-13 13:12:36 -05:00
if len(trigger.args) < 2:
2020-01-07 18:58:19 -05:00
return bot.msg("Missing arguments for reminder command.")
2020-01-13 13:12:36 -05:00
if ':' in trigger.args[1]:
at_time = datetime.now().strftime('%Y-%m-%d') + ' ' + trigger.args[1]
reminder = ' '.join(trigger.args[2:])
2018-03-16 03:13:43 -04:00
else:
2020-01-13 13:12:36 -05:00
at_time = ' '.join(trigger.args[1:3])
2020-01-08 07:51:00 -05:00
reminder = ' '.join(trigger.args[3:])
2018-03-16 03:13:43 -04:00
2020-01-07 18:58:19 -05:00
try:
2020-01-13 13:12:36 -05:00
dt = datetime.strptime(at_time, '%Y-%m-%d %H:%M:%S')
2020-01-07 18:58:19 -05:00
except ValueError:
return bot.msg("Datetime improperly formatted.")
2018-03-16 03:13:43 -04:00
2020-01-13 13:12:36 -05:00
args = (trigger.channel, trigger.nick, reminder)
bot.scheduler.add_task(announce_reminder, dt, args)
2020-01-14 15:02:55 -05:00
bot.reply("Okay, will remind at " + dt.strftime('[%Y-%m-%d %H:%M:%S]'))
2018-03-16 03:13:43 -04:00
2020-01-13 13:12:36 -05:00
def announce_reminder(bot, channel, remindee, reminder):
"""Announce the reminder."""
if reminder:
msg = f"{remindee}: {reminder}"
2018-03-16 03:13:43 -04:00
else:
2020-01-13 13:12:36 -05:00
msg = f"{remindee}!"
bot.msg(channel, msg)