11from http import HTTPStatus
22from random import choice
3+ from typing import Literal
34
45import disnake
56from disnake .ext import commands
67
78from monty .bot import Monty
89
910
10- HTTP_DOG_URL = "https://httpstatusdogs.com/img/{code}.jpg"
11- HTTP_CAT_URL = "https://http.cat/{code}.jpg"
11+ # why must every single 304 be a large penis
12+ HTTP_URLS : dict [str , list [tuple [str , tuple [int , ...]]]] = {
13+ "cat" : [
14+ ("https://http.cat/{code}.jpg" , ()),
15+ ("https://httpcats.com/{code}.jpg" , ()),
16+ ],
17+ "dog" : [
18+ ("https://http.dog/{code}.jpg" , (304 ,)),
19+ ("https://httpstatusdogs.com/img/{code}.jpg" , (304 , 308 , 422 )),
20+ ],
21+ "goat" : [
22+ ("https://httpgoats.com/{code}.jpg" , (304 , 422 )),
23+ ],
24+ }
1225
1326
1427class HTTPStatusCodes (commands .Cog , name = "HTTP Status Codes" ):
1528 """
16- Fetch an image depicting HTTP status codes as a dog or a cat.
29+ Fetch an image depicting HTTP status codes as a dog or a cat or as goat .
1730
18- If neither animal is selected a cat or dog is chosen randomly for the given status code.
31+ If neither animal is selected a cat or dog or goat is chosen randomly for the given status code.
1932 """
2033
2134 def __init__ (self , bot : Monty ) -> None :
@@ -27,60 +40,60 @@ def __init__(self, bot: Monty) -> None:
2740 invoke_without_command = True ,
2841 )
2942 async def http_status_group (self , ctx : commands .Context , code : int ) -> None :
30- """Choose a cat or dog randomly for the given status code."""
31- subcmd = choice ((self .http_cat , self .http_dog ))
43+ """Choose an animal randomly for the given status code."""
44+ subcmd = choice ((self .http_cat , self .http_dog , self . http_goat ))
3245 await subcmd (ctx , code )
3346
34- @http_status_group .command (name = "cat" )
35- async def http_cat (self , ctx : commands .Context , code : int ) -> None :
36- """Sends an embed with an image of a cat, portraying the status code."""
37- embed = disnake .Embed (title = f"**Status: { code } **" )
38- url = HTTP_CAT_URL .format (code = code )
47+ async def _fetcher (
48+ self ,
49+ ctx : commands .Context ,
50+ animal : Literal ["cat" , "dog" , "goat" ],
51+ code : int ,
52+ ) -> None :
53+ url , ignored_codes = choice (HTTP_URLS [animal ])
54+ if code in ignored_codes :
55+ # check the other urls for the animal
56+ for url , ignored_codes in HTTP_URLS [animal ]:
57+ if code not in ignored_codes :
58+ url = url
59+ break
60+ else :
61+ await ctx .send (f"The { animal } does not have an image for status code { code } ." )
62+ return
3963
64+ embed = disnake .Embed (title = f"**Status: { code } **" )
65+ url = url .format (code = code )
4066 try :
4167 HTTPStatus (code )
4268 async with self .bot .http_session .get (url , allow_redirects = False ) as response :
43- if response .status != 404 :
69+ if response .status == 200 :
4470 embed .set_image (url = url )
4571 else :
4672 raise NotImplementedError
73+ embed .set_footer (text = f"Powered by { response .url .host } " )
4774
4875 except ValueError :
4976 embed .set_footer (text = "Inputted status code does not exist." )
5077
5178 except NotImplementedError :
52- embed .set_footer (text = "Inputted status code is not implemented by http.cat yet." )
79+ embed .set_footer (text = f"Inputted status code is not implemented by { response .url .host } yet." )
80+
81+ await ctx .send (embed = embed )
5382
54- finally :
55- await ctx .send (embed = embed )
83+ @http_status_group .command (name = "cat" )
84+ async def http_cat (self , ctx : commands .Context , code : int ) -> None :
85+ """Sends an embed with an image of a cat, portraying the status code."""
86+ await self ._fetcher (ctx , "cat" , code )
5687
5788 @http_status_group .command (name = "dog" )
5889 async def http_dog (self , ctx : commands .Context , code : int ) -> None :
5990 """Sends an embed with an image of a dog, portraying the status code."""
60- # These codes aren't server-friendly.
61- if code in (304 , 422 ):
62- await self .http_cat (ctx , code )
63- return
64-
65- embed = disnake .Embed (title = f"**Status: { code } **" )
66- url = HTTP_DOG_URL .format (code = code )
67-
68- try :
69- HTTPStatus (code )
70- async with self .bot .http_session .get (url , allow_redirects = False ) as response :
71- if response .status != 302 :
72- embed .set_image (url = url )
73- else :
74- raise NotImplementedError
75-
76- except ValueError :
77- embed .set_footer (text = "Inputted status code does not exist." )
78-
79- except NotImplementedError :
80- embed .set_footer (text = "Inputted status code is not implemented by httpstatusdogs.com yet." )
91+ await self ._fetcher (ctx , "dog" , code )
8192
82- finally :
83- await ctx .send (embed = embed )
93+ @http_status_group .command (name = "goat" )
94+ async def http_goat (self , ctx : commands .Context , code : int ) -> None :
95+ """Sends an embed with an image of a goat, portraying the status code."""
96+ await self ._fetcher (ctx , "goat" , code )
8497
8598
8699def setup (bot : Monty ) -> None :
0 commit comments