[DadJokes] Handle aiohttp errors and non 200 status code (#253)
This commit is contained in:
@@ -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}`")
|
||||||
|
|||||||
Reference in New Issue
Block a user