Compare commits
No commits in common. "ae21c1ff8be212a92c7bb62434f12e13ce87597f" and "e30779cc1365dc756958dc7f325e7ebae3f863c6" have entirely different histories.
ae21c1ff8b
...
e30779cc13
|
@ -151,8 +151,7 @@
|
||||||
"commands": ["plus"],
|
"commands": ["plus"],
|
||||||
"aliases": {
|
"aliases": {
|
||||||
"plus": "more"
|
"plus": "more"
|
||||||
},
|
}
|
||||||
"shortcut": "+"
|
|
||||||
},
|
},
|
||||||
"randomchoice": {
|
"randomchoice": {
|
||||||
"commands": ["choose"],
|
"commands": ["choose"],
|
||||||
|
|
|
@ -37,13 +37,13 @@ class Plugin:
|
||||||
plugins. It can also save data using the Bot's storage feature to be
|
plugins. It can also save data using the Bot's storage feature to be
|
||||||
available after a restart.
|
available after a restart.
|
||||||
|
|
||||||
Initialisation should be very fast, no network connections or anything.
|
Initalisation should be very fast, no network connections or anything. They
|
||||||
They are initialised before connecting to the server, so their `is_ready`
|
are initialised before connecting to the server, so their `is_ready` flag
|
||||||
flag is set at that point. The loading order is more or less random, so a
|
is set at that point. The loading order is more or less random, so a plugin
|
||||||
plugin cannot assume another has been loaded during initialisation. If it
|
cannot assume another has been loaded during initialisation. If it wants to
|
||||||
wants to interact with another plugin, the earliest point to do that is in
|
interact with another plugin, the earliest point to do that is in the
|
||||||
the on_welcome callback which is called after connecting to a server, and
|
on_welcome callback which is called after connecting to a server, and can
|
||||||
can disable itself by setting its own `is_ready` flag to false.
|
disable itself by setting its own `is_ready` flag to false.
|
||||||
|
|
||||||
A plugin can access its config once the base `__init__` has been called.
|
A plugin can access its config once the base `__init__` has been called.
|
||||||
The configuration is valid only is `is_ready` is True, else accessing its
|
The configuration is valid only is `is_ready` is True, else accessing its
|
||||||
|
|
|
@ -4,38 +4,29 @@ from edmond.plugin import Plugin
|
||||||
class PlusPlugin(Plugin):
|
class PlusPlugin(Plugin):
|
||||||
"""Plugin to store additional content from other plugins.
|
"""Plugin to store additional content from other plugins.
|
||||||
|
|
||||||
This plugin does not do much on its own, it lets other plugins register
|
This plugin does not do much on its own, it lets other plugins register additional
|
||||||
additional content to compute on the fly when asked, e.g. the Wikipedia
|
content to compute on the fly when asked, e.g. the Wikipedia plugin can store the
|
||||||
plugin can store the Web page of the definition they just gave so users
|
Web page of the definition they just gave so users wanting to know more about a
|
||||||
wanting to know more about a definition can use the "plus" function to get
|
definition can use the "plus" function to get the URL to the Web page.
|
||||||
the URL to the Web page.
|
|
||||||
|
|
||||||
There can be only one handle registered at one time by target (so by
|
There can be only one handle registered at one time by target (so by channel, user,
|
||||||
channel, user, etc). External plugins use the `add_handler` to set the
|
etc). External plugins use the `add_handler` to set the current handler for a
|
||||||
current handler for a target.
|
target.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
REQUIRED_CONFIGS = ["commands"]
|
|
||||||
|
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
super().__init__(bot)
|
super().__init__(bot)
|
||||||
self.handlers = {}
|
self.handlers = {}
|
||||||
self.shortcut = self.config.get("shortcut")
|
|
||||||
|
|
||||||
def on_pubmsg(self, event):
|
def on_pubmsg(self, event):
|
||||||
if (
|
if not self.should_handle_command(event.arguments[0], no_content=True):
|
||||||
(self.shortcut and event.arguments[0].strip() == self.shortcut)
|
|
||||||
or self.should_handle_command(event.arguments[0], no_content=True)
|
|
||||||
):
|
|
||||||
self.process_handler(event)
|
|
||||||
return True
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def process_handler(self, event):
|
|
||||||
if handler := self.handlers.pop(event.target, None):
|
if handler := self.handlers.pop(event.target, None):
|
||||||
handler(event)
|
handler(event)
|
||||||
else:
|
else:
|
||||||
self.signal_failure(event.target)
|
self.signal_failure(event.target)
|
||||||
|
return True
|
||||||
|
|
||||||
def add_handler(self, target: str, handler):
|
def add_handler(self, target: str, handler):
|
||||||
self.handlers[target] = handler
|
self.handlers[target] = handler
|
||||||
|
|
|
@ -38,16 +38,14 @@ class WikipediaPlugin(Plugin):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def tell_random_summary(self, event):
|
def tell_random_summary(self, event):
|
||||||
page = None
|
summary = ""
|
||||||
retries = self.NUM_RETRIES
|
retries = self.NUM_RETRIES
|
||||||
while retries > 0:
|
while retries > 0:
|
||||||
try:
|
try:
|
||||||
page = wikipedia.page(title=wikipedia.random())
|
page = wikipedia.page(title=wikipedia.random(), sentences=1)
|
||||||
break
|
break
|
||||||
except Exception as exc:
|
except: # The wikipedia package can raise a lot of different stuff.
|
||||||
# The wikipedia package can raise a lot of different stuff,
|
pass
|
||||||
# so we sort of have to catch broadly.
|
|
||||||
self.bot.log_d(f"Wikipedia exception: {exc}")
|
|
||||||
retries -= 1
|
retries -= 1
|
||||||
if page:
|
if page:
|
||||||
if plus_plugin := self.bot.get_plugin("plus"):
|
if plus_plugin := self.bot.get_plugin("plus"):
|
||||||
|
|
Loading…
Reference in a new issue