diff options
author | 2021-06-03 15:58:05 +0800 | |
---|---|---|
committer | 2021-06-03 15:58:05 +0800 | |
commit | b60fd23bae52fb59eed0bf29aef2516234d1c858 (patch) | |
tree | 0ed0f44d590e8e4891b2f1ab142b10b48fae2763 /bot/exts/evergreen/status_codes.py | |
parent | Make `setup` docstring same as other cogs (diff) | |
parent | Added "centisecond" in trivia_quiz.json (#751) (diff) |
Merge branch 'main' into feature/pride-leaders
Diffstat (limited to 'bot/exts/evergreen/status_codes.py')
-rw-r--r-- | bot/exts/evergreen/status_codes.py | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index 874c87eb..181c71ce 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -1,28 +1,35 @@ from http import HTTPStatus +from random import choice import discord from discord.ext import commands +from bot.bot import Bot + 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.""" + """ + Fetch an image depicting HTTP status codes as a dog or a cat. + + If neither animal is selected a cat or dog is chosen randomly for the given status code. + """ - def __init__(self, bot: commands.Bot): + def __init__(self, bot: 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) + @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)) + await subcmd(ctx, code) - @http_status_group.command(name='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}**') + embed = discord.Embed(title=f"**Status: {code}**") url = HTTP_CAT_URL.format(code=code) try: @@ -34,18 +41,23 @@ class HTTPStatusCodes(commands.Cog): raise NotImplementedError except ValueError: - embed.set_footer(text='Inputted status code does not exist.') + 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.') + embed.set_footer(text="Inputted status code is not implemented by http.cat yet.") finally: await ctx.send(embed=embed) - @http_status_group.command(name='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}**') + # These codes aren't server-friendly. + if code in (304, 422): + await self.http_cat(ctx, code) + return + + embed = discord.Embed(title=f"**Status: {code}**") url = HTTP_DOG_URL.format(code=code) try: @@ -57,15 +69,15 @@ class HTTPStatusCodes(commands.Cog): raise NotImplementedError except ValueError: - embed.set_footer(text='Inputted status code does not exist.') + 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.') + embed.set_footer(text="Inputted status code is not implemented by httpstatusdogs.com yet.") finally: await ctx.send(embed=embed) -def setup(bot: commands.Bot) -> None: +def setup(bot: Bot) -> None: """Load the HTTPStatusCodes cog.""" bot.add_cog(HTTPStatusCodes(bot)) |