feat(cross-platform): 添加跨平台功能支持及配置优化
- 新增跨平台配置模型和全局配置支持 - 优化 Discord 适配器的连接管理和错误处理 - 添加 watchdog 和 discord.py 依赖 - 创建 DeepSeek API 配置文档 - 移除重复的同步帮助图片代码 - 改进跨平台插件配置加载逻辑
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
import os
|
||||
from typing import Dict, Any
|
||||
from core.utils.logger import ModuleLogger
|
||||
from core.config_loader import global_config
|
||||
|
||||
# 创建模块专用日志记录器
|
||||
logger = ModuleLogger("CrossPlatformConfig")
|
||||
@@ -15,50 +16,81 @@ class CrossPlatformConfig:
|
||||
self.CROSS_PLATFORM_CHANNEL = "neobot_cross_platform"
|
||||
self.ENABLE_CROSS_PLATFORM = True
|
||||
|
||||
# DeepSeek API 配置
|
||||
self.DEEPSEEK_API_KEY = "sk-7b824b05e85445f8a9ceef6c849388a9"
|
||||
self.DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
|
||||
self.DEEPSEEK_MODEL = "deepseek-chat"
|
||||
# DeepSeek API 配置 - 从环境变量或配置文件加载
|
||||
self.DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY", "")
|
||||
self.DEEPSEEK_API_URL = os.environ.get("DEEPSEEK_API_URL", "https://api.deepseek.com/v1/chat/completions")
|
||||
self.DEEPSEEK_MODEL = os.environ.get("DEEPSEEK_MODEL", "deepseek-chat")
|
||||
|
||||
# 是否启用翻译功能
|
||||
self.ENABLE_TRANSLATION = True
|
||||
|
||||
# 从全局配置加载
|
||||
self.load_from_global_config()
|
||||
|
||||
def load_from_global_config(self):
|
||||
"""从全局配置加载跨平台配置"""
|
||||
if global_config and hasattr(global_config, 'cross_platform'):
|
||||
cross_platform_config = global_config.cross_platform
|
||||
if cross_platform_config:
|
||||
self.ENABLE_CROSS_PLATFORM = getattr(cross_platform_config, 'enabled', True)
|
||||
self.CROSS_PLATFORM_MAP = {}
|
||||
|
||||
# 加载 mappings
|
||||
if hasattr(cross_platform_config, 'mappings') and cross_platform_config.mappings:
|
||||
for discord_id, mapping in cross_platform_config.mappings.items():
|
||||
if isinstance(mapping, dict):
|
||||
self.CROSS_PLATFORM_MAP[discord_id] = {
|
||||
"qq_group_id": int(mapping.get("qq_group_id", 0)),
|
||||
"name": mapping.get("name", "")
|
||||
}
|
||||
elif hasattr(mapping, 'qq_group_id'):
|
||||
self.CROSS_PLATFORM_MAP[discord_id] = {
|
||||
"qq_group_id": int(mapping.qq_group_id),
|
||||
"name": getattr(mapping, 'name', "")
|
||||
}
|
||||
logger.success(f"[CrossPlatform] 从全局配置加载了 {len(self.CROSS_PLATFORM_MAP)} 个映射")
|
||||
|
||||
async def reload(self):
|
||||
"""重新加载配置"""
|
||||
try:
|
||||
config_path = os.path.join(os.path.dirname(__file__), "..", "..", "config.toml")
|
||||
# 优先使用全局配置
|
||||
self.load_from_global_config()
|
||||
|
||||
if os.path.exists(config_path):
|
||||
try:
|
||||
import tomllib
|
||||
except ImportError:
|
||||
import tomli as tomllib
|
||||
# 如果全局配置不可用,尝试从文件加载
|
||||
if not self.CROSS_PLATFORM_MAP:
|
||||
config_path = os.path.join(os.path.dirname(__file__), "..", "..", "config.toml")
|
||||
|
||||
with open(config_path, "rb") as f:
|
||||
config_data = tomllib.load(f)
|
||||
if os.path.exists(config_path):
|
||||
try:
|
||||
import tomllib
|
||||
except ImportError:
|
||||
import tomli as tomllib
|
||||
|
||||
cross_platform_config = config_data.get("cross_platform", {})
|
||||
self.ENABLE_CROSS_PLATFORM = cross_platform_config.get("enabled", True)
|
||||
|
||||
# 重新加载映射配置
|
||||
mappings = cross_platform_config.get("mappings", {})
|
||||
self.CROSS_PLATFORM_MAP.clear()
|
||||
|
||||
if isinstance(mappings, dict) and mappings:
|
||||
for key, value in mappings.items():
|
||||
if isinstance(value, dict) and "qq_group_id" in value:
|
||||
try:
|
||||
# 直接将 key 转换为整数
|
||||
discord_id = int(str(key))
|
||||
self.CROSS_PLATFORM_MAP[discord_id] = {
|
||||
"qq_group_id": int(value.get("qq_group_id", 0)),
|
||||
"name": value.get("name", "")
|
||||
}
|
||||
except (ValueError, AttributeError):
|
||||
logger.warning(f"[CrossPlatform] 无效的 Discord 频道 ID: {key}")
|
||||
continue
|
||||
|
||||
logger.success(f"[CrossPlatform] 配置已重新加载: {len(self.CROSS_PLATFORM_MAP)} 个映射")
|
||||
with open(config_path, "rb") as f:
|
||||
config_data = tomllib.load(f)
|
||||
|
||||
cross_platform_config = config_data.get("cross_platform", {})
|
||||
self.ENABLE_CROSS_PLATFORM = cross_platform_config.get("enabled", True)
|
||||
|
||||
# 重新加载映射配置
|
||||
mappings = cross_platform_config.get("mappings", {})
|
||||
self.CROSS_PLATFORM_MAP.clear()
|
||||
|
||||
if isinstance(mappings, dict) and mappings:
|
||||
for key, value in mappings.items():
|
||||
if isinstance(value, dict) and "qq_group_id" in value:
|
||||
try:
|
||||
# 直接将 key 转换为整数
|
||||
discord_id = int(str(key))
|
||||
self.CROSS_PLATFORM_MAP[discord_id] = {
|
||||
"qq_group_id": int(value.get("qq_group_id", 0)),
|
||||
"name": value.get("name", "")
|
||||
}
|
||||
except (ValueError, AttributeError):
|
||||
logger.warning(f"[CrossPlatform] 无效的 Discord 频道 ID: {key}")
|
||||
continue
|
||||
|
||||
logger.success(f"[CrossPlatform] 配置已重新加载: {len(self.CROSS_PLATFORM_MAP)} 个映射")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[CrossPlatform] 重新加载配置失败: {e}")
|
||||
|
||||
Reference in New Issue
Block a user