fulvia/modules/remind.py

138 lines
3.5 KiB
Python
Raw Permalink 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
2020-01-16 15:23:56 -05:00
from collections import defaultdict
2018-03-16 03:13:43 -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')
def remind_recur(bot, trigger):
2020-01-16 15:23:56 -05:00
"""
Sets a recurring reminder.
2020-01-16 15:23:56 -05: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.
-r, --recur - Repeats the reminder every given period.
2020-01-16 15:23:56 -05: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)
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:
2023-06-29 19:50:57 -04:00
s = ' '.join(args.start)
try:
2023-06-29 19:50:57 -04:00
start = datetime.strptime(s,'%Y-%m-%d %H:%M:%S')
except ValueError:
return bot.msg("Datetime improperly formatted.")
2020-01-16 15:23:56 -05:00
else:
start = datetime.now()
2018-03-16 03:13:43 -04:00
reg = re.search(regex, args.period)
2020-01-13 13:12:36 -05:00
if not reg:
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:
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 "
msg += "\x0310lazy\x03 " if args.lazy else ""
2020-01-16 15:23:56 -05:00
msg += "remind at "
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
@module.require_admin
@module.commands('clear_dup_reminders')
2024-06-20 14:52:08 -04:00
def clear_dup_reminders(bot, trigger):
"""
Clear out duplicate reminders from Fulvia's memory.
This is only a temporary fix to the memory issue.
"""
bot.scheduler.tasks = list(dict.fromkeys(bot.scheduler.tasks))
bot.scheduler.dump_database()
2023-06-29 19:50:57 -04:00
def announce_reminder(bot, sched_time, channel, remindee, reminder, delta=None):
2020-01-13 13:12:36 -05:00
"""Announce the reminder."""
bot.msg(channel, reminder)
if delta:
2023-06-29 19:50:57 -04:00
sched_time = sched_time + delta
args = (channel, remindee, reminder, delta)
bot.scheduler.add_task(announce_reminder, sched_time, args)
2023-06-29 19:50:57 -04:00
def announce_lazy_reminder(bot, sched_time, remindee, reminder, delta=None):
"""Announce the reminder."""
bot.memory['lazy_remind'][remindee].append(reminder)
if delta:
2023-06-29 19:50:57 -04:00
sched_time = sched_time + 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."""
for remind in list(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)