[DadJokes] Handle aiohttp errors and non 200 status code (#253)

This commit is contained in:
PredaaA
2021-10-24 19:02:55 +02:00
committed by GitHub
parent afb8213c99
commit dfe27f0cba

View File

@@ -15,7 +15,12 @@ class DadJokes(commands.Cog):
@commands.command() @commands.command()
async def dadjoke(self, ctx): async def dadjoke(self, ctx):
"""Gets a random dad joke.""" """Gets a random dad joke."""
api = "https://icanhazdadjoke.com/" try:
async with aiohttp.request("GET", api, headers={"Accept": "text/plain"}) as r: async with aiohttp.request("GET", "https://icanhazdadjoke.com/", headers={"Accept": "text/plain"}) as r:
result = await r.text(encoding="UTF-8") if r.status != 200:
await ctx.send(f"`{result}`") return await ctx.send("Oops! Cannot get a dad joke...")
result = await r.text(encoding="UTF-8")
except aiohttp.ClientConnectionError:
return await ctx.send("Oops! Cannot get a dad joke...")
await ctx.send(f"`{result}`")