From aedfcd995624d605f58c9d3b2ebb42f3c43ffa65 Mon Sep 17 00:00:00 2001 From: Phxntxm Date: Mon, 10 Oct 2016 22:46:32 -0500 Subject: [PATCH] Added a couple extra error checks to urban dictionary lookup --- cogs/links.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cogs/links.py b/cogs/links.py index 43d4b4a..e0fc4b0 100644 --- a/cogs/links.py +++ b/cogs/links.py @@ -82,19 +82,21 @@ class Links: """Pulls the top urbandictionary.com definition for a term""" url = "http://api.urbandictionary.com/v0/define" params = {"term": msg} - async with self.session.get(url, params=params, headers=self.headers) as r: - data = await r.json() - - # Urban dictionary has some long definitions, some might not be able to be sent try: + async with self.session.get(url, params=params, headers=self.headers) as r: + data = await r.json() + # List is the list of definitions found, if it's empty then nothing was found if len(data['list']) == 0: await self.bot.say("No result with that term!") # If the list is not empty, use the first result and print it's defintion else: await self.bot.say(data['list'][0]['definition']) + # Urban dictionary has some long definitions, some might not be able to be sent except discord.HTTPException: await self.bot.say('```Error: Definition is too long for me to send```') + except (json.JSONDecodeError, KeyError): + await self.bot.say("Sorry but I failed to connect to urban dictionary!") @commands.command(pass_context=True) @checks.custom_perms(send_messages=True)