plus: new plugin
This commit is contained in:
parent
7964f63513
commit
195d0a38ea
|
@ -147,6 +147,12 @@
|
||||||
"positive": ["I like it."],
|
"positive": ["I like it."],
|
||||||
"negative": ["I don't like it."]
|
"negative": ["I don't like it."]
|
||||||
},
|
},
|
||||||
|
"plus": {
|
||||||
|
"commands": ["plus"],
|
||||||
|
"aliases": {
|
||||||
|
"plus": "more"
|
||||||
|
}
|
||||||
|
},
|
||||||
"randomchoice": {
|
"randomchoice": {
|
||||||
"commands": ["choose"],
|
"commands": ["choose"],
|
||||||
"separator": "or",
|
"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)
|
self.bot.say(event.target, summary)
|
||||||
|
|
||||||
def tell_definition(self, event):
|
def tell_definition(self, event):
|
||||||
summary = ""
|
page = None
|
||||||
|
reply = ""
|
||||||
retries = self.NUM_RETRIES
|
retries = self.NUM_RETRIES
|
||||||
while retries > 0:
|
while retries > 0:
|
||||||
try:
|
try:
|
||||||
summary = wikipedia.summary(self.command.content, sentences=1)
|
page = wikipedia.page(title=self.command.content)
|
||||||
break
|
break
|
||||||
except wikipedia.exceptions.DisambiguationError:
|
except wikipedia.exceptions.DisambiguationError:
|
||||||
summary = self.config["ambiguous_response"]
|
reply = self.config["ambiguous_response"]
|
||||||
break
|
break
|
||||||
except wikipedia.exceptions.PageError:
|
except wikipedia.exceptions.PageError:
|
||||||
summary = self.config["empty_response"]
|
reply = self.config["empty_response"]
|
||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
summary = self.bot.config["error_message"]
|
reply = self.bot.config["error_message"]
|
||||||
# Keep trying after a slight delay.
|
# Keep trying after a slight delay.
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
retries -= 1
|
retries -= 1
|
||||||
if summary:
|
if page:
|
||||||
self.bot.say(event.target, summary)
|
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