From f055ef76c44ef6db95bc93a40d4128d82fadea08 Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 2 Nov 2020 12:51:26 +0100 Subject: [PATCH] plugin: add handling conditions for ques. & cmd. The main purpose of this feature is to not answer questions when the bot is not awake in the sleep plugin, but by making it a requirement purely in the configuration we avoid making some plugins mandatory for the bot. --- README.md | 2 +- config.json.example | 7 ++++++- edmond/plugin.py | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 05edbbf..24efe4c 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Missing features - [ ] Youtube: parsing for title, requests for channel or video - [ ] Command aliases - [ ] Question aliases -- [ ] Sleep +- [x] Sleep - [ ] Various macros: - [ ] asks someone to stop doing something - [ ] king of diff --git a/config.json.example b/config.json.example index 47c6da1..ece5847 100644 --- a/config.json.example +++ b/config.json.example @@ -15,7 +15,12 @@ "opening_text": "/me cracks open a {beer} for {target}" }, "common": { - "command_suffix": "please" + "command_suffix": "please", + "handling_conditions": { + "sleep": { + "awake": true + } + } }, "horoscope": { "commands": ["horoscope"], diff --git a/edmond/plugin.py b/edmond/plugin.py index 1ef93cb..eef374b 100644 --- a/edmond/plugin.py +++ b/edmond/plugin.py @@ -9,8 +9,8 @@ class Plugin: def __init__(self, bot): self.bot = bot self.name = self.__class__.__name__.lower()[:-6] # Remove "Plugin". - self.config = self._get_config() - self.is_ready = self._check_config() + self.config = self.__get_config() + self.is_ready = self.__check_config() @property def callbacks(self): @@ -21,7 +21,7 @@ class Plugin: if cb.startswith("on_") and callable(getattr(self, cb)) } - def _get_config(self): + def __get_config(self): """Return the plugin section from the bot config, plus common values.""" plugins_configs = self.bot.config["plugins"] config = plugins_configs["common"].copy() @@ -37,7 +37,7 @@ class Plugin: self.bot.log_e(f"Could not load resource at {resource_path}.") return config - def _check_config(self): + def __check_config(self): """Return True if the plugin config is properly setup.""" missing = False for key in self.REQUIRED_CONFIGS: @@ -88,10 +88,16 @@ class Plugin: 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: return False - question = message[len(words[0]):].strip() + # Are the config conditions to answer a question valid? + if not self.__respects_handling_conditions(): + return False + + # Is it a question I can answer? + question = message[len(words[0]):].strip() for q in self.config.get("questions", []): if question.startswith(q): self.question = Question(q, question[len(q):].strip()) @@ -101,9 +107,16 @@ class Plugin: def should_handle_command(self, message, no_content=False): """Store Command in object and return True if it should handle it.""" + # Are the config conditions to handle a command valid? + if not self.__respects_handling_conditions(): + return False + + # Is it a valid command? command = self.parse_command(message, no_content=no_content) if not command: return False + + # Is it a command I can handle? commands = self.config.get("commands", []) if ( any(command.ident == c for c in commands) or @@ -127,6 +140,19 @@ class Plugin: content = " ".join(words[2:-1]) return Command(ident, content) + def __respects_handling_conditions(self): + """Check if question conditions are valid.""" + conditions = self.config.get("handling_conditions", {}) + for plugin_ns, plugin_conditions in conditions.items(): + for condition_key, condition_value in plugin_conditions.items(): + value = self.get_runtime_value(condition_key, ns=plugin_ns) + if condition_value != value: + self.bot.log_d( + f"Handling condition {plugin_ns}.{condition_key} false." + ) + return False + return True + def signal_failure(self, target): """Signal a plugin failure to target.""" self.bot.say(target, self.bot.config["error_message"])