notes: deliver notes when an user joins

This commit is contained in:
dece 2020-10-12 15:45:31 +02:00
parent 3d918d1de8
commit c2287f3cee
7 changed files with 42 additions and 10 deletions

View file

@ -44,3 +44,4 @@ Missing features
- [ ] Command aliases - [ ] Command aliases
- [ ] Question aliases - [ ] Question aliases
- [ ] Various macros - [ ] Various macros
- [ ] Sleep

View file

@ -40,7 +40,8 @@
}, },
"notes": { "notes": {
"commands": ["note down"], "commands": ["note down"],
"content_regex": "for (P:<target>\\S+) (P:<note>.+)" "content_regex": "for (?P<target>\\S+) (?P<note>.+)",
"deliver_format": "{dest}, {sender} tells you: {message}"
}, },
"random": { "random": {
"commands": ["choose"], "commands": ["choose"],
@ -50,7 +51,7 @@
"commands": ["science", "define"], "commands": ["science", "define"],
"lang": "en", "lang": "en",
"ambiguous_response": "It is ambiguous.", "ambiguous_response": "It is ambiguous.",
"empty_response": "I can't find it.", "empty_response": "I can't find it."
} }
} }
} }

View file

@ -22,11 +22,13 @@ class Bot(irc.client.SimpleIRCClient, Logger):
@property @property
def nick(self): def nick(self):
if self.connection.is_connected():
return self.connection.get_nickname()
return self.config["nick"] return self.config["nick"]
@property @property
def names(self): def names(self):
return (self.config["nick"], *self.config["alternative_nicks"]) return (self.nick, *self.config["alternative_nicks"])
def _get_storage(self): def _get_storage(self):
"""Load data from storage.""" """Load data from storage."""
@ -57,10 +59,12 @@ class Bot(irc.client.SimpleIRCClient, Logger):
connection.join(channel) connection.join(channel)
def on_join(self, connection, event): def on_join(self, connection, event):
if event.source.nick == self.nick:
self.log_i(f"Joined {event.target}.") self.log_i(f"Joined {event.target}.")
self.run_plugin_callbacks(event) self.run_plugin_callbacks(event)
def on_part(self, connection, event): def on_part(self, connection, event):
if event.source.nick == self.nick:
self.log_i(f"Left {event.target} (args: {event.arguments[0]}).") self.log_i(f"Left {event.target} (args: {event.arguments[0]}).")
self.run_plugin_callbacks(event) self.run_plugin_callbacks(event)

View file

@ -46,7 +46,7 @@ class Plugin:
missing = True missing = True
return not missing return not missing
def get_runtime_value(self, key, ns=None): def get_runtime_value(self, key, default=None, ns=None):
"""Get a value from the plugin runtime dict. """Get a value from the plugin runtime dict.
This will get the value from the plugin namespace, but it is possible to This will get the value from the plugin namespace, but it is possible to
@ -54,15 +54,15 @@ class Plugin:
""" """
if ns is None: if ns is None:
ns = self.name ns = self.name
return self.bot.values[ns].get(key) return self.bot.values[ns].get(key, default)
def set_runtime_value(self, key, value): def set_runtime_value(self, key, value):
"""Set a value in the plugin runtime dict.""" """Set a value in the plugin runtime dict."""
self.bot.values[self.name][key] = value self.bot.values[self.name][key] = value
def get_storage_value(self, key): def get_storage_value(self, key, default=None):
"""Get a value from the plugin persistent storage.""" """Get a value from the plugin persistent storage."""
return self.bot.storage.get(self.name, {}).get(key) return self.bot.storage.get(self.name, {}).get(key, default)
def set_storage_value(self, key, value): def set_storage_value(self, key, value):
"""Set a value in the plugin persistent storage.""" """Set a value in the plugin persistent storage."""
@ -80,6 +80,10 @@ class Plugin:
else: else:
self.bot.storage[self.name][key].append(value) self.bot.storage[self.name][key].append(value)
def remove_storage_list_value(self, key, value):
if self.name in self.bot.storage and key in self.bot.storage[self.name]:
self.bot.storage[self.name][key].remove(value)
def should_answer_question(self, message): def should_answer_question(self, message):
"""Store Question in object and return True if it should answer it.""" """Store Question in object and return True if it should answer it."""
words = message.split() words = message.split()

View file

@ -21,6 +21,8 @@ class MoodPlugin(Plugin):
self.set_runtime_value("mood", mood) self.set_runtime_value("mood", mood)
def on_join(self, event): def on_join(self, event):
if event.source.nick != self.bot.nick:
return
mood = self.get_runtime_value("mood") mood = self.get_runtime_value("mood")
greetings = self.config["greetings"].get(mood.value) greetings = self.config["greetings"].get(mood.value)
if greetings: if greetings:

View file

@ -5,7 +5,7 @@ from edmond.plugin import Plugin
class NotesPlugin(Plugin): class NotesPlugin(Plugin):
REQUIRED_CONFIGS = ["commands", "content_regex"] REQUIRED_CONFIGS = ["commands", "content_regex", "deliver_format"]
def __init__(self, bot): def __init__(self, bot):
super().__init__(bot) super().__init__(bot)
@ -17,6 +17,21 @@ class NotesPlugin(Plugin):
self._content_re = re.compile(self.config["content_regex"]) self._content_re = re.compile(self.config["content_regex"])
return self._content_re return self._content_re
def on_join(self, event):
nick = event.source.nick
if nick == self.bot.nick:
return
notes = self.get_storage_value("notes", [])
notes = filter(lambda n: n["dest"] == nick, notes)
for note in notes:
message = self.config["deliver_format"].format(
sender=note["sender"],
dest=nick,
message=note["message"],
)
self.bot.say(event.target, message)
self.remove_storage_list_value("notes", note)
def on_pubmsg(self, event): def on_pubmsg(self, event):
if not self.should_handle_command(event.arguments[0], no_content=True): if not self.should_handle_command(event.arguments[0], no_content=True):
return False return False

View file

@ -1,3 +1,5 @@
import random
import requests import requests
@ -5,3 +7,6 @@ def http_get(url):
response = requests.get(url) response = requests.get(url)
if response.status_code == 200: if response.status_code == 200:
return response.text return response.text
def proc(proba_percentage):
return random.random() < (proba_percentage / 100.0)