fix(config): 临时修复 master_users 加载失败问题

问题描述:
由于未明原因,配置文件(bot_config.toml)中的 `permission.master_users` 字段在加载过程中被意外清空,导致权限系统无法正确识别 Master 用户。初步排查指向复杂的配置版本自动更新与迁移逻辑。

解决方案:
在 `config.py` 的 `load_config` 函数中,于 Pydantic 模型验证完成后,增加了一段临时修复代码。该代码会从 `tomlkit` 解析出的原始配置数据中,重新读取 `master_users` 的值,并将其强制赋给最终的 `config` 对象。

具体实现:
1.  使用 `config_data.unwrap()` 将 `tomlkit` 对象安全地转换为标准 Python 字典。
2.  从字典中提取 `permission.master_users` 的值。
3.  将提取到的值直接赋给 `config.permission.master_users`。
4.  添加了详细的日志,以便追踪此修复操作的执行情况。

影响:
-   **正面**:立即恢复了 Master 用户的识别功能,解决了权限检查失效的核心问题。
-   **注意**:这是一个临时性的解决方案(workaround),并未从根本上解决配置更新逻辑中可能存在的缺陷。后续仍需对该逻辑进行深入排查和重构。
This commit is contained in:
tt-P607
2025-11-20 14:39:27 +08:00
parent 5374c26732
commit 5a93867417
2 changed files with 46 additions and 9 deletions

View File

@@ -185,6 +185,11 @@ def _update_dict(target: TOMLDocument | dict | Table, source: TOMLDocument | dic
if key == "version":
continue
# 在合并 permission.master_users 时添加特别调试日志
if key == "permission" and isinstance(value, (dict, Table)) and "master_users" in value:
logger.info(f"【调试日志】在 _update_dict 中检测到 'permission' 表,其 'master_users' 的值为: {value['master_users']}")
if key in target:
# 键已存在,更新值
target_value = target[key]
@@ -497,6 +502,19 @@ def load_config(config_path: str) -> Config:
logger.info("正在解析和验证配置文件...")
config = Config.from_dict(config_data)
logger.info("配置文件解析和验证完成")
# 【临时修复】在验证后,手动从原始数据重新加载 master_users
try:
# 先将 tomlkit 对象转换为纯 Python 字典
config_dict = config_data.unwrap()
if "permission" in config_dict and "master_users" in config_dict["permission"]:
raw_master_users = config_dict["permission"]["master_users"]
# 现在 raw_master_users 就是一个标准的 Python 列表了
config.permission.master_users = raw_master_users
logger.info(f"【临时修复】已手动将 master_users 设置为: {config.permission.master_users}")
except Exception as patch_exc:
logger.error(f"【临时修复】手动设置 master_users 失败: {patch_exc}")
return config
except Exception as e:
logger.critical(f"配置文件解析失败: {e}")