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 <max@aequito.sh>
This commit is contained in:
Max R. Carrara 2025-03-10 23:31:41 +01:00
parent fee37c2372
commit b2af736ae0
2 changed files with 46 additions and 0 deletions

2
.gitignore vendored
View file

@ -165,3 +165,5 @@ cython_debug/
# Ruff stuff
.ruff_cache/
# Bot stuff
token.txt

44
src/bot/env.py Normal file
View file

@ -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)