Compare commits

...

1 Commits

Author SHA1 Message Date
2397ded4b5 add --lazy switch to remind 2020-01-16 15:23:56 -05:00
2 changed files with 78 additions and 8 deletions

4
db.py
View File

@ -20,7 +20,9 @@ class FulviaDB:
def connect(self): def connect(self):
"""Return a raw database connection object.""" """Return a raw database connection object."""
return sqlite3.connect(self.filename, timeout=10) return sqlite3.connect(self.filename,
timeout=10,
detect_types=sqlite3.PARSE_DECLTYPES)
def execute(self, *args, **kwargs): def execute(self, *args, **kwargs):
""" """

View File

@ -5,10 +5,21 @@ Reminds of things.
import os import os
import re import re
from datetime import datetime, timedelta from datetime import datetime, timedelta
from collections import defaultdict
import config import config
import module import module
def setup(bot):
bot.db.execute("CREATE TABLE IF NOT EXISTS lazy_remind ("
"nick TEXT,"
"datetime TIMESTAMP,"
"reminder TEXT)")
lazy_reminds = bot.db.execute("SELECT * FROM lazy_remind").fetchall()
bot.memory['lazy_remind'] = defaultdict(list)
for remind in lazy_reminds:
bot.memory['lazy_remind'][remind[0]].append(remind[1:3])
regex = ( regex = (
"(?=\d+[ywdhms])" "(?=\d+[ywdhms])"
"(\d+y)?" "(\d+y)?"
@ -30,9 +41,20 @@ shorthand = {
@module.commands('remind') @module.commands('remind')
@module.example('.remind 3h45m Go to class') @module.example('.remind 3h45m Go to class')
def remind(bot, trigger): def remind(bot, trigger):
"""Gives you a reminder in the given amount of time.""" """
Gives you a reminder in the given amount of time.
-l, --lazy - Only activates the reminder when you speak.
"""
if len(trigger.args) == 1: if len(trigger.args) == 1:
return bot.msg("Missing arguments for reminder command.") return bot.msg("Missing arguments for reminder command.")
if trigger.args[1] in ['-l', '--lazy']:
if len(trigger.args) == 2:
return bot.msg("Missing arguments for reminder command.")
lazy = True
trigger.args.pop(1)
else:
lazy = False
reg = re.search(regex, trigger.args[1]) reg = re.search(regex, trigger.args[1])
if not reg: if not reg:
@ -41,9 +63,18 @@ def remind(bot, trigger):
delta = timedelta(**args) delta = timedelta(**args)
dt = datetime.now() + delta dt = datetime.now() + delta
reminder = ' '.join(trigger.args[2:]) reminder = ' '.join(trigger.args[2:])
args = (trigger.channel, trigger.nick, reminder) if lazy:
bot.scheduler.add_task(announce_reminder, dt, args) bot.memory['lazy_remind'][trigger.nick].append((dt, reminder))
bot.reply("Okay, will remind at " + dt.strftime('[%Y-%m-%d %H:%M:%S]')) bot.db.execute("INSERT INTO lazy_remind VALUES(?,?,?)",
(trigger.nick, dt, reminder))
else:
args = (trigger.channel, trigger.nick, reminder)
bot.scheduler.add_task(announce_reminder, dt, args)
msg = "Okay, will "
msg += "\x0310lazy\x03 " if lazy else ""
msg += "remind at "
msg+= dt.strftime('[%Y-%m-%d %H:%M:%S]')
bot.reply(msg)
@module.commands('at') @module.commands('at')
@ -53,9 +84,18 @@ def at(bot, trigger):
Gives you a reminder at the given time and date. Datetime must be in 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 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. YYYY-MM-DD is excluded, it is assumed to be today's date.
-l, --lazy - Only activates the reminder when you speak.
""" """
if len(trigger.args) < 2: if len(trigger.args) < 2:
return bot.msg("Missing arguments for reminder command.") return bot.msg("Missing arguments for reminder command.")
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
if ':' in trigger.args[1]: if ':' in trigger.args[1]:
at_time = datetime.now().strftime('%Y-%m-%d') + ' ' + trigger.args[1] at_time = datetime.now().strftime('%Y-%m-%d') + ' ' + trigger.args[1]
reminder = ' '.join(trigger.args[2:]) reminder = ' '.join(trigger.args[2:])
@ -68,9 +108,18 @@ def at(bot, trigger):
except ValueError: except ValueError:
return bot.msg("Datetime improperly formatted.") return bot.msg("Datetime improperly formatted.")
args = (trigger.channel, trigger.nick, reminder) if lazy:
bot.scheduler.add_task(announce_reminder, dt, args) bot.memory['lazy_remind'][trigger.nick].append((dt, reminder))
bot.reply("Okay, will remind at " + dt.strftime('[%Y-%m-%d %H:%M:%S]')) bot.db.execute("INSERT INTO lazy_remind VALUES(?,?,?)",
(trigger.nick, dt, reminder))
else:
args = (trigger.channel, trigger.nick, reminder)
bot.scheduler.add_task(announce_reminder, dt, args)
msg = "Okay, will "
msg += "\x0310lazy\x03 " if lazy else ""
msg += "remind at "
msg+= dt.strftime('[%Y-%m-%d %H:%M:%S]')
bot.reply(msg)
def announce_reminder(bot, channel, remindee, reminder): def announce_reminder(bot, channel, remindee, reminder):
@ -80,3 +129,22 @@ def announce_reminder(bot, channel, remindee, reminder):
else: else:
msg = f"{remindee}!" msg = f"{remindee}!"
bot.msg(channel, msg) bot.msg(channel, msg)
@module.hook(True)
def lazy_remind(bot, trigger):
"""Lazy reminds only activate when the person speaks."""
if trigger.nick not in bot.memory['lazy_remind']:
return
due = [r for r in bot.memory['lazy_remind'][trigger.nick] if r[0] <= datetime.now()]
if not due:
return
for remind in due:
if remind[1]:
bot.reply(remind[1])
else:
bot.msg(trigger.nick + '!')
bot.memory['lazy_remind'][trigger.nick].remove(remind)
bot.db.execute("DELETE FROM lazy_remind "
"WHERE nick = ? AND datetime = ? AND reminder = ?",
(trigger.nick,) + tuple(remind))