@@ -25,6 +25,11 @@ class XKCD(commands.Cog):
2525 def __init__ (self , bot : Bot ):
2626 self .bot = bot
2727 self .comics = {}
28+
29+ @commands .Cog .listener ()
30+ async def on_ready (self ):
31+ if not self .update_comics .is_running ():
32+ self .update_comics .start ()
2833
2934 @commands .hybrid_group (help = LONG_HELP_TEXT , brief = SHORT_HELP_TEXT )
3035 async def xkcd (self , ctx : Context , comic_id : int | None = None ):
@@ -78,7 +83,6 @@ async def search(self, ctx: Context, query: str):
7883
7984 return await ctx .reply (ret_str )
8085
81-
8286 async def get_recent_comic (self ) -> int | None :
8387 """gets the most recent comic id"""
8488 xkcd_response = await utils .get_json_from_url ("https://xkcd.com/info.0.json" )
@@ -96,22 +100,31 @@ async def get_all_comics(self) -> dict[int, str] | None:
96100 if https_response .status_code != 200 :
97101 return None
98102
99- html_text = https_response .text
100- lines = [line for line in html_text .splitlines () if line != '' ]
103+ lines = [line for line in https_response .text .splitlines ()]
101104 results = [pattern .findall (item ) for item in lines ]
102105
103106 # flatten results since findall returns list of tuples
104107 results = [match for sub in results for match in sub ]
105108
106109 # Create dictionary from list of tuples
107110 comics = {int (comic_id ): title for comic_id , title in results }
111+
112+ # Add the most recent comic which might not be in the archive page yet
113+ xkcd_response = await utils .get_json_from_url ("https://xkcd.com/info.0.json" )
114+ if xkcd_response :
115+ comics [xkcd_response ["num" ]] = xkcd_response ["safe_title" ]
108116
109117 return comics
110118
111119 @tasks .loop (time = datetime .time (hour = 4 , minute = 0 , tzinfo = datetime .timezone .utc ))
112120 async def update_comics (self ):
113121 """updates the comics dictionary daily"""
114122
123+ # No comics loaded yet, faster way to load all comics
124+ if not self .comics :
125+ self .comics = await self .get_all_comics ()
126+ return
127+
115128 xkcd_response = await utils .get_json_from_url ("https://xkcd.com/info.0.json" )
116129 if not xkcd_response :
117130 return None
0 commit comments