From 9f02dd13ff48c9d21cda0d39da65e259f453aea4 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 10 Feb 2025 13:07:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dqq=E5=9C=A8@=E5=92=8C?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E5=BC=80=E5=90=AF=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E8=BD=AC=E5=8F=91=E6=B6=88=E6=81=AF=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/message/components.py | 14 +++++----- .../aiocqhttp/aiocqhttp_message_event.py | 26 ++++++++++++++----- packages/astrbot/main.py | 11 +++++--- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/astrbot/core/message/components.py b/astrbot/core/message/components.py index 4a992d0b6..16ba0c1e4 100644 --- a/astrbot/core/message/components.py +++ b/astrbot/core/message/components.py @@ -341,14 +341,14 @@ class Forward(BaseMessageComponent): def __init__(self, **_): super().__init__(**_) - -class Node(BaseMessageComponent): # 该 component 仅支持使用 sendGroupForwardMessage 发送 +class Node(BaseMessageComponent): + '''群合并转发消息''' type: ComponentType = "Node" - id: T.Optional[int] = 0 - name: T.Optional[str] = "" - uin: T.Optional[int] = 0 - content: T.Optional[T.Union[str, list]] = "" - seq: T.Optional[T.Union[str, list]] = "" # 不清楚是什么 + id: T.Optional[int] = 0 # 忽略 + name: T.Optional[str] = "" # qq昵称 + uin: T.Optional[int] = 0 # qq号 + content: T.Optional[T.Union[str, list]] = "" # 子消息段列表 + seq: T.Optional[T.Union[str, list]] = "" # 忽略 time: T.Optional[int] = 0 def __init__(self, content: T.Union[str, list], **_): diff --git a/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py b/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py index 3fcbe82e5..27babe7a2 100644 --- a/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py +++ b/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py @@ -1,7 +1,7 @@ -import os +import asyncio from astrbot.api.event import AstrMessageEvent, MessageChain -from astrbot.api.message_components import Plain, Image, Record, At +from astrbot.api.message_components import Plain, Image, Record, At, Node, Music, Video from aiocqhttp import CQHttp from astrbot.core.utils.io import file_to_base64, download_image_by_url @@ -18,7 +18,7 @@ class AiocqhttpMessageEvent(AstrMessageEvent): d = segment.toDict() if isinstance(segment, Plain): d['type'] = 'text' - if isinstance(segment, (Image, Record)): + elif isinstance(segment, (Image, Record)): # convert to base64 if segment.file and segment.file.startswith("file:///"): bs64_data = file_to_base64(segment.file[8:]) @@ -31,7 +31,7 @@ class AiocqhttpMessageEvent(AstrMessageEvent): d['data'] = { 'file': bs64_data, } - if isinstance(segment, At): + elif isinstance(segment, At): d['data'] = { 'qq': str(segment.qq) # 转换为字符串 } @@ -40,7 +40,19 @@ class AiocqhttpMessageEvent(AstrMessageEvent): async def send(self, message: MessageChain): ret = await AiocqhttpMessageEvent._parse_onebot_json(message) - if os.environ.get('TEST_MODE', 'off') == 'on': - return - await self.bot.send(self.message_obj.raw_message, ret) + + send_one_by_one = False + for seg in message.chain: + if isinstance(seg, (Node, Music)): + # 转发消息不能和普通消息混在一起发送 + send_one_by_one = True + break + + if send_one_by_one: + for seg in message.chain: + await self.bot.send(self.message_obj.raw_message, await AiocqhttpMessageEvent._parse_onebot_json(MessageChain([seg]))) + await asyncio.sleep(0.5) + else: + await self.bot.send(self.message_obj.raw_message, ret) + await super().send(message) \ No newline at end of file diff --git a/packages/astrbot/main.py b/packages/astrbot/main.py index a380ce1e6..64a8f6a3c 100644 --- a/packages/astrbot/main.py +++ b/packages/astrbot/main.py @@ -74,8 +74,8 @@ AstrBot 指令: /model: 模型列表 /ls: 对话列表 /new: 创建新对话 -/switch: 切换对话 -/rename: 重命名对话 +/switch 序号: 切换对话 +/rename 新名字: 重命名当前对话 /del: 删除当前会话对话(op) /reset: 重置 LLM 会话(op) /history: 当前对话的对话记录 @@ -495,8 +495,11 @@ UID: {user_id} 此 ID 可用于设置管理员。/op 授权管理员, /deo message.set_result(MessageEventResult().message(f"切换到新对话: 新对话({cid[:4]})。")) @filter.command("switch") - async def switch_conv(self, message: AstrMessageEvent, index: int): + async def switch_conv(self, message: AstrMessageEvent, index: int = None): '''通过 /ls 前面的序号切换对话''' + if index is None: + message.set_result(MessageEventResult().message("请输入对话序号。/switch 对话序号。/ls 查看对话 /new 新建对话")) + return conversations = await self.context.conversation_manager.get_conversations(message.unified_msg_origin) if index > len(conversations) or index < 1: message.set_result(MessageEventResult().message("对话序号错误,请使用 /ls 查看")) @@ -860,4 +863,4 @@ UID: {user_id} 此 ID 可用于设置管理员。/op 授权管理员, /deo # if results: # req.system_prompt += "\nHere are documents that related to user's query: \n" # for result in results: - # req.system_prompt += f"- {result}\n" \ No newline at end of file + # req.system_prompt += f"- {result}\n"7 \ No newline at end of file