fix:修正配置路径的定义,确保使用os.path.join以提高跨平台兼容性

This commit is contained in:
墨梓柒
2025-06-19 00:20:37 +08:00
parent 535dcc76bf
commit 30356f5fdb

View File

@@ -43,8 +43,10 @@ install(extra_lines=3)
# 配置主程序日志格式
logger = get_logger("config")
CONFIG_DIR = "config"
TEMPLATE_DIR = "template"
# 获取当前文件所在目录的父目录的父目录即MaiBot项目根目录
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
CONFIG_DIR = os.path.join(PROJECT_ROOT, "config")
TEMPLATE_DIR = os.path.join(PROJECT_ROOT, "template")
# 考虑到实际上配置文件中的mai_version是不会自动更新的,所以采用硬编码
# 对该字段的更新请严格参照语义化版本规范https://semver.org/lang/zh-CN/
@@ -53,12 +55,12 @@ MMC_VERSION = "0.8.0-snapshot.1"
def update_config():
# 获取根目录路径
old_config_dir = f"{CONFIG_DIR}/old"
old_config_dir = os.path.join(CONFIG_DIR, "old")
# 定义文件路径
template_path = f"{TEMPLATE_DIR}/bot_config_template.toml"
old_config_path = f"{CONFIG_DIR}/bot_config.toml"
new_config_path = f"{CONFIG_DIR}/bot_config.toml"
template_path = os.path.join(TEMPLATE_DIR, "bot_config_template.toml")
old_config_path = os.path.join(CONFIG_DIR, "bot_config.toml")
new_config_path = os.path.join(CONFIG_DIR, "bot_config.toml")
# 检查配置文件是否存在
if not os.path.exists(old_config_path):
@@ -88,11 +90,9 @@ def update_config():
logger.info("已有配置文件未检测到版本号,可能是旧版本。将进行更新")
# 创建old目录如果不存在
os.makedirs(old_config_dir, exist_ok=True)
# 生成带时间戳的新文件名
os.makedirs(old_config_dir, exist_ok=True) # 生成带时间戳的新文件名
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
old_backup_path = f"{old_config_dir}/bot_config_{timestamp}.toml"
old_backup_path = os.path.join(old_config_dir, f"bot_config_{timestamp}.toml")
# 移动旧配置文件到old目录
shutil.move(old_config_path, old_backup_path)
@@ -198,5 +198,5 @@ logger.info(f"MaiCore当前版本: {MMC_VERSION}")
update_config()
logger.info("正在品鉴配置文件...")
global_config = load_config(config_path=f"{CONFIG_DIR}/bot_config.toml")
global_config = load_config(config_path=os.path.join(CONFIG_DIR, "bot_config.toml"))
logger.info("非常的新鲜,非常的美味!")