From dcebdb7a35971afc4aa0cb02d5e2090854aac044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=A5=E6=B2=B3=E6=99=B4?= Date: Thu, 17 Apr 2025 16:23:53 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=87=8D=E5=91=BD=E5=90=8D=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E4=BB=A5=E9=81=BF=E5=85=8D=E4=B8=8E=E5=86=85=E7=BD=AE?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 将 Message 类中的 time 参数重命名为 timestamp,避免与 Python 内置模块 time 冲突 2. 在消息处理相关函数中统一使用 timestamp 变量名称 3. 优化 message_storage 查询条件构建方式 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/plugins/PFC/conversation.py | 2 +- src/plugins/PFC/message_storage.py | 4 +--- src/plugins/chat/message.py | 14 +++++++------- src/plugins/chat/utils.py | 2 +- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index ba1bf3a78..598468e89 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -124,7 +124,7 @@ class Conversation: return Message( message_id=msg_dict["message_id"], chat_stream=chat_stream, - time=msg_dict["time"], + timestamp=msg_dict["time"], user_info=user_info, processed_plain_text=msg_dict.get("processed_plain_text", ""), detailed_plain_text=msg_dict.get("detailed_plain_text", ""), diff --git a/src/plugins/PFC/message_storage.py b/src/plugins/PFC/message_storage.py index b57f5d2b5..cd6a01e34 100644 --- a/src/plugins/PFC/message_storage.py +++ b/src/plugins/PFC/message_storage.py @@ -51,11 +51,9 @@ class MongoDBMessageStorage(MessageStorage): """MongoDB消息存储实现""" async def get_messages_after(self, chat_id: str, message_time: float) -> List[Dict[str, Any]]: - query = {"chat_id": chat_id} + query = {"chat_id": chat_id, "time": {"$gt": message_time}} # print(f"storage_check_message: {message_time}") - query["time"] = {"$gt": message_time} - return list(db.messages.find(query).sort("time", 1)) async def get_messages_before(self, chat_id: str, time_point: float, limit: int = 5) -> List[Dict[str, Any]]: diff --git a/src/plugins/chat/message.py b/src/plugins/chat/message.py index 6279e8f25..525f9da29 100644 --- a/src/plugins/chat/message.py +++ b/src/plugins/chat/message.py @@ -31,7 +31,7 @@ class Message(MessageBase): def __init__( self, message_id: str, - time: float, + timestamp: float, chat_stream: ChatStream, user_info: UserInfo, message_segment: Optional[Seg] = None, @@ -43,7 +43,7 @@ class Message(MessageBase): message_info = BaseMessageInfo( platform=chat_stream.platform, message_id=message_id, - time=time, + time=timestamp, group_info=chat_stream.group_info, user_info=user_info, ) @@ -143,7 +143,7 @@ class MessageRecv(Message): def _generate_detailed_text(self) -> str: """生成详细文本,包含时间和用户信息""" # time_str = time.strftime("%m-%d %H:%M:%S", time.localtime(self.message_info.time)) - time = self.message_info.time + timestamp = self.message_info.time user_info = self.message_info.user_info # name = ( # f"{user_info.user_nickname}(ta的昵称:{user_info.user_cardname},ta的id:{user_info.user_id})" @@ -151,7 +151,7 @@ class MessageRecv(Message): # else f"{user_info.user_nickname}(ta的id:{user_info.user_id})" # ) name = f"<{self.message_info.platform}:{user_info.user_id}:{user_info.user_nickname}:{user_info.user_cardname}>" - return f"[{time}] {name}: {self.processed_plain_text}\n" + return f"[{timestamp}] {name}: {self.processed_plain_text}\n" @dataclass @@ -170,7 +170,7 @@ class MessageProcessBase(Message): # 调用父类初始化 super().__init__( message_id=message_id, - time=round(time.time(), 3), # 保留3位小数 + timestamp=round(time.time(), 3), # 保留3位小数 chat_stream=chat_stream, user_info=bot_user_info, message_segment=message_segment, @@ -242,7 +242,7 @@ class MessageProcessBase(Message): def _generate_detailed_text(self) -> str: """生成详细文本,包含时间和用户信息""" # time_str = time.strftime("%m-%d %H:%M:%S", time.localtime(self.message_info.time)) - time = self.message_info.time + timestamp = self.message_info.time user_info = self.message_info.user_info # name = ( # f"{user_info.user_nickname}(ta的昵称:{user_info.user_cardname},ta的id:{user_info.user_id})" @@ -250,7 +250,7 @@ class MessageProcessBase(Message): # else f"{user_info.user_nickname}(ta的id:{user_info.user_id})" # ) name = f"<{self.message_info.platform}:{user_info.user_id}:{user_info.user_nickname}:{user_info.user_cardname}>" - return f"[{time}] {name}: {self.processed_plain_text}\n" + return f"[{timestamp}] {name}: {self.processed_plain_text}\n" @dataclass diff --git a/src/plugins/chat/utils.py b/src/plugins/chat/utils.py index 06fa6de82..b3ffb9a13 100644 --- a/src/plugins/chat/utils.py +++ b/src/plugins/chat/utils.py @@ -135,7 +135,7 @@ async def get_recent_group_messages(chat_id: str, limit: int = 12) -> list: msg = Message( message_id=msg_data["message_id"], chat_stream=chat_stream, - time=msg_data["time"], + timestamp=msg_data["time"], user_info=user_info, processed_plain_text=msg_data.get("processed_text", ""), detailed_plain_text=msg_data.get("detailed_plain_text", ""),