diff --git a/config.json.example b/config.json.example index ddf9e82..2e1c462 100644 --- a/config.json.example +++ b/config.json.example @@ -22,6 +22,10 @@ } } }, + "compliments": { + "sentences": ["you're breathtaking"], + "calm_rate": 100 + }, "horoscope": { "commands": ["horoscope"], "meditation": "/me looks at the starts", diff --git a/edmond/plugin.py b/edmond/plugin.py index 37d82f3..de65b13 100644 --- a/edmond/plugin.py +++ b/edmond/plugin.py @@ -86,11 +86,33 @@ class Plugin: 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_read_message(self, message): + """Return a message content if it has been addressed to me, else None. + + If the message starts with one of the bot's names, the rest of the + message is returned. It might be an empty str if the message was only + someone saying a bot's name. In other cases, return None. + + Note that you do not need to use this method for command/question + plugins, the following methods already check for this and do more + precise parsing of the message. This is rather for plugins reacting to a + message addressed to the bot that are neither commands or questions. + """ + first_word_and_rest = message.split(maxsplit=1) + num_parts = len(first_word_and_rest) + if num_parts == 0: + return None + elif first_word_and_rest[0] in self.bot.names: + if num_parts == 1: + return "" + else: + return first_word_and_rest[1].strip() + def should_answer_question(self, message): """Store Question in object and return True if it should answer it.""" words = message.split() # Is the message addressed to me? - if words[0].lower() not in self.bot.names: + if len(words) == 0 or words[0].lower() not in self.bot.names: return False # Are the config conditions to answer a question valid? @@ -178,6 +200,8 @@ class Plugin: method. """ words = message.split() + if len(words) == 0: + return None command_suffix = self.config["command_suffix"] if words[0].lower() in self.bot.names and words[-1] == command_suffix: raw = " ".join(words[1:-1]) diff --git a/edmond/plugins/compliments.py b/edmond/plugins/compliments.py new file mode 100644 index 0000000..3a95a78 --- /dev/null +++ b/edmond/plugins/compliments.py @@ -0,0 +1,36 @@ +import random + +from edmond.plugin import Plugin +from edmond.plugins.mood import Mood +from edmond.utils import proc + + +class ComplimentsPlugin(Plugin): + + REQUIRED_CONFIGS = ["sentences", "calm_rate"] + + def __init__(self, bot): + super().__init__(bot) + self.priority = -3 + self.mood_plugin = None + + def on_welcome(self, event): + self.mood_plugin = self.bot.get_plugin("mood") + + def on_pubmsg(self, event): + content = self.should_read_message(event.arguments[0]) + if content is None: + return False + if content in self.config["sentences"]: + self.react_to_compliment(event.source.nick, event.target) + return True + return False + + def react_to_compliment(self, sender, target): + mood = self.get_runtime_value("mood", ns="mood") + if self.mood_plugin is not None: + if mood == Mood.PISSED and proc(self.config["calm_rate"]): + self.mood_plugin.calm_down(target) + compliment = random.choice(self.config["sentences"]) + reply = f"{sender} {compliment}" + self.bot.say(target, reply)