1- # Adventure command from Python bot.
1+ # Adventure command from Python bot.
22import asyncio
3- from contextlib import suppress
43import json
4+ from contextlib import suppress
55from pathlib import Path
6- from typing import Literal , NamedTuple , TypedDict , Union
6+ from typing import Literal , NamedTuple , TypedDict
77
88from discord import Embed , HTTPException , Message , Reaction , User
99from discord .ext import commands
@@ -26,9 +26,7 @@ class Cog(NamedTuple):
2626
2727
2828class GameInfo (TypedDict ):
29- """
30- A dictionary containing the game information. Used in `available_games.json`.
31- """
29+ """A dictionary containing the game information. Used in `available_games.json`."""
3230
3331 id : str
3432 name : str
@@ -75,19 +73,18 @@ class AdventureData(TypedDict):
7573 """
7674 A dictionary containing the game data, serialized from a JSON file in `resources/fun/adventures`.
7775
78- The keys are the room names, and the values are dictionaries containing the room data, which can be either a RoomData or an EndRoomData.
76+ The keys are the room names, and the values are dictionaries containing the room data,
77+ which can be either a RoomData or an EndRoomData.
7978
8079 There must exist only one "start" key in the dictionary. However, there can be multiple endings, i.e., EndRoomData.
8180 """
8281
8382 start : RoomData
84- __annotations__ : dict [str , Union [ RoomData , EndRoomData ] ]
83+ __annotations__ : dict [str , RoomData | EndRoomData ]
8584
8685
8786class GameCodeNotFoundError (ValueError ):
88- """
89- Raised when a GameSession code doesn't exist.
90- """
87+ """Raised when a GameSession code doesn't exist."""
9188
9289 def __init__ (
9390 self ,
@@ -97,9 +94,7 @@ def __init__(
9794
9895
9996class GameSession :
100- """
101- An interactive session for the Adventure RPG game.
102- """
97+ """An interactive session for the Adventure RPG game."""
10398
10499 def __init__ (
105100 self ,
@@ -144,7 +139,7 @@ def _get_game_data(self, game_code: str) -> AdventureData | None:
144139 self .game_code = game_code
145140 except (ValueError , IndexError ):
146141 pass
147-
142+
148143 # load the game data from the JSON file
149144 try :
150145 game_data = json .loads (
@@ -172,15 +167,15 @@ def cancel_timeout(self) -> None:
172167 def reset_timeout (self ) -> None :
173168 """Cancels the original timeout task and sets it again from the start."""
174169 self .cancel_timeout ()
175-
170+
176171 # recreate the timeout task
177172 self ._timeout_task = self ._bot .loop .create_task (self .timeout ())
178173
179174 async def send_available_game_codes (self ) -> None :
180175 """Sends a list of all available game codes."""
181176 available_game_codes = "\n \n " .join (
182177 f"{ index } . **{ game ['name' ]} ** (`{ game ['id' ]} `)\n *{ game ['description' ]} *"
183- for index , game in enumerate (AVAILABLE_GAMES , start = 1 )
178+ for index , game in enumerate (AVAILABLE_GAMES , start = 1 )
184179 )
185180
186181 embed = Embed (
@@ -192,7 +187,7 @@ async def send_available_game_codes(self) -> None:
192187 embed .set_footer (text = "💡 Hint: use `.adventure [game_code]` or `.adventure [index]` to start a game." )
193188
194189 await self .destination .send (embed = embed )
195-
190+
196191 async def on_reaction_add (self , reaction : Reaction , user : User ) -> None :
197192 """Event handler for when reactions are added on the game message."""
198193 # ensure it was the relevant session message
@@ -235,13 +230,13 @@ async def prepare(self) -> None:
235230 self ._bot .add_listener (self .on_message_delete )
236231 else :
237232 await self .send_available_game_codes ()
238-
233+
239234
240235 def add_reactions (self ) -> None :
241236 """Adds the relevant reactions to the message based on if options are available in the current room."""
242237 if self .is_in_ending_room :
243238 return
244-
239+
245240 current_room = self ._current_room
246241 available_options = self .game_data [current_room ]["options" ]
247242 reactions = [option ["emoji" ] for option in available_options ]
@@ -259,7 +254,7 @@ def _format_room_data(self, room_data: RoomData) -> str:
259254 )
260255
261256 return f"{ text } \n \n { formatted_options } "
262-
257+
263258 def embed_message (self , room_data : RoomData | EndRoomData ) -> Embed :
264259 """Returns an Embed with the requested room data formatted within."""
265260 embed = Embed ()
@@ -296,9 +291,7 @@ async def update_message(self, room_id: str) -> None:
296291
297292 @classmethod
298293 async def start (cls , ctx : Context , game_code : str | None = None ) -> "GameSession" :
299- """
300- Create and begin a game session based on the given game code.
301- """
294+ """Create and begin a game session based on the given game code."""
302295 session = cls (ctx , game_code )
303296 await session .prepare ()
304297
@@ -340,12 +333,13 @@ async def new_adventure(self, ctx: Context, game_code: str | None = None) -> Non
340333 await GameSession .start (ctx , game_code )
341334 except GameCodeNotFoundError as error :
342335 await ctx .send (str (error ))
343-
336+
344337 @commands .command (name = "adventures" )
345338 async def list_adventures (self , ctx : Context ) -> None :
346339 """List all available adventure games."""
347340 await GameSession .start (ctx , None )
348341
349342
350343async def setup (bot : Bot ) -> None :
344+ """Load the Adventure cog."""
351345 await bot .add_cog (Adventure (bot ))
0 commit comments