From 661ab2cc966c96d6f05dfc0444f7569b2a8fd727 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Thu, 28 Jan 2021 04:44:36 +0530 Subject: Add http status dog and add http_cat to group http_status --- bot/exts/evergreen/status_codes.py | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 bot/exts/evergreen/status_codes.py (limited to 'bot/exts/evergreen/status_codes.py') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py new file mode 100644 index 00000000..eebc23af --- /dev/null +++ b/bot/exts/evergreen/status_codes.py @@ -0,0 +1,63 @@ +import re +from http import HTTPStatus + +import discord +from discord import HTTPException +from discord.ext import commands + + +class StatusCats(commands.Cog): + """Commands that give HTTP statuses described and visualized by cats and dogs.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.group(name="http_status", aliases=("status", "httpstatus")) + async def http_status_group(self, ctx: commands.Context) -> None: + """Group containing dog and cat http status code commands.""" + if not ctx.invoked_subcommand: + await ctx.send_help(ctx.command) + + @http_status_group.command(aliases=['cat']) + async def http_cat(self, ctx: commands.Context, code: int) -> None: + """Sends an embed with an image of a cat, portraying the status code.""" + embed = discord.Embed(title=f'**Status: {code}**') + + try: + HTTPStatus(code) + + except ValueError: + embed.set_footer(text='Inputted status code does not exist.') + + else: + embed.set_image(url=f'https://http.cat/{code}.jpg') + + finally: + await ctx.send(embed=embed) + + @http_status_group.command(aliases=['dog']) + async def http_dog(self, ctx: commands.Context, code: int) -> None: + """Sends an embed with an image of a dog, portraying the status code.""" + embed = discord.Embed(title=f'**Status: {code}**') + + try: + HTTPStatus(code) + async with self.bot.http_session.get( + f'https://httpstatusdogs.com/img/{code}.jpg', + allow_redirects=False + ) as response: + if response.status != 302: + embed.set_image(url=f'https://httpstatusdogs.com/img/{code}.jpg') + else: + raise ValueError + + except ValueError: + embed.set_footer(text='Inputted status code does not exist.') + + finally: + await ctx.send(embed=embed) + + +def setup(bot: commands.Bot) -> None: + """Load the StatusCats cog.""" + bot.add_cog(StatusCats(bot)) -- cgit v1.2.3 From f5eae5bec548bc1e450af7f07387531b80869f84 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sun, 31 Jan 2021 05:26:23 +0530 Subject: Remove unused imports --- bot/exts/evergreen/status_codes.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'bot/exts/evergreen/status_codes.py') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index eebc23af..473e778c 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -1,8 +1,6 @@ -import re from http import HTTPStatus import discord -from discord import HTTPException from discord.ext import commands -- cgit v1.2.3 From c87f6e0561bbeb6510292a0b09a829aa6fa290a7 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 1 Feb 2021 05:21:32 +0530 Subject: rename the cog to HTTPStatusCodes --- bot/exts/evergreen/status_codes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/exts/evergreen/status_codes.py') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index 473e778c..19375657 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -4,7 +4,7 @@ import discord from discord.ext import commands -class StatusCats(commands.Cog): +class HTTPStatusCodes(commands.Cog): """Commands that give HTTP statuses described and visualized by cats and dogs.""" def __init__(self, bot: commands.Bot): @@ -57,5 +57,5 @@ class StatusCats(commands.Cog): def setup(bot: commands.Bot) -> None: - """Load the StatusCats cog.""" - bot.add_cog(StatusCats(bot)) + """Load the HTTPStatusCodes cog.""" + bot.add_cog(HTTPStatusCodes(bot)) -- cgit v1.2.3 From e32182ee7000cea8a49d41605bb01563c0d68800 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 1 Feb 2021 05:28:35 +0530 Subject: Add similar repsonse.status check to too, and instead of ValueError raise NotImplemented error if the status is not implemented yet but is a valid status code --- bot/exts/evergreen/status_codes.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'bot/exts/evergreen/status_codes.py') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index 19375657..bfc4bb1a 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -23,13 +23,18 @@ class HTTPStatusCodes(commands.Cog): try: HTTPStatus(code) + async with self.bot.http_session.get( + f'https://http.cat/{code}.jpg', + allow_redirects=False + ) as response: + if response.status != 404: + embed.set_image(url=f'https://http.cat/{code}.jpg') + else: + raise NotImplementedError except ValueError: embed.set_footer(text='Inputted status code does not exist.') - else: - embed.set_image(url=f'https://http.cat/{code}.jpg') - finally: await ctx.send(embed=embed) @@ -47,11 +52,14 @@ class HTTPStatusCodes(commands.Cog): if response.status != 302: embed.set_image(url=f'https://httpstatusdogs.com/img/{code}.jpg') else: - raise ValueError + raise NotImplementedError except ValueError: embed.set_footer(text='Inputted status code does not exist.') + except NotImplementedError: + embed.set_footer(text='Inputted status code is not implemented by httpstatusdogs.com yet.') + finally: await ctx.send(embed=embed) -- cgit v1.2.3 From aee64f82f5a97a4bdd32c6d6d66f6f0eb8ef51f8 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 2 Feb 2021 05:23:34 +0530 Subject: Add not Implemented error handler for .status cat command --- bot/exts/evergreen/status_codes.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'bot/exts/evergreen/status_codes.py') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index bfc4bb1a..ff2ac9e1 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -35,6 +35,9 @@ class HTTPStatusCodes(commands.Cog): except ValueError: embed.set_footer(text='Inputted status code does not exist.') + except NotImplementedError: + embed.set_footer(text='Inputted status code is not implemented by http.cat yet.') + finally: await ctx.send(embed=embed) -- cgit v1.2.3 From 5e55ea1dbfd652c10999c88d1c32aebd1be768f0 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Thu, 4 Feb 2021 05:23:54 +0530 Subject: Make use of constants in the url --- bot/exts/evergreen/status_codes.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'bot/exts/evergreen/status_codes.py') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index ff2ac9e1..874c87eb 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -3,6 +3,9 @@ from http import HTTPStatus import discord from discord.ext import commands +HTTP_DOG_URL = "https://httpstatusdogs.com/img/{code}.jpg" +HTTP_CAT_URL = "https://http.cat/{code}.jpg" + class HTTPStatusCodes(commands.Cog): """Commands that give HTTP statuses described and visualized by cats and dogs.""" @@ -16,19 +19,17 @@ class HTTPStatusCodes(commands.Cog): if not ctx.invoked_subcommand: await ctx.send_help(ctx.command) - @http_status_group.command(aliases=['cat']) + @http_status_group.command(name='cat') async def http_cat(self, ctx: commands.Context, code: int) -> None: """Sends an embed with an image of a cat, portraying the status code.""" embed = discord.Embed(title=f'**Status: {code}**') + url = HTTP_CAT_URL.format(code=code) try: HTTPStatus(code) - async with self.bot.http_session.get( - f'https://http.cat/{code}.jpg', - allow_redirects=False - ) as response: + async with self.bot.http_session.get(url, allow_redirects=False) as response: if response.status != 404: - embed.set_image(url=f'https://http.cat/{code}.jpg') + embed.set_image(url=url) else: raise NotImplementedError @@ -41,19 +42,17 @@ class HTTPStatusCodes(commands.Cog): finally: await ctx.send(embed=embed) - @http_status_group.command(aliases=['dog']) + @http_status_group.command(name='dog') async def http_dog(self, ctx: commands.Context, code: int) -> None: """Sends an embed with an image of a dog, portraying the status code.""" embed = discord.Embed(title=f'**Status: {code}**') + url = HTTP_DOG_URL.format(code=code) try: HTTPStatus(code) - async with self.bot.http_session.get( - f'https://httpstatusdogs.com/img/{code}.jpg', - allow_redirects=False - ) as response: + async with self.bot.http_session.get(url, allow_redirects=False) as response: if response.status != 302: - embed.set_image(url=f'https://httpstatusdogs.com/img/{code}.jpg') + embed.set_image(url=url) else: raise NotImplementedError -- cgit v1.2.3