refactor(plugin): 优化插件配置加载与同步机制

- 移除全局插件配置开关 `plugins.centralized_config`。
- 简化 `PluginBase` 的配置加载逻辑,不再使用模板文件,而是直接在中央配置目录生成默认配置。
- 在 `PluginManager` 中增加配置同步逻辑,在加载插件时,实现插件目录与中央配置目录之间的双向同步,确保配置一致性。
- 更新 `bot_config_template.toml`,移除已废弃的 `[plugins]` 配置项并提升版本号。
This commit is contained in:
minecraft1024a
2025-08-30 11:21:26 +08:00
committed by Windpicker-owo
parent efdda4d6f4
commit 4b36a34c1e
5 changed files with 59 additions and 43 deletions

View File

@@ -1,5 +1,7 @@
import asyncio
import os
import shutil
import hashlib
import traceback
import importlib
@@ -37,6 +39,48 @@ class PluginManager:
self._ensure_plugin_directories()
logger.info("插件管理器初始化完成")
def _synchronize_plugin_config(self, plugin_name: str, plugin_dir: str):
"""
同步单个插件的配置。
"""
central_config_dir = os.path.join("config", "plugins", plugin_name)
plugin_config_dir = os.path.join(plugin_dir, "config")
# 确保中央配置目录存在
os.makedirs(central_config_dir, exist_ok=True)
# 1. 从插件目录同步到中央目录(如果中央配置不存在)
if os.path.exists(plugin_config_dir) and os.path.isdir(plugin_config_dir):
for filename in os.listdir(plugin_config_dir):
central_config_file = os.path.join(central_config_dir, filename)
plugin_config_file = os.path.join(plugin_config_dir, filename)
if not os.path.exists(central_config_file) and os.path.isfile(plugin_config_file):
shutil.copy2(plugin_config_file, central_config_file)
logger.info(f"{plugin_name} 复制默认配置到中央目录: {filename}")
# 2. 从中央目录同步到插件目录(覆盖)
if os.path.isdir(central_config_dir):
for filename in os.listdir(central_config_dir):
central_config_file = os.path.join(central_config_dir, filename)
plugin_config_file = os.path.join(plugin_config_dir, filename)
if not os.path.isfile(central_config_file):
continue
# 确保插件的 config 目录存在
os.makedirs(plugin_config_dir, exist_ok=True)
should_copy = True
if os.path.exists(plugin_config_file):
with open(central_config_file, 'rb') as f1, open(plugin_config_file, 'rb') as f2:
if hashlib.md5(f1.read()).hexdigest() == hashlib.md5(f2.read()).hexdigest():
should_copy = False
if should_copy:
shutil.copy2(central_config_file, plugin_config_file)
logger.info(f"同步中央配置到 {plugin_name}: {filename}")
# === 插件目录管理 ===
def add_plugin_directory(self, directory: str) -> bool:
@@ -104,6 +148,9 @@ class PluginManager:
if not plugin_dir:
return False, 1
# 同步插件配置
self._synchronize_plugin_config(plugin_name, plugin_dir)
plugin_instance = plugin_class(plugin_dir=plugin_dir) # 实例化插件可能因为缺少manifest而失败
if not plugin_instance:
logger.error(f"插件 {plugin_name} 实例化失败")