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
|
2020-01-16 15:23:56 -05:00
|
|
|
from collections import defaultdict
|
2018-03-16 03:13:43 -04:00
|
|
|
|
2020-06-29 14:59:24 -04:00
|
|
|
import tools
|
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
|
|
|
|
2020-01-16 15:23:56 -05:00
|
|
|
def setup(bot):
|
|
|
|
bot.memory['lazy_remind'] = defaultdict(list)
|
|
|
|
|
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')
|
2020-06-29 14:59:24 -04:00
|
|
|
def remind_recur(bot, trigger):
|
2020-01-16 15:23:56 -05:00
|
|
|
"""
|
2020-06-29 14:59:24 -04:00
|
|
|
Sets a recurring reminder.
|
2020-01-16 15:23:56 -05:00
|
|
|
|
2020-06-29 14:59:24 -04:00
|
|
|
-s, --start - Starts the cycle at the given point in time instead of now.
|
2020-01-16 15:23:56 -05:00
|
|
|
-l, --lazy - Only activates the reminder when you speak.
|
2020-06-29 14:59:24 -04:00
|
|
|
-r, --recur - Repeats the reminder every given period.
|
2020-01-16 15:23:56 -05:00
|
|
|
"""
|
2020-06-29 14:59:24 -04:00
|
|
|
if len(trigger.args) < 2:
|
|
|
|
return bot.msg("Missing arguments.")
|
|
|
|
|
|
|
|
parser = tools.FulviaArgparse()
|
|
|
|
parser.add_argument('period')
|
|
|
|
parser.add_argument('reminder', nargs='*')
|
2020-06-29 15:15:33 -04:00
|
|
|
parser.add_argument('-s', '--start', nargs=2)
|
2020-06-29 14:59:24 -04:00
|
|
|
parser.add_argument('-l', '--lazy', action='store_true')
|
|
|
|
parser.add_argument('-r', '--recur', action='store_true')
|
|
|
|
try:
|
|
|
|
args = parser.parse_args(trigger.args[1:])
|
|
|
|
except Exception as e:
|
|
|
|
return bot.reply(type(e).__name__ + ": " + str(e))
|
|
|
|
|
|
|
|
if args.start:
|
|
|
|
try:
|
2020-06-29 15:15:33 -04:00
|
|
|
start = datetime.strptime(' '.join(args.start),'%Y-%m-%d %H:%M:%S')
|
2020-06-29 14:59:24 -04:00
|
|
|
except ValueError:
|
|
|
|
return bot.msg("Datetime improperly formatted.")
|
2020-01-16 15:23:56 -05:00
|
|
|
else:
|
2020-06-29 14:59:24 -04:00
|
|
|
start = datetime.now()
|
2018-03-16 03:13:43 -04:00
|
|
|
|
2020-06-29 14:59:24 -04:00
|
|
|
reg = re.search(regex, args.period)
|
2020-01-13 13:12:36 -05:00
|
|
|
if not reg:
|
2020-06-29 14:59:24 -04:00
|
|
|
return bot.reply("I didn't understand that time period.")
|
|
|
|
parts = {shorthand[g[-1]]: int(g[:-1]) for g in reg.groups() if g}
|
|
|
|
if parts.get('years'):
|
|
|
|
parts['days'] = parts['years']*365 + parts.get('days', 0) # screw leap years
|
|
|
|
del parts['years']
|
|
|
|
delta = timedelta(**parts)
|
|
|
|
sched_time = start + delta
|
|
|
|
|
|
|
|
reminder = ' '.join(args.reminder)
|
|
|
|
if reminder:
|
|
|
|
reminder = trigger.nick + ': ' + reminder
|
2020-01-16 15:23:56 -05:00
|
|
|
else:
|
2020-06-29 14:59:24 -04:00
|
|
|
reminder = trigger.nick + '!'
|
|
|
|
|
|
|
|
if args.recur:
|
|
|
|
recur = delta
|
|
|
|
else:
|
|
|
|
recur = None
|
|
|
|
|
|
|
|
if args.lazy:
|
|
|
|
sched_args = (trigger.nick, reminder, recur)
|
|
|
|
bot.scheduler.add_task(announce_lazy_reminder, sched_time, sched_args)
|
|
|
|
else:
|
|
|
|
sched_args = (trigger.channel, trigger.nick, reminder, recur)
|
|
|
|
bot.scheduler.add_task(announce_reminder, sched_time, sched_args)
|
|
|
|
|
2020-01-16 15:23:56 -05:00
|
|
|
msg = "Okay, will "
|
2020-06-29 14:59:24 -04:00
|
|
|
msg += "\x0310lazy\x03 " if args.lazy else ""
|
2020-01-16 15:23:56 -05:00
|
|
|
msg += "remind at "
|
2020-06-29 14:59:24 -04:00
|
|
|
msg+= sched_time.strftime('[%Y-%m-%d %H:%M:%S]')
|
2020-01-16 15:23:56 -05:00
|
|
|
bot.reply(msg)
|
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.
|
2020-01-16 15:23:56 -05:00
|
|
|
|
|
|
|
-l, --lazy - Only activates the reminder when you speak.
|
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-16 15:23:56 -05:00
|
|
|
if trigger.args[1] in ['-l', '--lazy']:
|
|
|
|
if len(trigger.args) < 3:
|
|
|
|
return bot.msg("Missing arguments for reminder command.")
|
|
|
|
lazy = True
|
|
|
|
trigger.args.pop(1)
|
|
|
|
else:
|
|
|
|
lazy = False
|
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-06-29 14:59:24 -04:00
|
|
|
if reminder:
|
|
|
|
reminder = trigger.nick + ': ' + reminder
|
|
|
|
else:
|
|
|
|
reminder = trigger.nick + '!'
|
|
|
|
|
2020-01-07 18:58:19 -05:00
|
|
|
try:
|
2020-06-29 14:59:24 -04:00
|
|
|
sched_time = 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-16 15:23:56 -05:00
|
|
|
if lazy:
|
2020-06-29 14:59:24 -04:00
|
|
|
sched_args = (trigger.nick, reminder)
|
|
|
|
bot.scheduler.add_task(announce_lazy_reminder, sched_time, sched_args)
|
2020-01-16 15:23:56 -05:00
|
|
|
else:
|
2020-06-29 14:59:24 -04:00
|
|
|
sched_args = (trigger.channel, trigger.nick, reminder)
|
|
|
|
bot.scheduler.add_task(announce_reminder, sched_time, sched_args)
|
2020-01-16 15:23:56 -05:00
|
|
|
msg = "Okay, will "
|
|
|
|
msg += "\x0310lazy\x03 " if lazy else ""
|
|
|
|
msg += "remind at "
|
2020-06-29 14:59:24 -04:00
|
|
|
msg+= sched_time.strftime('[%Y-%m-%d %H:%M:%S]')
|
2020-01-16 15:23:56 -05:00
|
|
|
bot.reply(msg)
|
2018-03-16 03:13:43 -04:00
|
|
|
|
|
|
|
|
2020-06-29 14:59:24 -04:00
|
|
|
def announce_reminder(bot, channel, remindee, reminder, delta=None):
|
2020-01-13 13:12:36 -05:00
|
|
|
"""Announce the reminder."""
|
2020-06-29 14:59:24 -04:00
|
|
|
bot.msg(channel, reminder)
|
|
|
|
if delta:
|
|
|
|
sched_time = datetime.now() + delta
|
|
|
|
args = (channel, remindee, reminder, delta)
|
|
|
|
bot.scheduler.add_task(announce_reminder, sched_time, args)
|
|
|
|
|
|
|
|
|
|
|
|
def announce_lazy_reminder(bot, remindee, reminder, delta=None):
|
|
|
|
"""Announce the reminder."""
|
|
|
|
bot.memory['lazy_remind'][remindee].append(reminder)
|
|
|
|
if delta:
|
|
|
|
sched_time = datetime.now() + delta
|
|
|
|
args = (remindee, reminder, delta)
|
|
|
|
bot.scheduler.add_task(announce_lazy_reminder, sched_time, args)
|
2020-01-16 15:23:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
@module.hook(True)
|
|
|
|
def lazy_remind(bot, trigger):
|
|
|
|
"""Lazy reminds only activate when the person speaks."""
|
2020-06-29 14:59:24 -04:00
|
|
|
for remind in bot.memory['lazy_remind'].get(trigger.nick, []):
|
|
|
|
bot.msg(remind)
|
2020-01-16 15:23:56 -05:00
|
|
|
bot.memory['lazy_remind'][trigger.nick].remove(remind)
|