From 06dfb99635a5f2914aebfcad58d579ec46bd1c68 Mon Sep 17 00:00:00 2001 From: dece Date: Thu, 5 Nov 2020 22:45:21 +0100 Subject: [PATCH] plugin: handle question aliases --- README.md | 2 +- edmond/plugin.py | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1f090bb..87a0490 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Missing features - [ ] Wolframalpha - [x] Youtube: parsing for title, requests for channel or video - [x] Command aliases -- [ ] Question aliases +- [x] Question aliases - [x] Sleep - [x] Various macros: - [x] asks someone to stop doing something diff --git a/edmond/plugin.py b/edmond/plugin.py index c36b30d..8507e83 100644 --- a/edmond/plugin.py +++ b/edmond/plugin.py @@ -159,16 +159,18 @@ class Plugin: # Is it a question I can answer? question = message[len(words[0]):].strip() - for q in self.config.get("questions", []): - if question.startswith(q): - content = question[len(q):].strip() - content = content.rstrip("?").rstrip() - question = Question(q, content) - self.question = question - self.bot.log_d( - f"Answering question from plugin {self.name}: {question}" - ) - return True + for preamble in self.config.get("questions", []): + aliases = self.config.get("aliases", {}).get(preamble, []) + for q in (preamble, *aliases): + if question.startswith(q): + content = question[len(q):].strip() + content = content.rstrip("?").rstrip() + question = Question(preamble, content) + self.question = question + self.bot.log_d( + f"Answering from plugin {self.name}: {question}" + ) + return True return False def should_handle_command( @@ -278,7 +280,7 @@ class Plugin: @dataclass class Question: - preambule: str + preamble: str content: str