From 98d7b932fb97d6598758ffc6e4b288d29b3d932a Mon Sep 17 00:00:00 2001 From: redcreeper14385 Date: Thu, 22 Jul 2021 11:41:27 +0100 Subject: feat: improve xkcd command, add better error handling --- cogs/fun.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'cogs/fun.py') diff --git a/cogs/fun.py b/cogs/fun.py index be58e08..898b132 100644 --- a/cogs/fun.py +++ b/cogs/fun.py @@ -32,11 +32,18 @@ class Fun(commands.Cog): await ctx.send("https://i.imgur.com/bOLCsd4.png") @commands.command(name="xkcd", brief="Gets XKCD comics.", help="Gets comics from https://xkcd.com, with the given number.") - async def xkcd(self, ctx, comic): - if int(comic) == 0: - await ctx.send("Invalid comic number!") - if int(comic) == 1 and int(comic) != "1": - await ctx.send("Invalid input!") + async def xkcd(self, ctx, comic=None): + if comic is None: + try: + response = requests.get('https://xkcd.com/info.0.json').json() + embed = discord.Embed(title=f"XKCD {response['num']}: {response['safe_title']}", colour=0xa37dca) + embed.set_image(url=response['img']) + embed.set_footer(text=response['alt']) + await ctx.send(embed=embed) + except: + raise commands.CommandInvokeError("Comic not found. The XKCD database may be down.") + elif int(comic) == 0: + raise commands.CommandInvokeError("Invalid comic number!") else: try: response = requests.get(f'https://xkcd.com/{comic}/info.0.json').json() @@ -45,7 +52,7 @@ class Fun(commands.Cog): embed.set_footer(text=response['alt']) await ctx.send(embed=embed) except: - await ctx.send("An error occurred. This is most likely because you entered a number for a comic that doesn't exist.") + raise commands.CommandInvokeError("Comic not found. The XKCD database may be down.") def setup(bot): -- cgit 1.4.1-2-gfad0 >/roster.h
blob: 141b9b20ad3d2bce61118702804d8e98d8ea1b04 (plain) (tree)
1
2
3
4
5
6
7
8
9