throws errors at the user instead of crashes, apart from 2 commands.

This commit is contained in:
Helixu 2021-11-20 16:53:11 +00:00
parent f7068305bb
commit 5290d8c28a

13
bot.py
View file

@ -37,6 +37,7 @@ bot = commands.Bot(command_prefix='gib ')
@bot.command(brief="gives you a fluffy fox") @bot.command(brief="gives you a fluffy fox")
async def fox(ctx): async def fox(ctx):
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get('https://foxrudor.de/') as resp: async with session.get('https://foxrudor.de/') as resp:
file = File(io.BytesIO(await resp.read()),filename='fox.jpg') 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 aiohttp.ClientSession() as session:
async with session.get('https://some-random-api.ml/animal/cat') as resp: async with session.get('https://some-random-api.ml/animal/cat') as resp:
json = await resp.json() 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["fact"])
await ctx.send(json["image"]) await ctx.send(json["image"])
@ -55,6 +59,9 @@ async def panda(ctx):
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get('https://some-random-api.ml/animal/panda') as resp: async with session.get('https://some-random-api.ml/animal/panda') as resp:
json = await resp.json() 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["fact"])
await ctx.send(json["image"]) await ctx.send(json["image"])
@ -64,6 +71,9 @@ async def koala(ctx):
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get('https://some-random-api.ml/animal/koala') as resp: async with session.get('https://some-random-api.ml/animal/koala') as resp:
json = await resp.json() 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["fact"])
await ctx.send(json["image"]) await ctx.send(json["image"])
@ -73,6 +83,9 @@ async def raccoon(ctx):
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get('https://some-random-api.ml/animal/raccoon') as resp: async with session.get('https://some-random-api.ml/animal/raccoon') as resp:
json = await resp.json() 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["fact"])
await ctx.send(json["image"]) await ctx.send(json["image"])