Show an error (instead of throwing) when !define gets a 404

This commit is contained in:
jesopo 2019-07-03 07:59:12 +01:00
parent e19638827e
commit 6e2259131a

View file

@ -18,7 +18,13 @@ class Module(ModuleManager.BaseModule):
"sourceDictionaries": "wiktionary", "api_key": self.bot.config[ "sourceDictionaries": "wiktionary", "api_key": self.bot.config[
"wordnik-api-key"]}, json=True) "wordnik-api-key"]}, json=True)
return page if page:
if page.code == 200:
return True, page.data[0]
else:
return True, None
else:
return False, None
@utils.hook("received.command.define") @utils.hook("received.command.define")
def define(self, event): def define(self, event):
@ -32,11 +38,11 @@ class Module(ModuleManager.BaseModule):
word = event["target"].buffer.get(from_self=False) word = event["target"].buffer.get(from_self=False)
word = word.replace(" ", "+") word = word.replace(" ", "+")
page = self._get_definition(word) success, definition = self._get_definition(word)
if page: if success:
if len(page.data): if not definition == None:
text = utils.http.strip_html(page.data[0]["text"]) text = utils.http.strip_html(definition["text"])
event["stdout"].write("%s: %s" % (page.data[0]["word"], text)) event["stdout"].write("%s: %s" % (definition["word"], text))
else: else:
event["stderr"].write("No definitions found") event["stderr"].write("No definitions found")
else: else:
@ -55,10 +61,8 @@ class Module(ModuleManager.BaseModule):
"api_key":self.bot.config["wordnik-api-key"], "api_key":self.bot.config["wordnik-api-key"],
"min_dictionary_count":1},json=True) "min_dictionary_count":1},json=True)
if page and len(page.data): if page and len(page.data):
definition = self._get_definition(page.data["word"]) success, definition = self._get_definition(page.data["word"])
if definition and len(definition.data): if not success:
definition = definition.data[0]
else:
raise utils.EventError("Try again in a couple of seconds") raise utils.EventError("Try again in a couple of seconds")
text = utils.http.strip_html(definition["text"]) text = utils.http.strip_html(definition["text"])