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.
This commit is contained in:
dece 2020-11-02 12:51:26 +01:00
parent f346b9cb58
commit f055ef76c4
3 changed files with 38 additions and 7 deletions

View file

@ -43,7 +43,7 @@ Missing features
- [ ] Youtube: parsing for title, requests for channel or video - [ ] Youtube: parsing for title, requests for channel or video
- [ ] Command aliases - [ ] Command aliases
- [ ] Question aliases - [ ] Question aliases
- [ ] Sleep - [x] Sleep
- [ ] Various macros: - [ ] Various macros:
- [ ] asks someone to stop doing something - [ ] asks someone to stop doing something
- [ ] king of - [ ] king of

View file

@ -15,7 +15,12 @@
"opening_text": "/me cracks open a {beer} for {target}" "opening_text": "/me cracks open a {beer} for {target}"
}, },
"common": { "common": {
"command_suffix": "please" "command_suffix": "please",
"handling_conditions": {
"sleep": {
"awake": true
}
}
}, },
"horoscope": { "horoscope": {
"commands": ["horoscope"], "commands": ["horoscope"],

View file

@ -9,8 +9,8 @@ class Plugin:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.name = self.__class__.__name__.lower()[:-6] # Remove "Plugin". self.name = self.__class__.__name__.lower()[:-6] # Remove "Plugin".
self.config = self._get_config() self.config = self.__get_config()
self.is_ready = self._check_config() self.is_ready = self.__check_config()
@property @property
def callbacks(self): def callbacks(self):
@ -21,7 +21,7 @@ class Plugin:
if cb.startswith("on_") and callable(getattr(self, cb)) 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.""" """Return the plugin section from the bot config, plus common values."""
plugins_configs = self.bot.config["plugins"] plugins_configs = self.bot.config["plugins"]
config = plugins_configs["common"].copy() config = plugins_configs["common"].copy()
@ -37,7 +37,7 @@ class Plugin:
self.bot.log_e(f"Could not load resource at {resource_path}.") self.bot.log_e(f"Could not load resource at {resource_path}.")
return config return config
def _check_config(self): def __check_config(self):
"""Return True if the plugin config is properly setup.""" """Return True if the plugin config is properly setup."""
missing = False missing = False
for key in self.REQUIRED_CONFIGS: for key in self.REQUIRED_CONFIGS:
@ -88,10 +88,16 @@ class Plugin:
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()
# Is the message addressed to me?
if words[0].lower() not in self.bot.names: if words[0].lower() not in self.bot.names:
return False 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", []): for q in self.config.get("questions", []):
if question.startswith(q): if question.startswith(q):
self.question = Question(q, question[len(q):].strip()) self.question = Question(q, question[len(q):].strip())
@ -101,9 +107,16 @@ class Plugin:
def should_handle_command(self, message, no_content=False): def should_handle_command(self, message, no_content=False):
"""Store Command in object and return True if it should handle it.""" """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) command = self.parse_command(message, no_content=no_content)
if not command: if not command:
return False return False
# Is it a command I can handle?
commands = self.config.get("commands", []) commands = self.config.get("commands", [])
if ( if (
any(command.ident == c for c in commands) or any(command.ident == c for c in commands) or
@ -127,6 +140,19 @@ class Plugin:
content = " ".join(words[2:-1]) content = " ".join(words[2:-1])
return Command(ident, content) 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): def signal_failure(self, target):
"""Signal a plugin failure to target.""" """Signal a plugin failure to target."""
self.bot.say(target, self.bot.config["error_message"]) self.bot.say(target, self.bot.config["error_message"])