[docs]classNoDiscordToken(GoldyBotError):def__init__(self):super().__init__("No Discord token was entered! Enter your discord token into the .env file at root or pass it in when initializing Goldy class with the Token class. See '' for more info.")
[docs]classNoDatabaseToken(GoldyBotError):def__init__(self):super().__init__("No MongoDB database url was entered! Create and enter a MongoDB database url into the .env file at root or pass it in when initializing Goldy class with the Token class. See '' for more info.")
[docs]@dataclassclassToken:"""Handles grabbing the token in many various ways."""discord_token:str|None=field(repr=False,default=None)database_url:str|None=field(repr=False,default=None)def__post_init__(self):self.logger=LoggerAdapter(goldy_bot_logger,"Token")discord_token,mongodb_url=self.get_token_from_env()self.discord_token=(lambdaentered_token:discord_tokenifentered_tokenisNoneelseentered_token)(self.discord_token)self.database_url=(lambdaentered_token:mongodb_urlifentered_tokenisNoneelseentered_token)(self.database_url)ifself.discord_tokenisNone:self.create_token_env_file()raiseNoDiscordToken()ifself.database_urlisNone:self.create_token_env_file()raiseNoDatabaseToken()
[docs]defcreate_token_env_file(self)->bool:if".env"notinos.listdir("."):self.logger.debug("Creating .env file in root dir...")# Open template env file.template_env_file=open(Paths.TOKEN_ENV_TEMPLATE,mode="r")template_env_file.seek(0)# Create .env at root.env_file=open(".env",mode="w")# Copy template to it.env_file.write(template_env_file.read())self.logger.info(Colours.GREEN.apply_to_string("Created .env file in root dir!"))returnTrueself.logger.debug("A .env file already exists in root.")returnFalse
[docs]defget_token_from_env(self)->Tuple[str|None,str|None]:""" Returns tuple of tokens from a ``.env`` file. Tuple index order: - ``Discord Bot Token`` - ``MongoDB Url`` """self.logger.debug("Searching current working directory for .env file if exists...")config=AutoConfig(search_path=os.getcwd())# Grabbing tokens.# ------------------self.logger.debug("Grabbing discord token...")discord_token=config("DISCORD_TOKEN",default=None)self.logger.debug("Grabbing database url...")database_token=config("MONGODB_URL",default=None)# If the placeholder text is still there, set token to None.# -----------------------------------------------------------ifdiscord_token=="DISCORD BOT TOKEN HERE":discord_token=Noneifdatabase_token=="MONGO DATABASE URL HERE":database_token=Noneself.logger.debug("Done")returndiscord_token,database_token