diff options
| author | 2019-07-03 07:59:12 +0100 | |
|---|---|---|
| committer | 2019-07-03 07:59:12 +0100 | |
| commit | 6e2259131a69a9ef4bfdbdde2fc163e9c98ef9e0 (patch) | |
| tree | b80c3a948f91f0a6bf25978473229cae5187a79f /modules/define.py | |
| parent | '+= 1' -> '+= value' (diff) | |
| signature | ||
Show an error (instead of throwing) when !define gets a 404
Diffstat (limited to 'modules/define.py')
| -rw-r--r-- | modules/define.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/modules/define.py b/modules/define.py index e7813ccf..0c51490a 100644 --- a/modules/define.py +++ b/modules/define.py @@ -18,7 +18,13 @@ class Module(ModuleManager.BaseModule): "sourceDictionaries": "wiktionary", "api_key": self.bot.config[ "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") def define(self, event): @@ -32,11 +38,11 @@ class Module(ModuleManager.BaseModule): word = event["target"].buffer.get(from_self=False) word = word.replace(" ", "+") - page = self._get_definition(word) - if page: - if len(page.data): - text = utils.http.strip_html(page.data[0]["text"]) - event["stdout"].write("%s: %s" % (page.data[0]["word"], text)) + success, definition = self._get_definition(word) + if success: + if not definition == None: + text = utils.http.strip_html(definition["text"]) + event["stdout"].write("%s: %s" % (definition["word"], text)) else: event["stderr"].write("No definitions found") else: @@ -55,10 +61,8 @@ class Module(ModuleManager.BaseModule): "api_key":self.bot.config["wordnik-api-key"], "min_dictionary_count":1},json=True) if page and len(page.data): - definition = self._get_definition(page.data["word"]) - if definition and len(definition.data): - definition = definition.data[0] - else: + success, definition = self._get_definition(page.data["word"]) + if not success: raise utils.EventError("Try again in a couple of seconds") text = utils.http.strip_html(definition["text"]) |
