wikipedia: fix crash

This commit is contained in:
dece 2022-09-04 15:27:13 +02:00
parent e8a18c6a90
commit 8d9ccd8dc9
2 changed files with 13 additions and 11 deletions

View file

@ -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.
Initalisation should be very fast, no network connections or anything. They Initialisation should be very fast, no network connections or anything.
are initialised before connecting to the server, so their `is_ready` flag They are initialised before connecting to the server, so their `is_ready`
is set at that point. The loading order is more or less random, so a plugin flag is set at that point. The loading order is more or less random, so a
cannot assume another has been loaded during initialisation. If it wants to plugin cannot assume another has been loaded during initialisation. If it
interact with another plugin, the earliest point to do that is in the wants to interact with another plugin, the earliest point to do that is in
on_welcome callback which is called after connecting to a server, and can the on_welcome callback which is called after connecting to a server, and
disable itself by setting its own `is_ready` flag to false. can 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

View file

@ -38,14 +38,16 @@ class WikipediaPlugin(Plugin):
return True return True
def tell_random_summary(self, event): def tell_random_summary(self, event):
summary = "" page = None
retries = self.NUM_RETRIES retries = self.NUM_RETRIES
while retries > 0: while retries > 0:
try: try:
page = wikipedia.page(title=wikipedia.random(), sentences=1) page = wikipedia.page(title=wikipedia.random())
break break
except: # The wikipedia package can raise a lot of different stuff. except Exception as exc:
pass # The wikipedia package can raise a lot of different stuff,
# 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"):