From b2af736ae047bac19483cf7da8fe8fd0cffe8b8d Mon Sep 17 00:00:00 2001 From: "Max R. Carrara" Date: Mon, 10 Mar 2025 23:31:41 +0100 Subject: [PATCH] bot: env: map known env vars to object This just makes accessing them easier. What could be done in the future have some kind of utility method for debugging that prints all of them or returns all of them as a dict or something. Signed-off-by: Max R. Carrara --- .gitignore | 2 ++ src/bot/env.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/bot/env.py diff --git a/.gitignore b/.gitignore index 2d18474..437db9d 100644 --- a/.gitignore +++ b/.gitignore @@ -165,3 +165,5 @@ cython_debug/ # Ruff stuff .ruff_cache/ +# Bot stuff +token.txt diff --git a/src/bot/env.py b/src/bot/env.py new file mode 100644 index 0000000..abaa653 --- /dev/null +++ b/src/bot/env.py @@ -0,0 +1,44 @@ +import functools +import os +import pathlib +import re + + +class Environment: + @classmethod + @functools.lru_cache(maxsize=1) + def bot_log_level(cls) -> str: + return os.environ.get("BOT_LOG_LEVEL", "INFO").strip().upper() + + @classmethod + @functools.lru_cache(maxsize=1) + def bot_memory_db(cls) -> bool: + res = os.environ.get("BOT_MEMORY_DB", "0").strip() + + return bool(int(res)) + + @classmethod + @functools.lru_cache(maxsize=1) + def bot_owner_id(cls) -> int | None: + if "BOT_OWNER_ID" in os.environ: + return int(os.environ["BOT_OWNER_ID"]) + return + + @classmethod + @functools.lru_cache(maxsize=1) + def bot_prefix(cls) -> str: + res = os.environ.get("BOT_PREFIX", "!").strip() + + if re.match(r"\s", res): + raise ValueError( + f'Bot prefix may not contain any whitespace: "{res}" is invalid' + ) + + return res + + @classmethod + @functools.lru_cache(maxsize=1) + def bot_token_path(cls) -> pathlib.Path: + res = os.environ.get("BOT_TOKEN_PATH", "./token.txt") + + return pathlib.Path(res)