diff --git a/config.json.example b/config.json.example index 38bf44a..f5b4363 100644 --- a/config.json.example +++ b/config.json.example @@ -147,6 +147,12 @@ "positive": ["I like it."], "negative": ["I don't like it."] }, + "plus": { + "commands": ["plus"], + "aliases": { + "plus": "more" + } + }, "randomchoice": { "commands": ["choose"], "separator": "or", diff --git a/edmond/plugins/plus.py b/edmond/plugins/plus.py new file mode 100644 index 0000000..a65a791 --- /dev/null +++ b/edmond/plugins/plus.py @@ -0,0 +1,32 @@ +from edmond.plugin import Plugin + + +class PlusPlugin(Plugin): + """Plugin to store additional content from other plugins. + + This plugin does not do much on its own, it lets other plugins register additional + content to compute on the fly when asked, e.g. the Wikipedia plugin can store the + Web page of the definition they just gave so users wanting to know more about a + definition can use the "plus" function to get the URL to the Web page. + + There can be only one handle registered at one time by target (so by channel, user, + etc). External plugins use the `add_handler` to set the current handler for a + target. + """ + + def __init__(self, bot): + super().__init__(bot) + self.handlers = {} + + def on_pubmsg(self, event): + if not self.should_handle_command(event.arguments[0], no_content=True): + return False + + if handler := self.handlers.get(event.target): + handler(event) + else: + self.signal_failure(event.target) + return True + + def add_handler(self, target: str, handler): + self.handlers[target] = handler diff --git a/edmond/plugins/wikipedia.py b/edmond/plugins/wikipedia.py index daf4b68..c8e8154 100644 --- a/edmond/plugins/wikipedia.py +++ b/edmond/plugins/wikipedia.py @@ -51,22 +51,30 @@ class WikipediaPlugin(Plugin): self.bot.say(event.target, summary) def tell_definition(self, event): - summary = "" + page = None + reply = "" retries = self.NUM_RETRIES while retries > 0: try: - summary = wikipedia.summary(self.command.content, sentences=1) + page = wikipedia.page(title=self.command.content) break except wikipedia.exceptions.DisambiguationError: - summary = self.config["ambiguous_response"] + reply = self.config["ambiguous_response"] break except wikipedia.exceptions.PageError: - summary = self.config["empty_response"] + reply = self.config["empty_response"] break except: - summary = self.bot.config["error_message"] + reply = self.bot.config["error_message"] # Keep trying after a slight delay. time.sleep(1) retries -= 1 - if summary: - self.bot.say(event.target, summary) + if page: + reply = page.summary.split(". ", maxsplit=1)[0] + if len(reply) > 200: + reply = reply[:196] + " […]" + if plus_plugin := self.bot.get_plugin("plus"): + def handler(plus_event): + self.bot.say(plus_event.target, page.url) + plus_plugin.add_handler(event.target, handler) + self.bot.say(event.target, reply)