From 7459e4adcf7b144da1ce73c794afa965ed9aac09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=95=80=E9=93=AC=E9=85=B8=E9=92=BE?= <148796996+K2cr2O1@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:14:37 +0800 Subject: [PATCH] Dev (#79) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(discord): 修复 WebSocket 连接检测并增强跨平台文件处理 修复 Discord WebSocket 连接检测逻辑,使用正确的属性检查连接状态 为跨平台消息处理添加文件类型支持,并增加详细的调试日志 优化附件处理逻辑,确保所有文件类型都能正确识别和转发 * feat(跨平台): 优化消息处理并添加纯文本提取功能 添加 extract_text_only 函数过滤非文本标记 修改翻译逻辑仅处理纯文本内容 完善附件处理和消息内容拼接 修复仅包含表情时的消息处理问题 * refactor(discord-cross): 使用模块专用日志记录器替换全局日志记录器 将各模块中的全局日志记录器替换为模块专用日志记录器,以提供更清晰的日志来源标识 同时在适配器中添加会话状态检查和重连机制,提升消息发送的可靠性 * feat(翻译): 改进翻译功能,同时显示原文和译文 修改翻译功能,不再替换原文而是同时显示原文和翻译内容,方便用户对照 更新 DeepSeek API 配置为官方地址和模型 优化 Discord 适配器的重连逻辑,直接关闭 WebSocket 触发重连 修复 Discord 频道 ID 转换逻辑,简化处理流程 * feat(cross-platform): 添加跨平台功能支持及配置优化 - 新增跨平台配置模型和全局配置支持 - 优化 Discord 适配器的连接管理和错误处理 - 添加 watchdog 和 discord.py 依赖 - 创建 DeepSeek API 配置文档 - 移除重复的同步帮助图片代码 - 改进跨平台插件配置加载逻辑 * fix(jrcd): 修正群组ID检查条件 删除不再使用的示例插件文件 * feat: 改进配置加载逻辑并更新项目配置 当配置文件不存在时自动生成示例配置 添加pyproject.toml作为项目构建配置 更新.gitignore忽略更多文件类型 删除不再使用的反向WebSocket示例文件 * docs: 更新架构文档和项目结构说明 添加反向WebSocket连接模式说明 补充核心管理器文档 更新项目结构文件 在文档首页添加特色功能说明 * fix(discord): 修复WebSocket连接检查并添加错误日志 refactor(config): 更新配置文件的网络和认证信息 feat(cross-platform): 为跨平台消息处理添加异常捕获和日志 * fix(discord-cross): 修复跨平台消息处理和附件下载问题 修复QQ群消息处理中的非群消息过滤问题 优化Discord附件下载逻辑,使用aiohttp替代requests 修复Redis订阅任务重复创建问题 调整消息格式化的embed字段处理逻辑 --- adapters/discord_adapter.py | 19 +++++++++++++------ plugins/discord-cross/handlers.py | 4 ++++ plugins/discord-cross/parser.py | 10 +++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/adapters/discord_adapter.py b/adapters/discord_adapter.py index 0a7fb39..155702b 100644 --- a/adapters/discord_adapter.py +++ b/adapters/discord_adapter.py @@ -41,6 +41,7 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object): self.proxy = None self.proxy_type = "http" + self._redis_sub_task = None if global_config.discord.proxy: self.proxy = global_config.discord.proxy self.proxy_type = global_config.discord.proxy_type or "http" @@ -66,7 +67,8 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object): self.start_heartbeat_task(interval=30) # 启动 Redis 订阅以处理跨平台消息 - asyncio.create_task(self.start_redis_subscription()) + if self._redis_sub_task is None or self._redis_sub_task.done(): + self._redis_sub_task = asyncio.create_task(self.start_redis_subscription()) async def on_message(self, message: 'discord.Message'): """当收到 Discord 消息时触发""" @@ -94,7 +96,7 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object): async def start_redis_subscription(self): """启动 Redis 订阅以处理跨平台消息发送""" - if redis_manager.redis is None: + if redis_manager._redis is None: self.logger.warning("[DiscordAdapter] Redis 未初始化,跳过订阅") return @@ -226,7 +228,12 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object): if attachment_url.startswith('http'): try: - response = requests.get(attachment_url, proxies=proxies, timeout=30) + import aiohttp + proxy_url = self.proxy if self.proxy else None + async with aiohttp.ClientSession() as session: + async with session.get(attachment_url, proxy=proxy_url, timeout=30) as response: + content_bytes = await response.read() + if not filename: filename = os.path.basename(attachment_url.split('?')[0]) or "attachment" @@ -235,7 +242,7 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object): if is_voice: # 尝试转换为 OGG Opus - ogg_bytes = await self.convert_to_ogg_opus(response.content) + ogg_bytes = await self.convert_to_ogg_opus(content_bytes) if ogg_bytes: # 转换成功,作为语音消息发送 # discord.py 官方 API 目前不支持直接发送语音消息 @@ -277,9 +284,9 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object): files.append(discord.File(fp=io.BytesIO(ogg_bytes), filename="voice.ogg")) else: # 转换失败,作为普通文件发送 - files.append(discord.File(fp=io.BytesIO(response.content), filename=filename)) + files.append(discord.File(fp=io.BytesIO(content_bytes), filename=filename)) else: - files.append(discord.File(fp=io.BytesIO(response.content), filename=filename)) + files.append(discord.File(fp=io.BytesIO(content_bytes), filename=filename)) except Exception as e: self.logger.error(f"[DiscordAdapter] 下载附件失败: {attachment_url}, 错误: {e}") diff --git a/plugins/discord-cross/handlers.py b/plugins/discord-cross/handlers.py index 0ed3a2e..1e13b51 100644 --- a/plugins/discord-cross/handlers.py +++ b/plugins/discord-cross/handlers.py @@ -55,6 +55,10 @@ async def handle_qq_group_message(event: GroupMessageEvent): if not config.ENABLE_CROSS_PLATFORM: return + # 忽略非群消息和 Discord 注入的消息 + if not hasattr(event, 'group_id') or hasattr(event, '_is_discord_message'): + return + group_id = event.group_id mapped_channel = None for discord_channel_id, info in config.CROSS_PLATFORM_MAP.items(): diff --git a/plugins/discord-cross/parser.py b/plugins/discord-cross/parser.py index d85a372..99fcb6f 100644 --- a/plugins/discord-cross/parser.py +++ b/plugins/discord-cross/parser.py @@ -327,13 +327,14 @@ async def format_qq_to_discord_content( "name": f"{qq_nickname}", "icon_url": f"https://q1.qlogo.cn/g?b=qq&nk={qq_user_id}&s=640" }, - "description": content if content else "", - "timestamp": None, "footer": { "text": f"来自 QQ" } } + if content: + embed["description"] = content + if attachments: image_urls = [] voice_urls = [] @@ -364,7 +365,10 @@ async def format_qq_to_discord_content( filtered_attachments.append(att) attachments = filtered_attachments - embed["description"] = content if content else "" + if content: + embed["description"] = content + elif "description" not in embed: + embed["description"] = "" if image_urls: embed["image"] = {"url": image_urls[0]}