Edm0nd/edmond/plugin.py

142 lines
5.2 KiB
Python
Raw Normal View History

from dataclasses import dataclass
from pathlib import Path
class Plugin:
2020-10-09 12:49:35 +02:00
REQUIRED_CONFIGS = []
def __init__(self, bot):
self.bot = bot
2020-10-09 12:19:58 +02:00
self.name = self.__class__.__name__.lower()[:-6] # Remove "Plugin".
2020-10-09 22:34:32 +02:00
self.config = self._get_config()
self.is_ready = self._check_config()
@property
def callbacks(self):
"""List of callback types available for this plugin."""
return {
cb[3:]: getattr(self, cb)
for cb in dir(self)
if cb.startswith("on_") and callable(getattr(self, cb))
}
2020-10-09 22:34:32 +02:00
def _get_config(self):
2020-10-09 12:49:35 +02:00
"""Return the plugin section from the bot config, plus common values."""
plugins_configs = self.bot.config["plugins"]
2020-10-09 13:30:52 +02:00
config = plugins_configs["common"].copy()
2020-10-09 12:49:35 +02:00
config.update(plugins_configs.get(self.name, {}))
# Load resources as config values.
resources_dir = self.bot.config["resources_dir"]
for key, resource_path in config.get("resources", {}).items():
resource_path = Path(resources_dir) / resource_path
try:
with open(resource_path, "rt") as resource_file:
config[key] = [l.strip() for l in resource_file.readlines()]
except OSError:
self.bot.log_e(f"Could not load resource at {resource_path}.")
2020-10-09 12:49:35 +02:00
return config
2020-10-09 22:34:32 +02:00
def _check_config(self):
2020-10-09 12:49:35 +02:00
"""Return True if the plugin config is properly setup."""
missing = False
for key in self.REQUIRED_CONFIGS:
if key not in self.config:
self.bot.log_e(f"Missing '{key}' in {self.name} configuration.")
2020-10-09 12:49:35 +02:00
missing = True
2020-10-09 13:30:52 +02:00
return not missing
2020-10-09 12:19:58 +02:00
def get_runtime_value(self, key, default=None, ns=None):
2020-10-09 23:37:35 +02:00
"""Get a value from the plugin runtime dict.
This will get the value from the plugin namespace, but it is possible to
get runtime values from other plugins using their name as `ns`.
"""
2020-10-09 12:49:35 +02:00
if ns is None:
ns = self.name
return self.bot.values[ns].get(key, default)
2020-10-09 12:19:58 +02:00
def set_runtime_value(self, key, value):
"""Set a value in the plugin runtime dict."""
self.bot.values[self.name][key] = value
def get_storage_value(self, key, default=None):
2020-10-09 23:37:35 +02:00
"""Get a value from the plugin persistent storage."""
return self.bot.storage.get(self.name, {}).get(key, default)
2020-10-09 23:37:35 +02:00
def set_storage_value(self, key, value):
"""Set a value in the plugin persistent storage."""
if self.name not in self.bot.storage:
self.bot.storage[self.name] = {key: value}
else:
self.bot.storage[self.name][key] = value
def append_storage_list_value(self, key, value):
"""Append a value to a list in the plugin persistent storage."""
if self.name not in self.bot.storage:
self.bot.storage[self.name] = {key: [value]}
elif key not in self.bot.storage[self.name]:
self.bot.storage[self.name][key] = [value]
else:
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)
2020-10-09 12:19:58 +02:00
def should_answer_question(self, message):
"""Store Question in object and return True if it should answer it."""
words = message.split()
if words[0].lower() not in self.bot.names:
return False
question = message[len(words[0]):].strip()
2020-10-09 12:49:35 +02:00
for q in self.config.get("questions", []):
2020-10-09 12:19:58 +02:00
if question.startswith(q):
self.question = Question(q, question[len(q):].strip())
2020-10-09 13:30:52 +02:00
self.bot.log_d(f"Answering question from plugin {self.name}.")
2020-10-09 12:19:58 +02:00
return True
return False
def should_handle_command(self, message, no_content=False):
2020-10-09 12:19:58 +02:00
"""Store Command in object and return True if it should handle it."""
command = self.parse_command(message, no_content=no_content)
2020-10-09 12:49:35 +02:00
commands = self.config.get("commands", [])
2020-10-09 23:37:35 +02:00
if (
any(command.ident == c for c in commands) or
(no_content and any(command.ident.startswith(c) for c in commands))
):
self.command = command
2020-10-09 13:30:52 +02:00
self.bot.log_d(f"Processing command from plugin {self.name}.")
return True
return False
def parse_command(self, message, no_content=False):
"""Return a command ID if this message is a command."""
words = message.split()
2020-10-09 12:49:35 +02:00
command_suffix = self.config["command_suffix"]
if words[0].lower() in self.bot.names and words[-1] == command_suffix:
if no_content:
ident = " ".join(words[1:-1])
content = ""
else:
ident = words[1]
content = " ".join(words[2:-1])
return Command(ident, content)
def signal_failure(self, target):
"""Signal a plugin failure to target."""
self.bot.say(target, self.bot.config["error_message"])
2020-10-09 12:19:58 +02:00
@dataclass
class Question:
preambule: str
content: str
@dataclass
class Command:
ident: str
content: str