From e276ef9486145d50c4b4c47cb618b4c6d3366b95 Mon Sep 17 00:00:00 2001 From: Parker Date: Wed, 26 Jun 2024 18:41:37 -0500 Subject: [PATCH] Fix support for environment variables --- code/bot.py | 3 +-- code/config.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/code/bot.py b/code/bot.py index 7da4f07..4483ca4 100644 --- a/code/bot.py +++ b/code/bot.py @@ -57,6 +57,5 @@ async def get_access_token(): if __name__ == "__main__": - config_contents = config.load_config() - config.validate_config(config_contents) + config.load_config() bot.run(config.TOKEN) diff --git a/code/config.py b/code/config.py index 1e7c5ba..c7be1da 100644 --- a/code/config.py +++ b/code/config.py @@ -45,12 +45,15 @@ create a new templated config.ini file if it doesn't exist. def load_config(): # Look for variables in the environment if "TOKEN" in os.environ or "BOT_COLOR" in os.environ or "BOT_INVITE_LINK" in os.environ: + LOG.info("Detected environment variables. Checking for configuration options.") validate_env_vars() + else: + LOG.info("Detected local environment. Checking for config.ini file.") try: with open("config.ini", "r") as f: file_contents = f.read() - return file_contents + validate_config(file_contents) except FileNotFoundError: config = configparser.ConfigParser() @@ -221,25 +224,26 @@ def validate_env_vars(): BOT_INVITE_LINK = os.environ["BOT_INVITE_LINK"] # Make sure FEEDBACK_CHANNEL_ID is either None or 19 characters long - if os.environ["FEEDBACK_CHANNEL_ID"] is not None: + try: if len(os.environ["FEEDBACK_CHANNEL_ID"]) != 19: LOG.error("FEEDBACK_CHANNEL_ID is not a valid Discord channel ID.") errors += 1 else: FEEDBACK_CHANNEL_ID = int(os.environ["FEEDBACK_CHANNEL_ID"]) - else: + except KeyError: FEEDBACK_CHANNEL_ID = None # Make sure BUG_CHANNEL_ID is either None or 19 characters long - if os.environ["BUG_CHANNEL_ID"] is not None: + try: if len(os.environ["BUG_CHANNEL_ID"]) != 19: LOG.error("BUG_CHANNEL_ID is not a valid Discord channel ID.") errors += 1 else: BUG_CHANNEL_ID = int(os.environ["BUG_CHANNEL_ID"]) - else: + except KeyError: BUG_CHANNEL_ID = None + # Assign the rest of the variables TOKEN = os.environ["TOKEN"] SPOTIFY_CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]