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)