performance improvements, stability, and primarily settings-handling improvements. - Due to the container transition, some settings handling became quietly broken or defunct.
35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
# modules/common/settings.py
|
|
import os
|
|
|
|
class ConfigView:
|
|
"""Unified read: ENV first, then optional bot.config['DEFAULT'], then fallback."""
|
|
def __init__(self, bot):
|
|
self._env = os.environ
|
|
self._default = {}
|
|
try:
|
|
self._default = getattr(bot, "config", {}).get("DEFAULT", {}) or {}
|
|
except Exception:
|
|
pass
|
|
|
|
def get(self, key: str, default: str = "") -> str:
|
|
v = self._env.get(key.upper(), "")
|
|
if not v:
|
|
v = self._default.get(key, "")
|
|
v = (v or "").strip().strip('"').strip("'")
|
|
return v if v else default
|
|
|
|
def int(self, key: str, default: int = 0) -> int:
|
|
try:
|
|
return int(self.get(key, ""))
|
|
except Exception:
|
|
return default
|
|
|
|
def bool(self, key: str, default: bool = False) -> bool:
|
|
v = self.get(key, "")
|
|
if not v:
|
|
return default
|
|
return v.lower() in ("1", "true", "yes", "on")
|
|
|
|
def cfg(bot) -> ConfigView:
|
|
return ConfigView(bot)
|