From 5be793a054b08d2f4b19422aa03c36c96c4c69ee Mon Sep 17 00:00:00 2001 From: Oct-autumn Date: Fri, 14 Mar 2025 15:32:10 +0800 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E2=80=9C?= =?UTF-8?q?=E6=88=B3=E4=B8=80=E6=88=B3=E2=80=9D=E4=B8=8E=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E8=80=83=E8=99=91=E5=9B=9E=E5=A4=8D=E7=9A=84=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=20=20=20-=20?= =?UTF-8?q?=E5=B0=86handle=5Fmessage=E5=87=BD=E6=95=B0=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=AE=9E=E4=BE=8B=E5=A4=84=E7=90=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=8F=90=E5=8F=96=E5=87=BA=E6=9D=A5=EF=BC=8C=E5=BD=A2?= =?UTF-8?q?=E6=88=90message=5Fprocess=E5=87=BD=E6=95=B0=EF=BC=88=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E4=BB=A3=E7=A0=81=E5=A4=8D=E7=94=A8=E7=8E=87=EF=BC=89?= =?UTF-8?q?=20=20=20-=20=E5=B0=86=E2=80=9C=E6=88=B3=E4=B8=80=E6=88=B3?= =?UTF-8?q?=E2=80=9D=E7=9A=84=E9=80=9A=E7=9F=A5=E5=A4=84=E7=90=86=E4=B8=BA?= =?UTF-8?q?=E4=B8=80=E6=9D=A1=E9=80=9A=E7=94=A8=E6=B6=88=E6=81=AF=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=EF=BC=8C=E4=BA=A4=E7=94=B1message=5Fprocess=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=A4=84=E7=90=86=20=20=20-=20=E5=90=8C=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E7=94=B1=E4=BA=8E=E4=BD=BF=E7=94=A8=E4=BA=86=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E6=B6=88=E6=81=AF=E5=AE=9E=E4=BE=8B=EF=BC=8C=E2=80=9C?= =?UTF-8?q?=E6=88=B3=E4=B8=80=E6=88=B3=E2=80=9D=E7=9A=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=B0=86=E4=B8=8E=E5=85=B6=E4=BB=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=A4=84=E7=90=86=E7=BB=9F=E4=B8=80=E7=BB=8F=E8=BF=87?= =?UTF-8?q?=E7=BE=A4=E7=BB=84=E6=9D=83=E9=99=90=E9=89=B4=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/chat/bot.py | 295 +++++++++++++++++++++++++--------------- 1 file changed, 182 insertions(+), 113 deletions(-) diff --git a/src/plugins/chat/bot.py b/src/plugins/chat/bot.py index 4d1318f2a..aab5c91e9 100644 --- a/src/plugins/chat/bot.py +++ b/src/plugins/chat/bot.py @@ -10,7 +10,6 @@ from nonebot.adapters.onebot.v11 import ( PokeNotifyEvent, GroupRecallNoticeEvent, FriendRecallNoticeEvent, - ) from ..memory_system.memory import hippocampus @@ -55,112 +54,15 @@ class ChatBot: if not self._started: self._started = True - async def handle_notice(self, event: NoticeEvent, bot: Bot) -> None: - """处理收到的通知""" - # 戳一戳通知 - if isinstance(event, PokeNotifyEvent): - # 不处理其他人的戳戳 - if not event.is_tome(): - return - - # 用户屏蔽,不区分私聊/群聊 - if event.user_id in global_config.ban_user_id: - return - - reply_poke_probability = 1.0 # 回复戳一戳的概率,如果要改可以在这里改,暂不提取到配置文件 - - if random() < reply_poke_probability: - raw_message = "[戳了戳]你" # 默认类型 - if info := event.raw_info: - poke_type = info[2].get("txt", "戳了戳") # 戳戳类型,例如“拍一拍”、“揉一揉”、“捏一捏” - custom_poke_message = info[4].get("txt", "") # 自定义戳戳消息,若不存在会为空字符串 - raw_message = f"[{poke_type}]你{custom_poke_message}" - - raw_message += "(这是一个类似摸摸头的友善行为,而不是恶意行为,请不要作出攻击发言)" - await self.directly_reply(raw_message, event.user_id, event.group_id) - - if isinstance(event, GroupRecallNoticeEvent) or isinstance(event, FriendRecallNoticeEvent): - user_info = UserInfo( - user_id=event.user_id, - user_nickname=get_user_nickname(event.user_id) or None, - user_cardname=get_user_cardname(event.user_id) or None, - platform="qq", - ) - - group_info = GroupInfo(group_id=event.group_id, group_name=None, platform="qq") - - chat = await chat_manager.get_or_create_stream( - platform=user_info.platform, user_info=user_info, group_info=group_info - ) - - await self.storage.store_recalled_message(event.message_id, time.time(), chat) - - - - async def handle_message(self, event: MessageEvent, bot: Bot) -> None: - """处理收到的消息""" - - self.bot = bot # 更新 bot 实例 - - # 用户屏蔽,不区分私聊/群聊 - if event.user_id in global_config.ban_user_id: - return - - if ( - event.reply - and hasattr(event.reply, "sender") - and hasattr(event.reply.sender, "user_id") - and event.reply.sender.user_id in global_config.ban_user_id - ): - logger.debug(f"跳过处理回复来自被ban用户 {event.reply.sender.user_id} 的消息") - return - # 处理私聊消息 - if isinstance(event, PrivateMessageEvent): - if not global_config.enable_friend_chat: # 私聊过滤 - return - else: - try: - user_info = UserInfo( - user_id=event.user_id, - user_nickname=(await bot.get_stranger_info(user_id=event.user_id, no_cache=True))["nickname"], - user_cardname=None, - platform="qq", - ) - except Exception as e: - logger.error(f"获取陌生人信息失败: {e}") - return - logger.debug(user_info) - - # group_info = GroupInfo(group_id=0, group_name="私聊", platform="qq") - group_info = None - - # 处理群聊消息 - else: - # 白名单设定由nontbot侧完成 - if event.group_id: - if event.group_id not in global_config.talk_allowed_groups: - return - - user_info = UserInfo( - user_id=event.user_id, - user_nickname=event.sender.nickname, - user_cardname=event.sender.card or None, - platform="qq", - ) - - group_info = GroupInfo(group_id=event.group_id, group_name=None, platform="qq") - - # group_info = await bot.get_group_info(group_id=event.group_id) - # sender_info = await bot.get_group_member_info(group_id=event.group_id, user_id=event.user_id, no_cache=True) - - message_cq = MessageRecvCQ( - message_id=event.message_id, - user_info=user_info, - raw_message=str(event.original_message), - group_info=group_info, - reply_message=event.reply, - platform="qq", - ) + async def message_process(self, message_cq: MessageRecvCQ) -> None: + """处理转化后的统一格式消息 + 1. 过滤消息 + 2. 记忆激活 + 3. 意愿激活 + 4. 生成回复并发送 + 5. 更新关系 + 6. 更新情绪 + """ message_json = message_cq.to_dict() # 进入maimbot @@ -178,7 +80,9 @@ class ChatBot: await relationship_manager.update_relationship( chat_stream=chat, ) - await relationship_manager.update_relationship_value(chat_stream=chat, relationship_value=0.5) + await relationship_manager.update_relationship_value( + chat_stream=chat, relationship_value=0.5 + ) await message.process() # 过滤词 @@ -199,12 +103,16 @@ class ChatBot: logger.info(f"[正则表达式过滤]消息匹配到{pattern},filtered") return - current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(messageinfo.time)) + current_time = time.strftime( + "%Y-%m-%d %H:%M:%S", time.localtime(messageinfo.time) + ) # topic=await topic_identifier.identify_topic_llm(message.processed_plain_text) topic = "" - interested_rate = await hippocampus.memory_activate_value(message.processed_plain_text) / 100 + interested_rate = ( + await hippocampus.memory_activate_value(message.processed_plain_text) / 100 + ) logger.debug(f"对{message.processed_plain_text}的激活度:{interested_rate}") # logger.info(f"\033[1;32m[主题识别]\033[0m 使用{global_config.topic_extract}主题: {topic}") @@ -261,7 +169,10 @@ class ChatBot: # 找到message,删除 # print(f"开始找思考消息") for msg in container.messages: - if isinstance(msg, MessageThinking) and msg.message_info.message_id == think_id: + if ( + isinstance(msg, MessageThinking) + and msg.message_info.message_id == think_id + ): # print(f"找到思考消息: {msg}") thinking_message = msg container.messages.remove(msg) @@ -355,12 +266,168 @@ class ChatBot: chat_stream=chat, relationship_value=valuedict[emotion[0]] ) # 使用情绪管理器更新情绪 - self.mood_manager.update_mood_from_emotion(emotion[0], global_config.mood_intensity_factor) + self.mood_manager.update_mood_from_emotion( + emotion[0], global_config.mood_intensity_factor + ) # willing_manager.change_reply_willing_after_sent( # chat_stream=chat # ) + async def handle_notice(self, event: NoticeEvent, bot: Bot) -> None: + """处理收到的通知""" + if isinstance(event, PokeNotifyEvent): + # 戳一戳 通知 + # 不处理其他人的戳戳 + if not event.is_tome(): + return + + # 用户屏蔽,不区分私聊/群聊 + if event.user_id in global_config.ban_user_id: + return + + # 白名单模式 + if event.group_id: + if event.group_id not in global_config.talk_allowed_groups: + return + + raw_message = f"[戳了戳]{global_config.BOT_NICKNAME}" # 默认类型 + if info := event.raw_info: + poke_type = info[2].get( + "txt", "戳了戳" + ) # 戳戳类型,例如“拍一拍”、“揉一揉”、“捏一捏” + custom_poke_message = info[4].get( + "txt", "" + ) # 自定义戳戳消息,若不存在会为空字符串 + raw_message = ( + f"[{poke_type}]{global_config.BOT_NICKNAME}{custom_poke_message}" + ) + + raw_message += "(这是一个类似摸摸头的友善行为,而不是恶意行为,请不要作出攻击发言)" + + user_info = UserInfo( + user_id=event.user_id, + user_nickname=( + await bot.get_stranger_info(user_id=event.user_id, no_cache=True) + )["nickname"], + user_cardname=None, + platform="qq", + ) + + if event.group_id: + group_info = GroupInfo( + group_id=event.group_id, group_name=None, platform="qq" + ) + else: + group_info = None + + message_cq = MessageRecvCQ( + message_id=0, + user_info=user_info, + raw_message=str(raw_message), + group_info=group_info, + reply_message=None, + platform="qq", + ) + + await self.message_process(message_cq) + elif isinstance(event, GroupRecallNoticeEvent) or isinstance( + event, FriendRecallNoticeEvent + ): + user_info = UserInfo( + user_id=event.user_id, + user_nickname=get_user_nickname(event.user_id) or None, + user_cardname=get_user_cardname(event.user_id) or None, + platform="qq", + ) + + group_info = GroupInfo( + group_id=event.group_id, group_name=None, platform="qq" + ) + + chat = await chat_manager.get_or_create_stream( + platform=user_info.platform, user_info=user_info, group_info=group_info + ) + + await self.storage.store_recalled_message( + event.message_id, time.time(), chat + ) + + async def handle_message(self, event: MessageEvent, bot: Bot) -> None: + """处理收到的消息""" + + self.bot = bot # 更新 bot 实例 + + # 用户屏蔽,不区分私聊/群聊 + if event.user_id in global_config.ban_user_id: + return + + if ( + event.reply + and hasattr(event.reply, "sender") + and hasattr(event.reply.sender, "user_id") + and event.reply.sender.user_id in global_config.ban_user_id + ): + logger.debug( + f"跳过处理回复来自被ban用户 {event.reply.sender.user_id} 的消息" + ) + return + # 处理私聊消息 + if isinstance(event, PrivateMessageEvent): + if not global_config.enable_friend_chat: # 私聊过滤 + return + else: + try: + user_info = UserInfo( + user_id=event.user_id, + user_nickname=( + await bot.get_stranger_info( + user_id=event.user_id, no_cache=True + ) + )["nickname"], + user_cardname=None, + platform="qq", + ) + except Exception as e: + logger.error(f"获取陌生人信息失败: {e}") + return + logger.debug(user_info) + + # group_info = GroupInfo(group_id=0, group_name="私聊", platform="qq") + group_info = None + + # 处理群聊消息 + else: + # 白名单设定由nontbot侧完成 + if event.group_id: + if event.group_id not in global_config.talk_allowed_groups: + return + + user_info = UserInfo( + user_id=event.user_id, + user_nickname=event.sender.nickname, + user_cardname=event.sender.card or None, + platform="qq", + ) + + group_info = GroupInfo( + group_id=event.group_id, group_name=None, platform="qq" + ) + + # group_info = await bot.get_group_info(group_id=event.group_id) + # sender_info = await bot.get_group_member_info(group_id=event.group_id, user_id=event.user_id, no_cache=True) + + message_cq = MessageRecvCQ( + message_id=event.message_id, + user_info=user_info, + raw_message=str(event.original_message), + group_info=group_info, + reply_message=event.reply, + platform="qq", + ) + + await self.message_process(message_cq) + async def directly_reply(self, raw_message: str, user_id: int, group_id: int): """ 直接回复发来的消息,不经过意愿管理器 @@ -402,7 +469,9 @@ class ChatBot: platform=messageinfo.platform, ) - current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(messageinfo.time)) + current_time = time.strftime( + "%Y-%m-%d %H:%M:%S", time.localtime(messageinfo.time) + ) logger.info( f"[{current_time}][{chat.group_info.group_name if chat.group_info else '私聊'}]{chat.user_info.user_nickname}:" f"{message.processed_plain_text}" From 47868428650b985f5a5033944dae0d924cc38f0e Mon Sep 17 00:00:00 2001 From: Oct-autumn Date: Fri, 14 Mar 2025 15:32:10 +0800 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E2=80=9C?= =?UTF-8?q?=E6=88=B3=E4=B8=80=E6=88=B3=E2=80=9D=E4=B8=8E=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E8=80=83=E8=99=91=E5=9B=9E=E5=A4=8D=E7=9A=84=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=20=20=20-=20?= =?UTF-8?q?=E5=B0=86handle=5Fmessage=E5=87=BD=E6=95=B0=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=AE=9E=E4=BE=8B=E5=A4=84=E7=90=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=8F=90=E5=8F=96=E5=87=BA=E6=9D=A5=EF=BC=8C=E5=BD=A2?= =?UTF-8?q?=E6=88=90message=5Fprocess=E5=87=BD=E6=95=B0=EF=BC=88=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E4=BB=A3=E7=A0=81=E5=A4=8D=E7=94=A8=E7=8E=87=EF=BC=89?= =?UTF-8?q?=20=20=20-=20=E5=B0=86=E2=80=9C=E6=88=B3=E4=B8=80=E6=88=B3?= =?UTF-8?q?=E2=80=9D=E7=9A=84=E9=80=9A=E7=9F=A5=E5=A4=84=E7=90=86=E4=B8=BA?= =?UTF-8?q?=E4=B8=80=E6=9D=A1=E9=80=9A=E7=94=A8=E6=B6=88=E6=81=AF=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=EF=BC=8C=E4=BA=A4=E7=94=B1message=5Fprocess=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=A4=84=E7=90=86=20=20=20-=20=E5=90=8C=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E7=94=B1=E4=BA=8E=E4=BD=BF=E7=94=A8=E4=BA=86=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E6=B6=88=E6=81=AF=E5=AE=9E=E4=BE=8B=EF=BC=8C=E2=80=9C?= =?UTF-8?q?=E6=88=B3=E4=B8=80=E6=88=B3=E2=80=9D=E7=9A=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=B0=86=E4=B8=8E=E5=85=B6=E4=BB=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=A4=84=E7=90=86=E7=BB=9F=E4=B8=80=E7=BB=8F=E8=BF=87?= =?UTF-8?q?=E7=BE=A4=E7=BB=84=E6=9D=83=E9=99=90=E9=89=B4=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/chat/bot.py | 311 +++++++++++++++++++--------------------- 1 file changed, 147 insertions(+), 164 deletions(-) diff --git a/src/plugins/chat/bot.py b/src/plugins/chat/bot.py index 704a9a18d..7681345e3 100644 --- a/src/plugins/chat/bot.py +++ b/src/plugins/chat/bot.py @@ -55,112 +55,7 @@ class ChatBot: if not self._started: self._started = True - async def handle_notice(self, event: NoticeEvent, bot: Bot) -> None: - """处理收到的通知""" - # 戳一戳通知 - if isinstance(event, PokeNotifyEvent): - # 不处理其他人的戳戳 - if not event.is_tome(): - return - - # 用户屏蔽,不区分私聊/群聊 - if event.user_id in global_config.ban_user_id: - return - - reply_poke_probability = 1.0 # 回复戳一戳的概率,如果要改可以在这里改,暂不提取到配置文件 - - if random() < reply_poke_probability: - raw_message = "[戳了戳]你" # 默认类型 - if info := event.raw_info: - poke_type = info[2].get("txt", "戳了戳") # 戳戳类型,例如“拍一拍”、“揉一揉”、“捏一捏” - custom_poke_message = info[4].get("txt", "") # 自定义戳戳消息,若不存在会为空字符串 - raw_message = f"[{poke_type}]你{custom_poke_message}" - - raw_message += "(这是一个类似摸摸头的友善行为,而不是恶意行为,请不要作出攻击发言)" - await self.directly_reply(raw_message, event.user_id, event.group_id) - - if isinstance(event, GroupRecallNoticeEvent) or isinstance(event, FriendRecallNoticeEvent): - user_info = UserInfo( - user_id=event.user_id, - user_nickname=get_user_nickname(event.user_id) or None, - user_cardname=get_user_cardname(event.user_id) or None, - platform="qq", - ) - - group_info = GroupInfo(group_id=event.group_id, group_name=None, platform="qq") - - chat = await chat_manager.get_or_create_stream( - platform=user_info.platform, user_info=user_info, group_info=group_info - ) - - await self.storage.store_recalled_message(event.message_id, time.time(), chat) - - async def handle_message(self, event: MessageEvent, bot: Bot) -> None: - """处理收到的消息""" - - self.bot = bot # 更新 bot 实例 - - # 用户屏蔽,不区分私聊/群聊 - if event.user_id in global_config.ban_user_id: - return - - if ( - event.reply - and hasattr(event.reply, "sender") - and hasattr(event.reply.sender, "user_id") - and event.reply.sender.user_id in global_config.ban_user_id - ): - logger.debug(f"跳过处理回复来自被ban用户 {event.reply.sender.user_id} 的消息") - return - # 处理私聊消息 - - - if isinstance(event, PrivateMessageEvent): - if not global_config.enable_friend_chat: # 私聊过滤 - return - else: - try: - user_info = UserInfo( - user_id=event.user_id, - user_nickname=(await bot.get_stranger_info(user_id=event.user_id, no_cache=True))["nickname"], - user_cardname=None, - platform="qq", - ) - except Exception as e: - logger.error(f"获取陌生人信息失败: {e}") - return - logger.debug(user_info) - - # group_info = GroupInfo(group_id=0, group_name="私聊", platform="qq") - group_info = None - - # 处理群聊消息 - else: - # 白名单设定由nontbot侧完成 - if event.group_id: - if event.group_id not in global_config.talk_allowed_groups: - return - - user_info = UserInfo( - user_id=event.user_id, - user_nickname=event.sender.nickname, - user_cardname=event.sender.card or None, - platform="qq", - ) - - group_info = GroupInfo(group_id=event.group_id, group_name=None, platform="qq") - - # group_info = await bot.get_group_info(group_id=event.group_id) - # sender_info = await bot.get_group_member_info(group_id=event.group_id, user_id=event.user_id, no_cache=True) - - message_cq = MessageRecvCQ( - message_id=event.message_id, - user_info=user_info, - raw_message=str(event.original_message), - group_info=group_info, - reply_message=event.reply, - platform="qq", - ) + async def message_process(self, message_cq:MessageRecvCQ): await message_cq.initialize() message_json = message_cq.to_dict() # 哦我嘞个json @@ -364,72 +259,160 @@ class ChatBot: # chat_stream=chat # ) - async def directly_reply(self, raw_message: str, user_id: int, group_id: int): - """ - 直接回复发来的消息,不经过意愿管理器 - """ + async def handle_notice(self, event: NoticeEvent, bot: Bot) -> None: + """处理收到的通知""" + if isinstance(event, PokeNotifyEvent): + # 戳一戳 通知 + # 不处理其他人的戳戳 + if not event.is_tome(): + return - # 构造用户信息和群组信息 - user_info = UserInfo( - user_id=user_id, - user_nickname=get_user_nickname(user_id) or None, - user_cardname=get_user_cardname(user_id) or None, - platform="qq", - ) - group_info = GroupInfo(group_id=group_id, group_name=None, platform="qq") + # 用户屏蔽,不区分私聊/群聊 + if event.user_id in global_config.ban_user_id: + return + + # 白名单模式 + if event.group_id: + if event.group_id not in global_config.talk_allowed_groups: + return + + raw_message = f"[戳了戳]{global_config.BOT_NICKNAME}" # 默认类型 + if info := event.raw_info: + poke_type = info[2].get( + "txt", "戳了戳" + ) # 戳戳类型,例如“拍一拍”、“揉一揉”、“捏一捏” + custom_poke_message = info[4].get( + "txt", "" + ) # 自定义戳戳消息,若不存在会为空字符串 + raw_message = ( + f"[{poke_type}]{global_config.BOT_NICKNAME}{custom_poke_message}" + ) + + raw_message += "(这是一个类似摸摸头的友善行为,而不是恶意行为,请不要作出攻击发言)" + + user_info = UserInfo( + user_id=event.user_id, + user_nickname=( + await bot.get_stranger_info(user_id=event.user_id, no_cache=True) + )["nickname"], + user_cardname=None, + platform="qq", + ) + + if event.group_id: + group_info = GroupInfo( + group_id=event.group_id, group_name=None, platform="qq" + ) + else: + group_info = None + + message_cq = MessageRecvCQ( + message_id=0, + user_info=user_info, + raw_message=str(raw_message), + group_info=group_info, + reply_message=None, + platform="qq", + ) + + await self.message_process(message_cq) + + elif isinstance(event, GroupRecallNoticeEvent) or isinstance( + event, FriendRecallNoticeEvent + ): + user_info = UserInfo( + user_id=event.user_id, + user_nickname=get_user_nickname(event.user_id) or None, + user_cardname=get_user_cardname(event.user_id) or None, + platform="qq", + ) + + group_info = GroupInfo( + group_id=event.group_id, group_name=None, platform="qq" + ) + + chat = await chat_manager.get_or_create_stream( + platform=user_info.platform, user_info=user_info, group_info=group_info + ) + + await self.storage.store_recalled_message( + event.message_id, time.time(), chat + ) + + async def handle_message(self, event: MessageEvent, bot: Bot) -> None: + """处理收到的消息""" + + self.bot = bot # 更新 bot 实例 + + # 用户屏蔽,不区分私聊/群聊 + if event.user_id in global_config.ban_user_id: + return + + if ( + event.reply + and hasattr(event.reply, "sender") + and hasattr(event.reply.sender, "user_id") + and event.reply.sender.user_id in global_config.ban_user_id + ): + logger.debug( + f"跳过处理回复来自被ban用户 {event.reply.sender.user_id} 的消息" + ) + return + # 处理私聊消息 + if isinstance(event, PrivateMessageEvent): + if not global_config.enable_friend_chat: # 私聊过滤 + return + else: + try: + user_info = UserInfo( + user_id=event.user_id, + user_nickname=( + await bot.get_stranger_info( + user_id=event.user_id, no_cache=True + ) + )["nickname"], + user_cardname=None, + platform="qq", + ) + except Exception as e: + logger.error(f"获取陌生人信息失败: {e}") + return + logger.debug(user_info) + + # group_info = GroupInfo(group_id=0, group_name="私聊", platform="qq") + group_info = None + + # 处理群聊消息 + else: + # 白名单设定由nontbot侧完成 + if event.group_id: + if event.group_id not in global_config.talk_allowed_groups: + return + + user_info = UserInfo( + user_id=event.user_id, + user_nickname=event.sender.nickname, + user_cardname=event.sender.card or None, + platform="qq", + ) + + group_info = GroupInfo( + group_id=event.group_id, group_name=None, platform="qq" + ) + + # group_info = await bot.get_group_info(group_id=event.group_id) + # sender_info = await bot.get_group_member_info(group_id=event.group_id, user_id=event.user_id, no_cache=True) message_cq = MessageRecvCQ( - message_id=None, + message_id=event.message_id, user_info=user_info, - raw_message=raw_message, + raw_message=str(event.original_message), group_info=group_info, - reply_message=None, + reply_message=event.reply, platform="qq", ) - await message_cq.initialize() - message_json = message_cq.to_dict() - message = MessageRecv(message_json) - groupinfo = message.message_info.group_info - userinfo = message.message_info.user_info - messageinfo = message.message_info - - chat = await chat_manager.get_or_create_stream( - platform=messageinfo.platform, user_info=userinfo, group_info=groupinfo - ) - message.update_chat_stream(chat) - await message.process() - - bot_user_info = UserInfo( - user_id=global_config.BOT_QQ, - user_nickname=global_config.BOT_NICKNAME, - platform=messageinfo.platform, - ) - - current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(messageinfo.time)) - logger.info( - f"[{current_time}][{chat.group_info.group_name if chat.group_info else '私聊'}]{chat.user_info.user_nickname}:" - f"{message.processed_plain_text}" - ) - - # 使用大模型生成回复 - response, raw_content = await self.gpt.generate_response(message) - - if response: - for msg in response: - message_segment = Seg(type="text", data=msg) - - bot_message = MessageSending( - message_id=None, - chat_stream=chat, - bot_user_info=bot_user_info, - sender_info=userinfo, - message_segment=message_segment, - reply=None, - is_head=False, - is_emoji=False, - ) - message_manager.add_message(bot_message) + await self.message_process(message_cq) # 创建全局ChatBot实例 From 9a0267df48204ebc6342511c08d185e0552bd04e Mon Sep 17 00:00:00 2001 From: Cookie987 Date: Fri, 14 Mar 2025 22:13:57 +0800 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20Linux=E4=B8=80=E9=94=AE=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E8=84=9A=E6=9C=AC=E9=80=82=E9=85=8D=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E5=88=86=E6=94=AF=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/run.sh b/run.sh index c3f6969b6..663fc8a67 100644 --- a/run.sh +++ b/run.sh @@ -97,8 +97,8 @@ check_python() { # 5/6: 选择分支 choose_branch() { BRANCH=$(whiptail --title "🔀 [5/6] 选择 Maimbot 分支" --menu "请选择要安装的 Maimbot 分支:" 15 60 2 \ - "main" "稳定版本(推荐)" \ - "debug" "开发版本(可能不稳定)" 3>&1 1>&2 2>&3) + "main" "稳定版本(推荐,供下载使用)" \ + "main-fix" "生产环境紧急修复" 3>&1 1>&2 2>&3) if [[ -z "$BRANCH" ]]; then BRANCH="main" @@ -201,6 +201,8 @@ install_napcat() { } # 运行安装步骤 +whiptail --title "⚠️ 警告:安装前详阅" --msgbox "项目处于活跃开发阶段,代码可能随时更改\n文档未完善,有问题可以提交 Issue 或者 Discussion\nQQ机器人存在被限制风险,请自行了解,谨慎使用\n由于持续迭代,可能存在一些已知或未知的bug\n由于开发中,可能消耗较多token\n\n本脚本可能更新不及时,如遇到bug请优先尝试手动部署以确定是否为脚本问题" 14 60 + check_system check_mongodb check_napcat @@ -233,7 +235,7 @@ fi if [[ "$IS_INSTALL_NAPCAT" == "true" ]]; then echo -e "${GREEN}安装 NapCat...${RESET}" - curl -o napcat.sh https://nclatest.znin.net/NapNeko/NapCat-Installer/main/script/install.sh && bash napcat.sh + curl -o napcat.sh https://nclatest.znin.net/NapNeko/NapCat-Installer/main/script/install.sh && bash napcat.sh --cli y --docker n fi echo -e "${GREEN}创建 Python 虚拟环境...${RESET}" From 1ec6892f33f8ac1c8f9635ec153ab361f4d7fb7e Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Fri, 14 Mar 2025 23:41:39 +0800 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=BF=9D=E7=A6=81?= =?UTF-8?q?=E8=AF=8D=E5=88=97=E8=A1=A8=E7=94=B1=E4=BA=8E=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E5=B9=B2=E5=87=80=E5=AF=BC=E8=87=B4=E5=85=A8?= =?UTF-8?q?=E9=80=89=E4=BA=86=EF=BC=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webui.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/webui.py b/webui.py index bd2e9ee0b..2c99ba8fa 100644 --- a/webui.py +++ b/webui.py @@ -165,7 +165,10 @@ def format_list_to_str_alias(lst): format_list_to_str([1, "two", 3.0]) '[1, "two", 3.0]' """ - resarr = lst.split(", ") + resarr = [] + if len(lst) != 0: + resarr = lst.split(", ") + return resarr def format_list_to_int(lst): @@ -729,24 +732,24 @@ with gr.Blocks(title="MaimBot配置文件编辑") as app: with gr.Row(): ban_msgs_regex_list_display = gr.TextArea( value="\n".join(ban_msgs_regex_list), - label="违禁词列表", + label="违禁消息正则列表", interactive=False, lines=5 ) with gr.Row(): with gr.Column(scale=3): - ban_msgs_regex_new_item_input = gr.Textbox(label="添加新违禁词") + ban_msgs_regex_new_item_input = gr.Textbox(label="添加新违禁消息正则") ban_msgs_regex_add_btn = gr.Button("添加", scale=1) with gr.Row(): with gr.Column(scale=3): ban_msgs_regex_item_to_delete = gr.Dropdown( choices=ban_msgs_regex_list, - label="选择要删除的违禁词" + label="选择要删除的违禁消息正则" ) ban_msgs_regex_delete_btn = gr.Button("删除", scale=1) - ban_msgs_regex_final_result = gr.Text(label="修改后的违禁词") + ban_msgs_regex_final_result = gr.Text(label="修改后的违禁消息正则") ban_msgs_regex_add_btn.click( add_item, inputs=[ban_msgs_regex_new_item_input, ban_msgs_regex_list_state], From dc054d456b8cffc2a28ca1c0ec21eacb99b10928 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Fri, 14 Mar 2025 23:45:28 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86willing=E7=9A=84?= =?UTF-8?q?classical?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/chat/bot.py | 1 - src/plugins/willing/mode_classical.py | 18 ++++++++++-------- template/bot_config_template.toml | 8 ++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/plugins/chat/bot.py b/src/plugins/chat/bot.py index 704a9a18d..56efd825d 100644 --- a/src/plugins/chat/bot.py +++ b/src/plugins/chat/bot.py @@ -216,7 +216,6 @@ class ChatBot: is_mentioned = is_mentioned_bot_in_message(message) reply_probability = await willing_manager.change_reply_willing_received( chat_stream=chat, - topic=topic[0] if topic else None, is_mentioned_bot=is_mentioned, config=global_config, is_emoji=message.is_emoji, diff --git a/src/plugins/willing/mode_classical.py b/src/plugins/willing/mode_classical.py index 1e17130be..dbd7cf3f7 100644 --- a/src/plugins/willing/mode_classical.py +++ b/src/plugins/willing/mode_classical.py @@ -28,7 +28,6 @@ class WillingManager: async def change_reply_willing_received(self, chat_stream: ChatStream, - topic: str = None, is_mentioned_bot: bool = False, config = None, is_emoji: bool = False, @@ -37,14 +36,14 @@ class WillingManager: """改变指定聊天流的回复意愿并返回回复概率""" chat_id = chat_stream.stream_id current_willing = self.chat_reply_willing.get(chat_id, 0) - - if topic and current_willing < 1: - current_willing += 0.2 - elif topic: - current_willing += 0.05 + + interested_rate = interested_rate * config.response_interested_rate_amplifier + + if interested_rate > 0.5: + current_willing += (interested_rate - 0.5) if is_mentioned_bot and current_willing < 1.0: - current_willing += 0.9 + current_willing += 1 elif is_mentioned_bot: current_willing += 0.05 @@ -53,7 +52,10 @@ class WillingManager: self.chat_reply_willing[chat_id] = min(current_willing, 3.0) - reply_probability = (current_willing - 0.5) * 2 + + reply_probability = min(max((current_willing - 0.5),0.03)* config.response_willing_amplifier * 2,1) + + reply_probability = reply_probability # 检查群组权限(如果是群聊) if chat_stream.group_info and config: diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index 16a28d96e..89ebbe162 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -20,14 +20,14 @@ alias_names = ["小麦", "阿麦"] [personality] prompt_personality = [ - "曾经是一个学习地质的女大学生,现在学习心理学和脑科学,你会刷贴吧", # 贴吧人格 - "是一个女大学生,你有黑色头发,你会刷小红书", # 小红书人格 - "是一个女大学生,你会刷b站,对ACG文化感兴趣" # b站人格 + "用一句话或几句话描述性格特点和其他特征", + "用一句话或几句话描述性格特点和其他特征", + "例如,是一个热爱国家热爱党的新时代好青年" ] personality_1_probability = 0.6 # 第一种人格出现概率 personality_2_probability = 0.3 # 第二种人格出现概率 personality_3_probability = 0.1 # 第三种人格出现概率,请确保三个概率相加等于1 -prompt_schedule = "一个曾经学习地质,现在学习心理学和脑科学的女大学生,喜欢刷qq,贴吧,知乎和小红书" +prompt_schedule = "用一句话或几句话描述描述性格特点和其他特征" [message] min_text_length = 2 # 与麦麦聊天时麦麦只会回答文本大于等于此数的消息 From 438ba009b24a151dbc7724a2e20f36e187b498ce Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 15 Mar 2025 00:00:06 +0800 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20=E7=81=AB=E9=80=9F=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?config=E6=96=87=E4=BB=B6=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=97=B6=E5=A4=87=E4=BB=BD=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webui.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/webui.py b/webui.py index 2c99ba8fa..a9041749b 100644 --- a/webui.py +++ b/webui.py @@ -211,7 +211,18 @@ def save_trigger(server_address, server_port, final_result_list,t_mongodb_host,t #============================================== #主要配置文件保存函数 def save_config_to_file(t_config_data): - with open("config/bot_config.toml", "w", encoding="utf-8") as f: + filename = "config/bot_config.toml" + backup_filename = f"{filename}.bak" + if not os.path.exists(backup_filename): + if os.path.exists(filename): + logger.info(f"{filename} 已存在,正在备份到 {backup_filename}...") + shutil.copy(filename, backup_filename) # 备份文件 + logger.success(f"文件已备份到 {backup_filename}") + else: + logger.warning(f"{filename} 不存在,无法进行备份。") + + + with open(filename, "w", encoding="utf-8") as f: toml.dump(t_config_data, f) logger.success("配置已保存到 bot_config.toml 文件中") def save_bot_config(t_qqbot_qq, t_nickname,t_nickname_final_result): From 419bd56e30e988668d33c564045b1c719c2dff33 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Sat, 15 Mar 2025 01:04:53 +0800 Subject: [PATCH 7/7] Update mode_classical.py --- src/plugins/willing/mode_classical.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/plugins/willing/mode_classical.py b/src/plugins/willing/mode_classical.py index dbd7cf3f7..14ae81c7a 100644 --- a/src/plugins/willing/mode_classical.py +++ b/src/plugins/willing/mode_classical.py @@ -11,10 +11,9 @@ class WillingManager: async def _decay_reply_willing(self): """定期衰减回复意愿""" while True: - await asyncio.sleep(3) + await asyncio.sleep(1) for chat_id in self.chat_reply_willing: - # 每分钟衰减10%的回复意愿 - self.chat_reply_willing[chat_id] = max(0, self.chat_reply_willing[chat_id] * 0.6) + self.chat_reply_willing[chat_id] = max(0, self.chat_reply_willing[chat_id] * 0.9) def get_willing(self, chat_stream: ChatStream) -> float: """获取指定聊天流的回复意愿""" @@ -54,8 +53,6 @@ class WillingManager: reply_probability = min(max((current_willing - 0.5),0.03)* config.response_willing_amplifier * 2,1) - - reply_probability = reply_probability # 检查群组权限(如果是群聊) if chat_stream.group_info and config: @@ -65,9 +62,6 @@ class WillingManager: if chat_stream.group_info.group_id in config.talk_frequency_down_groups: reply_probability = reply_probability / 3.5 - - if is_mentioned_bot and sender_id == "1026294844": - reply_probability = 1 return reply_probability