plus: new plugin
This commit is contained in:
parent
bcdf7db830
commit
5caa857df7
|
@ -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",
|
||||
|
|
32
edmond/plugins/plus.py
Normal file
32
edmond/plugins/plus.py
Normal file
|
@ -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
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue