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:
|
2023-06-29 19:50:57 -04:00
|
|
|
s = ' '.join(args.start)
|
2020-06-29 14:59:24 -04:00
|
|
|
try:
|
2023-06-29 19:50:57 -04:00
|
|
|
start = datetime.strptime(s,'%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
|
|
|
|
|
|
|
|
2024-06-20 14:46:13 -04:00
|
|
|
@module.require_admin
|
|
|
|
@module.commands('clear_dup_reminders')
|
2024-06-20 14:52:08 -04:00
|
|
|
def clear_dup_reminders(bot, trigger):
|
2024-06-20 14:46:13 -04:00
|
|
|
"""
|
|
|
|
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."""
|
2020-06-29 14:59:24 -04:00
|
|
|
bot.msg(channel, reminder)
|
|
|
|
if delta:
|
2023-06-29 19:50:57 -04:00
|
|
|
sched_time = sched_time + delta
|
2020-06-29 14:59:24 -04:00
|
|
|
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):
|
2020-06-29 14:59:24 -04:00
|
|
|
"""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
|
2020-06-29 14:59:24 -04:00
|
|
|
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-08-21 01:00:06 -04:00
|
|
|
for remind in list(bot.memory['lazy_remind'].get(trigger.nick, [])):
|
2020-06-29 14:59:24 -04:00
|
|
|
bot.msg(remind)
|
2020-01-16 15:23:56 -05:00
|
|
|
bot.memory['lazy_remind'][trigger.nick].remove(remind)
|