From 061fcefeef3667288c83f587d34e3a851f3fc17f Mon Sep 17 00:00:00 2001 From: UnCLAS-Prommer Date: Sat, 17 May 2025 17:34:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4mmc=E7=AB=AF=E7=9A=84?= =?UTF-8?q?=E7=99=BD=E5=90=8D=E5=8D=95=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/message_receive/bot.py | 96 ++++++++++++++----------- src/chat/message_receive/chat_stream.py | 50 ++++++------- src/config/official_configs.py | 8 +-- template/bot_config_template.toml | 9 +-- 4 files changed, 86 insertions(+), 77 deletions(-) diff --git a/src/chat/message_receive/bot.py b/src/chat/message_receive/bot.py index cea791de4..88bf141a1 100644 --- a/src/chat/message_receive/bot.py +++ b/src/chat/message_receive/bot.py @@ -38,10 +38,10 @@ class ChatBot: async def _create_pfc_chat(self, message: MessageRecv): try: - chat_id = str(message.chat_stream.stream_id) - private_name = str(message.message_info.user_info.user_nickname) + if global_config.experimental.pfc_chatting: + chat_id = str(message.chat_stream.stream_id) + private_name = str(message.message_info.user_info.user_nickname) - if global_config.experimental.enable_pfc_chatting: await self.pfc_manager.get_or_create_conversation(chat_id, private_name) except Exception as e: @@ -75,27 +75,27 @@ class ChatBot: # print(message_data) logger.trace(f"处理消息:{str(message_data)[:120]}...") message = MessageRecv(message_data) - groupinfo = message.message_info.group_info - userinfo = message.message_info.user_info + group_info = message.message_info.group_info + user_info = message.message_info.user_info # 用户黑名单拦截 - if userinfo.user_id in global_config.chat_target.ban_user_id: - logger.debug(f"用户{userinfo.user_id}被禁止回复") - return + # if userinfo.user_id in global_config.chat_target.ban_user_id: + # logger.debug(f"用户{userinfo.user_id}被禁止回复") + # return - if groupinfo is None: - logger.trace("检测到私聊消息,检查") - # 好友黑名单拦截 - if userinfo.user_id not in global_config.experimental.talk_allowed_private: - # logger.debug(f"用户{userinfo.user_id}没有私聊权限") - return + # if groupinfo is None: + # logger.trace("检测到私聊消息,检查") + # # 好友黑名单拦截 + # if userinfo.user_id not in global_config.experimental.talk_allowed_private: + # # logger.debug(f"用户{userinfo.user_id}没有私聊权限") + # return # 群聊黑名单拦截 # print(groupinfo.group_id) # print(global_config.chat_target.talk_allowed_groups) - if groupinfo is not None and groupinfo.group_id not in global_config.chat_target.talk_allowed_groups: - logger.debug(f"群{groupinfo.group_id}被禁止回复") - return + # if groupinfo is not None and groupinfo.group_id not in global_config.chat_target.talk_allowed_groups: + # logger.debug(f"群{groupinfo.group_id}被禁止回复") + # return # 确认从接口发来的message是否有自定义的prompt模板信息 if message.message_info.template_info and not message.message_info.template_info.template_default: @@ -112,33 +112,49 @@ class ChatBot: async def preprocess(): logger.trace("开始预处理消息...") # 如果在私聊中 - if groupinfo is None: + if group_info is None: logger.trace("检测到私聊消息") # 是否在配置信息中开启私聊模式 - if global_config.experimental.enable_friend_chat: - logger.trace("私聊模式已启用") - # 是否进入PFC - if global_config.enable_pfc_chatting: - logger.trace("进入PFC私聊处理流程") - userinfo = message.message_info.user_info - messageinfo = message.message_info - # 创建聊天流 - logger.trace(f"为{userinfo.user_id}创建/获取聊天流") - chat = await chat_manager.get_or_create_stream( - platform=messageinfo.platform, - user_info=userinfo, - group_info=groupinfo, - ) - message.update_chat_stream(chat) - await self.only_process_chat.process_message(message) - await self._create_pfc_chat(message) - # 禁止PFC,进入普通的心流消息处理逻辑 - else: - logger.trace("进入普通心流私聊处理") - await self.heartflow_processor.process_message(message_data) + # if global_config.experimental.enable_friend_chat: + # logger.trace("私聊模式已启用") + # # 是否进入PFC + # if global_config.enable_pfc_chatting: + # logger.trace("进入PFC私聊处理流程") + # userinfo = message.message_info.user_info + # messageinfo = message.message_info + # # 创建聊天流 + # logger.trace(f"为{userinfo.user_id}创建/获取聊天流") + # chat = await chat_manager.get_or_create_stream( + # platform=messageinfo.platform, + # user_info=userinfo, + # group_info=groupinfo, + # ) + # message.update_chat_stream(chat) + # await self.only_process_chat.process_message(message) + # await self._create_pfc_chat(message) + # # 禁止PFC,进入普通的心流消息处理逻辑 + # else: + # logger.trace("进入普通心流私聊处理") + # await self.heartflow_processor.process_message(message_data) + if global_config.experimental.pfc_chatting: + logger.trace("进入PFC私聊处理流程") + # 创建聊天流 + logger.trace(f"为{user_info.user_id}创建/获取聊天流") + chat = await chat_manager.get_or_create_stream( + platform=message.message_info.platform, + user_info=user_info, + group_info=group_info, + ) + message.update_chat_stream(chat) + await self.only_process_chat.process_message(message) + await self._create_pfc_chat(message) + # 禁止PFC,进入普通的心流消息处理逻辑 + else: + logger.trace("进入普通心流私聊处理") + await self.heartflow_processor.process_message(message_data) # 群聊默认进入心流消息处理逻辑 else: - logger.trace(f"检测到群聊消息,群ID: {groupinfo.group_id}") + logger.trace(f"检测到群聊消息,群ID: {group_info.group_id}") await self.heartflow_processor.process_message(message_data) if template_group_name: diff --git a/src/chat/message_receive/chat_stream.py b/src/chat/message_receive/chat_stream.py index 723d6da47..e00fc7370 100644 --- a/src/chat/message_receive/chat_stream.py +++ b/src/chat/message_receive/chat_stream.py @@ -39,7 +39,7 @@ class ChatStream: def to_dict(self) -> dict: """转换为字典格式""" - result = { + return { "stream_id": self.stream_id, "platform": self.platform, "user_info": self.user_info.to_dict() if self.user_info else None, @@ -47,7 +47,6 @@ class ChatStream: "create_time": self.create_time, "last_active_time": self.last_active_time, } - return result @classmethod def from_dict(cls, data: dict) -> "ChatStream": @@ -235,33 +234,34 @@ class ChatManager: @staticmethod async def _save_stream(stream: ChatStream): """保存聊天流到数据库""" - if not stream.saved: - stream_data_dict = stream.to_dict() + if stream.saved: + return + stream_data_dict = stream.to_dict() - def _db_save_stream_sync(s_data_dict: dict): - user_info_d = s_data_dict.get("user_info") - group_info_d = s_data_dict.get("group_info") + def _db_save_stream_sync(s_data_dict: dict): + user_info_d = s_data_dict.get("user_info") + group_info_d = s_data_dict.get("group_info") - fields_to_save = { - "platform": s_data_dict["platform"], - "create_time": s_data_dict["create_time"], - "last_active_time": s_data_dict["last_active_time"], - "user_platform": user_info_d["platform"] if user_info_d else "", - "user_id": user_info_d["user_id"] if user_info_d else "", - "user_nickname": user_info_d["user_nickname"] if user_info_d else "", - "user_cardname": user_info_d.get("user_cardname", "") if user_info_d else None, - "group_platform": group_info_d["platform"] if group_info_d else "", - "group_id": group_info_d["group_id"] if group_info_d else "", - "group_name": group_info_d["group_name"] if group_info_d else "", - } + fields_to_save = { + "platform": s_data_dict["platform"], + "create_time": s_data_dict["create_time"], + "last_active_time": s_data_dict["last_active_time"], + "user_platform": user_info_d["platform"] if user_info_d else "", + "user_id": user_info_d["user_id"] if user_info_d else "", + "user_nickname": user_info_d["user_nickname"] if user_info_d else "", + "user_cardname": user_info_d.get("user_cardname", "") if user_info_d else None, + "group_platform": group_info_d["platform"] if group_info_d else "", + "group_id": group_info_d["group_id"] if group_info_d else "", + "group_name": group_info_d["group_name"] if group_info_d else "", + } - ChatStreams.replace(stream_id=s_data_dict["stream_id"], **fields_to_save).execute() + ChatStreams.replace(stream_id=s_data_dict["stream_id"], **fields_to_save).execute() - try: - await asyncio.to_thread(_db_save_stream_sync, stream_data_dict) - stream.saved = True - except Exception as e: - logger.error(f"保存聊天流 {stream.stream_id} 到数据库失败 (Peewee): {e}", exc_info=True) + try: + await asyncio.to_thread(_db_save_stream_sync, stream_data_dict) + stream.saved = True + except Exception as e: + logger.error(f"保存聊天流 {stream.stream_id} 到数据库失败 (Peewee): {e}", exc_info=True) async def _save_all_streams(self): """保存所有聊天流""" diff --git a/src/config/official_configs.py b/src/config/official_configs.py index d92d925d6..6ad4648ba 100644 --- a/src/config/official_configs.py +++ b/src/config/official_configs.py @@ -340,11 +340,11 @@ class TelemetryConfig(ConfigBase): class ExperimentalConfig(ConfigBase): """实验功能配置类""" - enable_friend_chat: bool = False - """是否启用好友聊天""" + # enable_friend_chat: bool = False + # """是否启用好友聊天""" - talk_allowed_private: set[str] = field(default_factory=lambda: set()) - """允许聊天的私聊列表""" + # talk_allowed_private: set[str] = field(default_factory=lambda: set()) + # """允许聊天的私聊列表""" pfc_chatting: bool = False """是否启用PFC""" diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index 8ffbcfa92..b66c3b180 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "2.0.0" +version = "2.1.0" #----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读---- #如果你想要修改配置文件,请在修改后将version的值进行变更 @@ -18,12 +18,7 @@ nickname = "麦麦" alias_names = ["麦叠", "牢麦"] #该选项还在调试中,暂时未生效 [chat_target] -talk_allowed_groups = [ - 123, - 123, -] #可以回复消息的群号码 talk_frequency_down_groups = [] #降低回复频率的群号码 -ban_user_id = [] #禁止回复和读取消息的QQ号 [personality] #未完善 personality_core = "用一句话或几句话描述人格的核心特点" # 建议20字以内,谁再写3000字小作文敲谁脑袋 @@ -171,8 +166,6 @@ enable_kaomoji_protection = false # 是否启用颜文字保护 enable = true [experimental] #实验性功能 -enable_friend_chat = false # 是否启用好友聊天 -talk_allowed_private = [] # 可以回复消息的QQ号 pfc_chatting = false # 是否启用PFC聊天,该功能仅作用于私聊,与回复模式独立 #下面的模型若使用硅基流动则不需要更改,使用ds官方则改成.env自定义的宏,使用自定义模型则选择定位相似的模型自己填写