about summary refs log tree commit diff stats
path: root/cogs/fun.py
diff options
context:
space:
mode:
authorredcreeper14385 <mounderfod@gmail.com>2021-07-22 11:41:27 +0100
committerredcreeper14385 <mounderfod@gmail.com>2021-07-22 11:42:22 +0100
commit98d7b932fb97d6598758ffc6e4b288d29b3d932a (patch)
treecf91aa0206192859a9b6bcee253c360a19cc9c83 /cogs/fun.py
parentd0f50b38f4f2cbdacff647853acd4aee3d0201ee (diff)
downloadtiny-potato-bot-98d7b932fb97d6598758ffc6e4b288d29b3d932a.tar.gz
feat: improve xkcd command, add better error handling
Diffstat (limited to 'cogs/fun.py')
-rw-r--r--cogs/fun.py19
1 files changed, 13 insertions, 6 deletions
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):