# Released under the MIT License. See LICENSE for details."""Functionality related to discord sdk integration"""from__future__importannotationsfromtypingimportTYPE_CHECKINGimport_babaseifTYPE_CHECKING:fromtypingimportAny# Add a config key preferably for thisENABLE_DISCORD=True# disable this for now
[docs]@staticmethoddefis_available()->bool:"""Check if the Discord SDK is available. _babase.discord_is_ready() returns None if not available."""return_babase.discord_is_ready()isnotNone
asyncdef_shutdown_coroutine(self)->None:"""Coroutine for shutting down Discord."""_babase.discord_shutdown()@propertydefis_ready(self)->bool:"""Check if the Discord SDK is ready."""return_babase.discord_is_ready()
[docs]defset_presence(self,state:str|None=None,details:str|None=None,start_timestamp:float|None=None,end_timestamp:float|None=None,large_image_key:str|None=None,small_image_key:str|None=None,large_image_text:str|None=None,small_image_text:str|None=None,party_id:str|None=None,party_size:tuple[int,int]|None=None,)->None:"""Set Discord rich presence state. Args: state: Current game state (e.g. "In Match", "Main Menu") details: Additional details about current activity start_timestamp: Activity start time (epoch timestamp) end_timestamp: Activity end time (epoch timestamp) large_image_key: Key for large image asset small_image_key: Key for small image asset large_image_text: Hover text for large image small_image_text: Hover text for small image party_id: Current party ID for join/spectate party_size: Tuple of (current_size, max_size) """ifnotself.is_available():return# Build presence dict with only non-None valuespresence:dict[str,Any]={}ifstateisnotNone:self.state=statepresence['state']=stateifdetailsisnotNone:self.details=detailspresence['details']=detailsifstart_timestampisnotNone:self.start_timestamp=start_timestamppresence['start_timestamp']=start_timestampifend_timestampisnotNone:self.end_timestamp=end_timestamppresence['end_timestamp']=end_timestampiflarge_image_keyisnotNone:self.large_image_key=large_image_keypresence['large_image_key']=large_image_keyifsmall_image_keyisnotNone:self.small_image_key=small_image_keypresence['small_image_key']=small_image_keyiflarge_image_textisnotNone:self.large_image_text=large_image_textpresence['large_image_text']=large_image_textifsmall_image_textisnotNone:self.small_image_text=small_image_textpresence['small_image_text']=small_image_text# Set party info if providedifparty_idisnotNone:_babase.discord_set_party(party_id=party_id)ifparty_sizeisnotNone:_babase.discord_set_party(current_party_size=party_size[0],max_party_size=party_size[1])# Update rich presenceifpresence:_babase.discord_richpresence(**presence)
# Docs-generation hack; import some stuff that we likely only forward-declared# in our actual source code so that docs tools can find it.fromtypingimport(Coroutine,Any,Literal,Callable,Generator,Awaitable,Sequence,Self)importasynciofromconcurrent.futuresimportFuturefrompathlibimportPathfromenumimportEnum