|
1 | 1 | import base64 |
2 | 2 | import os |
3 | 3 | from pathlib import Path |
4 | | -from typing import Final |
| 4 | +from typing import Any, Final, cast |
5 | 5 |
|
6 | 6 | import yaml |
7 | 7 | from dotenv import load_dotenv |
@@ -34,23 +34,29 @@ def convert_dict_str_to_int(original_dict: dict[str, int]) -> dict[int, int]: |
34 | 34 | workspace_root = Path(__file__).parent.parent.parent |
35 | 35 |
|
36 | 36 | config_file = workspace_root / "config/settings.yml" |
| 37 | +config_file_example = workspace_root / "config/settings.yml.example" |
37 | 38 | config = yaml.safe_load(config_file.read_text()) |
| 39 | +config_example = yaml.safe_load(config_file_example.read_text()) |
| 40 | + |
| 41 | + |
| 42 | +# Recursively merge defaults into user config (fills nested missing keys too) |
| 43 | +def merge_defaults(user: dict[str, Any], default: dict[str, Any]) -> None: |
| 44 | + for key, default_val in default.items(): |
| 45 | + if key not in user: |
| 46 | + user[key] = default_val |
| 47 | + logger.warning(f"Added missing config key: {key}") |
| 48 | + elif isinstance(default_val, dict) and isinstance(user.get(key), dict): |
| 49 | + merge_defaults(user[key], cast(dict[str, Any], default_val)) |
| 50 | + |
| 51 | + |
| 52 | +merge_defaults(config, config_example) |
38 | 53 |
|
39 | 54 |
|
40 | 55 | class Config: |
41 | 56 | # Permissions |
42 | 57 | BOT_OWNER_ID: Final[int] = config["USER_IDS"]["BOT_OWNER"] |
43 | 58 | SYSADMIN_IDS: Final[list[int]] = config["USER_IDS"]["SYSADMINS"] |
44 | | - # ALLOW_SYSADMINS_EVAL: Final[bool] = config["ALLOW_SYSADMINS_EVAL"] |
45 | | - # default to false if not specified in config |
46 | | - _allow_sysadmins_eval = config.get("ALLOW_SYSADMINS_EVAL") |
47 | | - if _allow_sysadmins_eval is None: |
48 | | - logger.warning( |
49 | | - "ALLOW_SYSADMINS_EVAL not found in config, defaulting to False. " |
50 | | - "If you want to use the old behavior, please set it to True in your config.", |
51 | | - ) |
52 | | - _allow_sysadmins_eval = False |
53 | | - ALLOW_SYSADMINS_EVAL: Final[bool] = _allow_sysadmins_eval |
| 59 | + ALLOW_SYSADMINS_EVAL: Final[bool] = config["ALLOW_SYSADMINS_EVAL"] |
54 | 60 |
|
55 | 61 | # Production env |
56 | 62 | DEFAULT_PROD_PREFIX: Final[str] = config["BOT_INFO"]["PROD_PREFIX"] |
|
0 commit comments