From 3f16521663b5804d938ad6b29b369409edbf77e4 Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 11 Feb 2020 11:19:17 +0000 Subject: [PATCH] add cron_reminders.py (admin-only for now) --- modules/cron_reminders.py | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 modules/cron_reminders.py diff --git a/modules/cron_reminders.py b/modules/cron_reminders.py new file mode 100644 index 00000000..396188ae --- /dev/null +++ b/modules/cron_reminders.py @@ -0,0 +1,59 @@ +from src import ModuleManager, utils + +SETTING = "cron-reminders" + +class Module(ModuleManager.BaseModule): + _name = "cron" + + @utils.hook("cron") + def cron(self, event): + try: + self._catch_cron(event["schedule"]) + except Exception as e: + self.log.error("Failed to call cron reminders: %s", [str(e)], + exc_info=True) + def _catch_cron(self, schedule_check): + all = [] + for server in self.bot.servers.values(): + channel_reminders = server.find_all_user_channel_settings(SETTING) + for channel_name, nickname, value in channel_reminders: + if channel_name in server.channels: + channel = server.channels.get(channel_name) + for schedule, text in value: + if schedule_check(schedule): + all.append([channel, nickname, text]) + + user_reminders = server.get_all_user_settings(SETTING) + for nickname, value in user_reminders: + if server.has_user(nickname): + user = server.get_user(nickname) + for schedule, text in value: + if schedule_check(schedule): + all.append([user, user.nickname, text]) + + for target, nickname, text in all: + target.send_message("%s, reminder: %s" % (nickname, text)) + + + @utils.hook("received.command.cron") + @utils.kwarg("permission", "cron") + @utils.spec("!'add !word !string") + @utils.spec("!'remove !int") + def command(self, event): + user_id = event["user"].get_id() + r = event["target"].get_user_setting(user_id, SETTING, []) + + if event["spec"][0] == "add": + r.append([event["spec"][1].replace("+", " "), event["spec"][2]]) + event["stdout"].write("%s: added reminder" + % event["user"].nickname) + + elif event["spec"][0] == "remove": + if len(r) <= event["spec"][1]: + raise utils.EventError("%s: invalid index (max %d)" + % (event["user"].nickname, len(r)-1)) + schedule, text = r.pop(event["spec"][1]) + event["stdout"].write("%s: removed reminder %d: %s (%s)" + % (event["user"].nickname, event["spec"][1], schedule, text)) + + event["target"].set_user_setting(user_id, SETTING, r)