From dc941c19852b146143d244df59cf2233dbdcaeca Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 9 Oct 2020 12:19:58 +0200 Subject: [PATCH] plugin: add question-type plugins --- edmond/__main__.py | 3 +++ edmond/bot.py | 3 +++ edmond/plugin.py | 35 ++++++++++++++++++++++++++++++++--- edmond/plugins/horoscope.py | 1 + edmond/plugins/mood.py | 31 +++++++++++++++++++++++++++++++ edmond/plugins/opinion.py | 8 -------- 6 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 edmond/plugins/mood.py delete mode 100644 edmond/plugins/opinion.py diff --git a/edmond/__main__.py b/edmond/__main__.py index 342995a..28e59b0 100644 --- a/edmond/__main__.py +++ b/edmond/__main__.py @@ -1,4 +1,5 @@ import argparse +import random import edmond.bot import edmond.config @@ -10,6 +11,8 @@ def main(): argparser.add_argument("-c", "--config", default="config.json") args = argparser.parse_args() + random.seed() + logger = edmond.log.get_logger(name="edmond") config = edmond.config.load_config(args.config, logger=logger) bot = edmond.bot.Bot(config, logger) diff --git a/edmond/bot.py b/edmond/bot.py index 1727ea3..a946c83 100644 --- a/edmond/bot.py +++ b/edmond/bot.py @@ -16,6 +16,7 @@ class Bot(irc.client.SimpleIRCClient, Logger): self.config = config self.logger = logger self.plugins = [] + self.values = {} @property def nick(self): @@ -27,6 +28,7 @@ class Bot(irc.client.SimpleIRCClient, Logger): def on_welcome(self, connection, event): self.log_i(f"Connected to server {event.source}.") + self.run_plugin_callbacks(event) for channel in self.config["channels"]: connection.join(channel) @@ -74,6 +76,7 @@ class Bot(irc.client.SimpleIRCClient, Logger): class_name = plugin_name.capitalize() + "Plugin" plugin_class = getattr(module, class_name) self.plugins.append(plugin_class(self)) + self.values[plugin_name] = {} self.log_d(f"Loaded {class_name}.") def say(self, target, message): diff --git a/edmond/plugin.py b/edmond/plugin.py index 731b6e6..fd130df 100644 --- a/edmond/plugin.py +++ b/edmond/plugin.py @@ -5,6 +5,7 @@ class Plugin: def __init__(self, bot): self.bot = bot + self.name = self.__class__.__name__.lower()[:-6] # Remove "Plugin". self.config = self.get_config() @property @@ -18,12 +19,34 @@ class Plugin: def get_config(self): """Return the plugin section from the bot config.""" - name = self.__class__.__name__.lower()[:-6] # Remove Plugin suffix. plugins_configs = self.bot.config["plugins"] - return plugins_configs.get(name, {}) + return plugins_configs.get(self.name, {}) + + def get_runtime_value(self, key, plugin_name=None): + """Get a value from the plugin runtime dict.""" + if plugin_name is None: + plugin_name = self.name + return self.bot.values[self.name].get(key) + + def set_runtime_value(self, key, value): + """Set a value in the plugin runtime dict.""" + self.bot.values[self.name][key] = value + + 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() + + for q in self.QUESTIONS: + if question.startswith(q): + self.question = Question(q, question[len(q):].strip()) + return True + return False def should_handle_command(self, message): - """Store Command in object and return True if it should handle it. """ + """Store Command in object and return True if it should handle it.""" command = self.parse_command(message) if command and any(c == command.ident for c in self.COMMANDS): self.command = command @@ -39,6 +62,12 @@ class Plugin: return Command(ident, content) +@dataclass +class Question: + preambule: str + content: str + + @dataclass class Command: ident: str diff --git a/edmond/plugins/horoscope.py b/edmond/plugins/horoscope.py index 42a9e7f..67cb1f9 100644 --- a/edmond/plugins/horoscope.py +++ b/edmond/plugins/horoscope.py @@ -19,3 +19,4 @@ class HoroscopePlugin(Plugin): text = http_get(self.config["url"]) if text: self.bot.say(event.target, text) + return True diff --git a/edmond/plugins/mood.py b/edmond/plugins/mood.py new file mode 100644 index 0000000..a6eeb28 --- /dev/null +++ b/edmond/plugins/mood.py @@ -0,0 +1,31 @@ +import random +from enum import Enum + +from edmond.plugin import Plugin + + +class Mood(Enum): + CALM = 0 + PISSED = 1 + + +class MoodPlugin(Plugin): + + QUESTIONS = ["how are you?"] + + def __init__(self, bot): + super().__init__(bot) + + def on_welcome(self, event): + mood = random.choice(list(Mood)) + self.set_runtime_value("mood", mood) + + def on_pubmsg(self, event): + if not self.should_answer_question(event.arguments[0]): + return False + mood = self.get_runtime_value("mood") + if mood == Mood.CALM: + self.bot.say(event.target, "I'm calm.") + elif mood == Mood.PISSED: + self.bot.say(event.target, "I'm pissed off.") + return True diff --git a/edmond/plugins/opinion.py b/edmond/plugins/opinion.py deleted file mode 100644 index f9ae1e5..0000000 --- a/edmond/plugins/opinion.py +++ /dev/null @@ -1,8 +0,0 @@ -from edmond.plugin import Plugin - - -class OpinionPlugin(Plugin): - - def __init__(self, bot): - super().__init__(bot) -