diff --git a/config.json.example b/config.json.example index ece5847..f709eb4 100644 --- a/config.json.example +++ b/config.json.example @@ -56,6 +56,7 @@ "separator": "or" }, "sleep": { + "commands": ["sleep", "wake up"], "sleep_time": 23, "wakeup_time": 7, "snore": "Zzzz", diff --git a/edmond/plugins/notes.py b/edmond/plugins/notes.py index f6fa279..c4f31de 100644 --- a/edmond/plugins/notes.py +++ b/edmond/plugins/notes.py @@ -64,6 +64,8 @@ class NotesPlugin(Plugin): self.deliver_notes(event.target, event.source.nick) return True + return False + def deliver_notes(self, target, nick): """Deliver all notes for this user in the target channel/user.""" notes = self.get_storage_value("notes", []) diff --git a/edmond/plugins/sleep.py b/edmond/plugins/sleep.py index 2611e28..de3a4a7 100644 --- a/edmond/plugins/sleep.py +++ b/edmond/plugins/sleep.py @@ -8,16 +8,18 @@ class SleepPlugin(Plugin): """Handle sleep state of the bot, snore a little bit.""" REQUIRED_CONFIGS = [ - "sleep_time", "wakeup_time", "snore", "sleep_message", "wakeup_message", - "snore_rate" + "commands", "sleep_time", "wakeup_time", "snore", "sleep_message", + "wakeup_message", "snore_rate" ] def __init__(self, bot): super().__init__(bot) + def on_welcome(self, event): + self.set_runtime_value("awake", True) + def on_ping(self, event): awake = self.get_runtime_value("awake") - self.bot.log_w(f"{self.bot.channels}") in_sleep_hours = self.is_sleep_time(datetime.now()) if awake and in_sleep_hours: self.fall_asleep() @@ -30,12 +32,38 @@ class SleepPlugin(Plugin): for channel in self.bot.channels: self.bot.say(channel, self.config["snore"]) + def on_pubmsg(self, event): + if not self.should_handle_command( + event.arguments[0], + no_content=True, + exclude_conditions=["sleep"], + ): + return False + + # "sleep" command. + command0 = self.config["commands"][0] + if self.command.ident.startswith(command0): + self.fall_asleep() + return True + + # "wake up" command. + command1 = self.config["commands"][1] + if self.command.ident.startswith(command1): + self.wake_up() + return True + + return False + def fall_asleep(self): + if not self.get_runtime_value("awake"): + return for channel in self.bot.channels: self.bot.say(channel, self.config["sleep_message"]) self.set_runtime_value("awake", False) def wake_up(self): + if self.get_runtime_value("awake"): + return self.set_runtime_value("awake", True) for channel in self.bot.channels: self.bot.say(channel, self.config["wakeup_message"])