- 新增QQ群头衔+用户名格式剥离:【头衔】用户名 或 [头衔] 用户名 - 处理OCR将【识别为[、】识别为]的情况 - 保持原有纯文本昵称剥离功能 - 测试用例全部通过
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
"""配置管理 — JSON 配置读写"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
CONFIG_FILE = Path(__file__).parent / "config.json"
|
|
|
|
DEFAULT_CONFIG = {
|
|
"monitor_enabled": False,
|
|
"knowledge_base_path": "",
|
|
"poll_interval_seconds": 2.0,
|
|
"wechat_enabled": True,
|
|
"qq_enabled": True,
|
|
"auto_reply_enabled": False,
|
|
"auto_reply_min_score": 0.35,
|
|
"qq_nicknames": [], # 需要从消息文本开头剥离的QQ昵称列表
|
|
}
|
|
|
|
|
|
def load_config() -> dict:
|
|
"""加载配置,文件不存在则返回默认"""
|
|
if not CONFIG_FILE.exists():
|
|
return DEFAULT_CONFIG.copy()
|
|
try:
|
|
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
# 合并默认值,保证新字段不缺失
|
|
merged = DEFAULT_CONFIG.copy()
|
|
merged.update(data)
|
|
return merged
|
|
except (json.JSONDecodeError, OSError):
|
|
return DEFAULT_CONFIG.copy()
|
|
|
|
|
|
def save_config(cfg: dict):
|
|
"""保存配置"""
|
|
merged = DEFAULT_CONFIG.copy()
|
|
merged.update(cfg)
|
|
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
|
|
json.dump(merged, f, ensure_ascii=False, indent=2)
|