From 5290d8c28a1b7cd72df2b83e8d93d9a50dc2eaa5 Mon Sep 17 00:00:00 2001 From: Helixu Date: Sat, 20 Nov 2021 16:53:11 +0000 Subject: [PATCH] throws errors at the user instead of crashes, apart from 2 commands. --- bot.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bot.py b/bot.py index f723bf8..5046047 100755 --- a/bot.py +++ b/bot.py @@ -37,6 +37,7 @@ bot = commands.Bot(command_prefix='gib ') @bot.command(brief="gives you a fluffy fox") async def fox(ctx): + async with aiohttp.ClientSession() as session: async with session.get('https://foxrudor.de/') as resp: file = File(io.BytesIO(await resp.read()),filename='fox.jpg') @@ -47,6 +48,9 @@ async def cat(ctx): async with aiohttp.ClientSession() as session: async with session.get('https://some-random-api.ml/animal/cat') as resp: json = await resp.json() + error = json.get('error') + if error: + return await ctx.send(f'Received unexpected error, unclear instructions, got stuck in toaster! ({error})') await ctx.send(json["fact"]) await ctx.send(json["image"]) @@ -55,6 +59,9 @@ async def panda(ctx): async with aiohttp.ClientSession() as session: async with session.get('https://some-random-api.ml/animal/panda') as resp: json = await resp.json() + error = json.get('error') + if error: + return await ctx.send(f'Received unexpected error, thats not good! ({error})') await ctx.send(json["fact"]) await ctx.send(json["image"]) @@ -64,6 +71,9 @@ async def koala(ctx): async with aiohttp.ClientSession() as session: async with session.get('https://some-random-api.ml/animal/koala') as resp: json = await resp.json() + error = json.get('error') + if error: + return await ctx.send(f'Received unexpected error, try our sister game, Minceraft! ({error})') await ctx.send(json["fact"]) await ctx.send(json["image"]) @@ -73,6 +83,9 @@ async def raccoon(ctx): async with aiohttp.ClientSession() as session: async with session.get('https://some-random-api.ml/animal/raccoon') as resp: json = await resp.json() + error = json.get('error') + if error: + return await ctx.send(f'Received unexpected error, blame ginlang! ({error})') await ctx.send(json["fact"]) await ctx.send(json["image"])