From e276ef9486145d50c4b4c47cb618b4c6d3366b95 Mon Sep 17 00:00:00 2001 From: Parker Date: Wed, 26 Jun 2024 18:41:37 -0500 Subject: Fix support for environment variables --- code/config.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'code/config.py') 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"] -- cgit v1.2.3-70-g09d2