sleep: add sleep/wakeup commands

This commit is contained in:
dece 2020-11-02 13:30:41 +01:00
parent 127ad5c895
commit a7b68a1596
3 changed files with 34 additions and 3 deletions

View file

@ -56,6 +56,7 @@
"separator": "or" "separator": "or"
}, },
"sleep": { "sleep": {
"commands": ["sleep", "wake up"],
"sleep_time": 23, "sleep_time": 23,
"wakeup_time": 7, "wakeup_time": 7,
"snore": "Zzzz", "snore": "Zzzz",

View file

@ -64,6 +64,8 @@ class NotesPlugin(Plugin):
self.deliver_notes(event.target, event.source.nick) self.deliver_notes(event.target, event.source.nick)
return True return True
return False
def deliver_notes(self, target, nick): def deliver_notes(self, target, nick):
"""Deliver all notes for this user in the target channel/user.""" """Deliver all notes for this user in the target channel/user."""
notes = self.get_storage_value("notes", []) notes = self.get_storage_value("notes", [])

View file

@ -8,16 +8,18 @@ class SleepPlugin(Plugin):
"""Handle sleep state of the bot, snore a little bit.""" """Handle sleep state of the bot, snore a little bit."""
REQUIRED_CONFIGS = [ REQUIRED_CONFIGS = [
"sleep_time", "wakeup_time", "snore", "sleep_message", "wakeup_message", "commands", "sleep_time", "wakeup_time", "snore", "sleep_message",
"snore_rate" "wakeup_message", "snore_rate"
] ]
def __init__(self, bot): def __init__(self, bot):
super().__init__(bot) super().__init__(bot)
def on_welcome(self, event):
self.set_runtime_value("awake", True)
def on_ping(self, event): def on_ping(self, event):
awake = self.get_runtime_value("awake") awake = self.get_runtime_value("awake")
self.bot.log_w(f"{self.bot.channels}")
in_sleep_hours = self.is_sleep_time(datetime.now()) in_sleep_hours = self.is_sleep_time(datetime.now())
if awake and in_sleep_hours: if awake and in_sleep_hours:
self.fall_asleep() self.fall_asleep()
@ -30,12 +32,38 @@ class SleepPlugin(Plugin):
for channel in self.bot.channels: for channel in self.bot.channels:
self.bot.say(channel, self.config["snore"]) 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): def fall_asleep(self):
if not self.get_runtime_value("awake"):
return
for channel in self.bot.channels: for channel in self.bot.channels:
self.bot.say(channel, self.config["sleep_message"]) self.bot.say(channel, self.config["sleep_message"])
self.set_runtime_value("awake", False) self.set_runtime_value("awake", False)
def wake_up(self): def wake_up(self):
if self.get_runtime_value("awake"):
return
self.set_runtime_value("awake", True) self.set_runtime_value("awake", True)
for channel in self.bot.channels: for channel in self.bot.channels:
self.bot.say(channel, self.config["wakeup_message"]) self.bot.say(channel, self.config["wakeup_message"])