diff options
author | 2021-09-03 16:28:34 +0100 | |
---|---|---|
committer | 2021-09-03 16:28:34 +0100 | |
commit | 3c11c48bc83dae608104392aeec20181352dde1f (patch) | |
tree | 4264ddbb25e86184255574cfd0e8fa9bb11d7bcb | |
parent | Merge pull request #839 from python-discord/android-codeblock-fix (diff) | |
parent | Merge branch 'main' into teapot-support (diff) |
Merge pull request #833 from brad90four/teapot-support
Teapot support - Continues PR #610
-rw-r--r-- | bot/exts/evergreen/status_codes.py | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index 181c71ce..501cbe0a 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -1,4 +1,3 @@ -from http import HTTPStatus from random import choice import discord @@ -8,6 +7,13 @@ from bot.bot import Bot HTTP_DOG_URL = "https://httpstatusdogs.com/img/{code}.jpg" HTTP_CAT_URL = "https://http.cat/{code}.jpg" +STATUS_TEMPLATE = "**Status: {code}**" +ERR_404 = "Unable to find status floof for {code}." +ERR_UNKNOWN = "Error attempting to retrieve status floof for {code}." +ERROR_LENGTH_EMBED = discord.Embed( + title="Input status code does not exist", + description="The range of valid status codes is 100 to 599", +) class HTTPStatusCodes(commands.Cog): @@ -20,7 +26,11 @@ class HTTPStatusCodes(commands.Cog): def __init__(self, bot: Bot): self.bot = bot - @commands.group(name="http_status", aliases=("status", "httpstatus"), invoke_without_command=True) + @commands.group( + name="http_status", + aliases=("status", "httpstatus"), + invoke_without_command=True, + ) async def http_status_group(self, ctx: commands.Context, code: int) -> None: """Choose a cat or dog randomly for the given status code.""" subcmd = choice((self.http_cat, self.http_dog)) @@ -28,54 +38,48 @@ class HTTPStatusCodes(commands.Cog): @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(url, allow_redirects=False) as response: - if response.status != 404: - embed.set_image(url=url) - else: - 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 http.cat yet.") - - finally: - await ctx.send(embed=embed) + """Send a cat version of the requested HTTP status code.""" + if code in range(100, 600): + await self.build_embed(url=HTTP_CAT_URL.format(code=code), ctx=ctx, code=code) + return + await ctx.send(embed=ERROR_LENGTH_EMBED) @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.""" - # These codes aren't server-friendly. - if code in (304, 422): - await self.http_cat(ctx, code) + """Send a dog version of the requested HTTP status code.""" + if code in range(100, 600): + await self.build_embed(url=HTTP_DOG_URL.format(code=code), ctx=ctx, code=code) return - - embed = discord.Embed(title=f"**Status: {code}**") - url = HTTP_DOG_URL.format(code=code) - - try: - HTTPStatus(code) - async with self.bot.http_session.get(url, allow_redirects=False) as response: - if response.status != 302: - embed.set_image(url=url) - else: - 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) + await ctx.send(embed=ERROR_LENGTH_EMBED) + + async def build_embed(self, url: str, ctx: commands.Context, code: int) -> None: + """Attempt to build and dispatch embed. Append error message instead if something goes wrong.""" + async with self.bot.http_session.get(url, allow_redirects=False) as response: + if response.status in range(200, 300): + await ctx.send( + embed=discord.Embed( + title=STATUS_TEMPLATE.format(code=code) + ).set_image(url=url) + ) + elif response.status in (302, 404): # dog URL returns 302 instead of 404 + if "dog" in url: + await ctx.send( + embed=discord.Embed( + title=ERR_404.format(code=code) + ).set_image(url="https://httpstatusdogs.com/img/404.jpg") + ) + return + await ctx.send( + embed=discord.Embed( + title=ERR_404.format(code=code) + ).set_image(url="https://http.cat/404.jpg") + ) + else: + await ctx.send( + embed=discord.Embed( + title=STATUS_TEMPLATE.format(code=code) + ).set_footer(text=ERR_UNKNOWN.format(code=code)) + ) def setup(bot: Bot) -> None: |