Update bot.py

pull/333/head
Pocketfans 2025-03-13 17:33:05 +08:00
parent 14f3d318ec
commit 48992cf796
1 changed files with 99 additions and 174 deletions

View File

@ -1,32 +1,26 @@
import re
import time import time
from random import random from random import random
from loguru import logger from loguru import logger
from nonebot.adapters.onebot.v11 import ( from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent
Bot,
GroupMessageEvent,
MessageEvent,
PrivateMessageEvent,
)
from ..memory_system.memory import hippocampus from ..memory_system.memory import hippocampus
from ..moods.moods import MoodManager # 导入情绪管理器 from ..moods.moods import MoodManager # 导入情绪管理器
from .config import global_config from .config import global_config
from .cq_code import CQCode # 导入CQCode模块
from .emoji_manager import emoji_manager # 导入表情包管理器 from .emoji_manager import emoji_manager # 导入表情包管理器
from .llm_generator import ResponseGenerator from .llm_generator import ResponseGenerator
from .message import MessageSending, MessageRecv, MessageThinking, MessageSet from .message import (
from .message_cq import ( Message,
MessageRecvCQ, Message_Sending,
Message_Thinking, # 导入 Message_Thinking 类
MessageSet,
) )
from .chat_stream import chat_manager
from .message_sender import message_manager # 导入新的消息管理器 from .message_sender import message_manager # 导入新的消息管理器
from .relationship_manager import relationship_manager from .relationship_manager import relationship_manager
from .storage import MessageStorage from .storage import MessageStorage
from .utils import calculate_typing_time, is_mentioned_bot_in_message from .utils import calculate_typing_time, is_mentioned_bot_in_txt
from .utils_image import image_path_to_base64
from .willing_manager import willing_manager # 导入意愿管理器 from .willing_manager import willing_manager # 导入意愿管理器
from .message_base import UserInfo, GroupInfo, Seg
class ChatBot: class ChatBot:
@ -46,257 +40,188 @@ class ChatBot:
if not self._started: if not self._started:
self._started = True self._started = True
async def handle_message(self, event: MessageEvent, bot: Bot) -> None: async def handle_message(self, event: GroupMessageEvent, bot: Bot) -> None:
"""处理收到的消息""" """处理收到的消息"""
if event.group_id not in global_config.talk_allowed_groups:
return
self.bot = bot # 更新 bot 实例 self.bot = bot # 更新 bot 实例
# 用户屏蔽,不区分私聊/群聊
if event.user_id in global_config.ban_user_id: if event.user_id in global_config.ban_user_id:
return return
# 处理私聊消息 group_info = await bot.get_group_info(group_id=event.group_id)
if isinstance(event, PrivateMessageEvent): sender_info = await bot.get_group_member_info(group_id=event.group_id, user_id=event.user_id, no_cache=True)
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") await relationship_manager.update_relationship(user_id=event.user_id, data=sender_info)
group_info = None await relationship_manager.update_relationship_value(user_id=event.user_id, relationship_value=0.5)
# 处理群聊消息 message = Message(
else: group_id=event.group_id,
# 白名单设定由nontbot侧完成 user_id=event.user_id,
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, message_id=event.message_id,
user_info=user_info, user_cardname=sender_info['card'],
raw_message=str(event.original_message), raw_message=str(event.original_message),
group_info=group_info, plain_text=event.get_plaintext(),
reply_message=event.reply, reply_message=event.reply,
platform="qq",
) )
message_json = message_cq.to_dict() await message.initialize()
# 进入maimbot
message = MessageRecv(message_json)
groupinfo = message.message_info.group_info
userinfo = message.message_info.user_info
messageinfo = message.message_info
# 消息过滤涉及到config有待更新
chat = await chat_manager.get_or_create_stream(
platform=messageinfo.platform, user_info=userinfo, group_info=groupinfo
)
message.update_chat_stream(chat)
await relationship_manager.update_relationship(
chat_stream=chat,
)
await relationship_manager.update_relationship_value(chat_stream=chat, relationship_value=0.5)
await message.process()
# 过滤词 # 过滤词
for word in global_config.ban_words: for word in global_config.ban_words:
if word in message.processed_plain_text: if word in message.detailed_plain_text:
logger.info( logger.info(
f"[{chat.group_info.group_name if chat.group_info.group_id else '私聊'}]{userinfo.user_nickname}:{message.processed_plain_text}" f"[{message.group_name}]{message.user_nickname}:{message.processed_plain_text}")
)
logger.info(f"[过滤词识别]消息中含有{word}filtered") logger.info(f"[过滤词识别]消息中含有{word}filtered")
return return
# 正则表达式过滤 current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(message.time))
for pattern in global_config.ban_msgs_regex:
if re.search(pattern, message.raw_message):
logger.info(
f"[{chat.group_info.group_name if chat.group_info.group_id else '私聊'}]{userinfo.user_nickname}:{message.raw_message}"
)
logger.info(f"[正则表达式过滤]消息匹配到{pattern}filtered")
return
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=await topic_identifier.identify_topic_llm(message.processed_plain_text)
topic = ''
topic = "" interested_rate = 0
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.debug(f"{message.processed_plain_text}"
f"的激活度:{interested_rate}")
# logger.info(f"\033[1;32m[主题识别]\033[0m 使用{global_config.topic_extract}主题: {topic}") # logger.info(f"\033[1;32m[主题识别]\033[0m 使用{global_config.topic_extract}主题: {topic}")
await self.storage.store_message(message, chat, topic[0] if topic else None) await self.storage.store_message(message, topic[0] if topic else None)
is_mentioned = is_mentioned_bot_in_message(message) is_mentioned = is_mentioned_bot_in_txt(message.processed_plain_text)
reply_probability = await willing_manager.change_reply_willing_received( reply_probability = willing_manager.change_reply_willing_received(
chat_stream=chat, event.group_id,
topic=topic[0] if topic else None, topic[0] if topic else None,
is_mentioned_bot=is_mentioned, is_mentioned,
config=global_config, global_config,
is_emoji=message.is_emoji, event.user_id,
interested_rate=interested_rate, message.is_emoji,
interested_rate
) )
current_willing = willing_manager.get_willing(chat_stream=chat) current_willing = willing_manager.get_willing(event.group_id)
logger.info( logger.info(
f"[{current_time}][{chat.group_info.group_name if chat.group_info.group_id else '私聊'}]{chat.user_info.user_nickname}:" f"[{current_time}][{message.group_name}]{message.user_nickname}:"
f"{message.processed_plain_text}[回复意愿:{current_willing:.2f}][概率:{reply_probability * 100:.1f}%]" f"{message.processed_plain_text}[回复意愿:{current_willing:.2f}][概率:{reply_probability * 100:.1f}%]")
)
response = None response = ""
if random() < reply_probability: if random() < reply_probability:
bot_user_info = UserInfo( tinking_time_point = round(time.time(), 2)
user_id=global_config.BOT_QQ, think_id = 'mt' + str(tinking_time_point)
user_nickname=global_config.BOT_NICKNAME, thinking_message = Message_Thinking(message=message, message_id=think_id)
platform=messageinfo.platform,
)
thinking_time_point = round(time.time(), 2)
think_id = "mt" + str(thinking_time_point)
thinking_message = MessageThinking(
message_id=think_id,
chat_stream=chat,
bot_user_info=bot_user_info,
reply=message,
)
message_manager.add_message(thinking_message) message_manager.add_message(thinking_message)
willing_manager.change_reply_willing_sent(chat) willing_manager.change_reply_willing_sent(thinking_message.group_id)
response, raw_content = await self.gpt.generate_response(message) response, raw_content = await self.gpt.generate_response(message)
# print(f"response: {response}")
if response: if response:
# print(f"有response: {response}") container = message_manager.get_container(event.group_id)
container = message_manager.get_container(chat.stream_id)
thinking_message = None thinking_message = None
# 找到message,删除 # 找到message,删除
# print(f"开始找思考消息")
for msg in container.messages: for msg in container.messages:
if isinstance(msg, MessageThinking) and msg.message_info.message_id == think_id: if isinstance(msg, Message_Thinking) and msg.message_id == think_id:
# print(f"找到思考消息: {msg}")
thinking_message = msg thinking_message = msg
container.messages.remove(msg) container.messages.remove(msg)
# print(f"\033[1;32m[思考消息删除]\033[0m 已找到思考消息对象,开始删除")
break break
# 如果找不到思考消息,直接返回 # 如果找不到思考消息,直接返回
if not thinking_message: if not thinking_message:
logger.warning("未找到对应的思考消息,可能已超时被移除") logger.warning(f"未找到对应的思考消息,可能已超时被移除")
return return
# 记录开始思考的时间,避免从思考到回复的时间太久 # 记录开始思考的时间,避免从思考到回复的时间太久
thinking_start_time = thinking_message.thinking_start_time thinking_start_time = thinking_message.thinking_start_time
message_set = MessageSet(chat, think_id) message_set = MessageSet(event.group_id, global_config.BOT_QQ,
think_id) # 发送消息的id和产生发送消息的message_thinking是一致的
# 计算打字时间1是为了模拟打字2是避免多条回复乱序 # 计算打字时间1是为了模拟打字2是避免多条回复乱序
accu_typing_time = 0 accu_typing_time = 0
# print(f"\033[1;32m[开始回复]\033[0m 开始将回复1载入发送容器")
mark_head = False mark_head = False
for msg in response: for msg in response:
# print(f"\033[1;32m[回复内容]\033[0m {msg}") # print(f"\033[1;32m[回复内容]\033[0m {msg}")
# 通过时间改变时间戳 # 通过时间改变时间戳
typing_time = calculate_typing_time(msg) typing_time = calculate_typing_time(msg)
logger.debug(f"typing_time: {typing_time}")
accu_typing_time += typing_time accu_typing_time += typing_time
timepoint = thinking_time_point + accu_typing_time timepoint = tinking_time_point + accu_typing_time
message_segment = Seg(type="text", data=msg)
# logger.debug(f"message_segment: {message_segment}") bot_message = Message_Sending(
bot_message = MessageSending( group_id=event.group_id,
user_id=global_config.BOT_QQ,
message_id=think_id, message_id=think_id,
chat_stream=chat, raw_message=msg,
bot_user_info=bot_user_info, plain_text=msg,
sender_info=userinfo, processed_plain_text=msg,
message_segment=message_segment, user_nickname=global_config.BOT_NICKNAME,
reply=message, group_name=message.group_name,
is_head=not mark_head, time=timepoint, # 记录了回复生成的时间
is_emoji=False, thinking_start_time=thinking_start_time, # 记录了思考开始的时间
reply_message_id=message.message_id
) )
logger.debug(f"bot_message: {bot_message}") await bot_message.initialize()
if not mark_head: if not mark_head:
bot_message.is_head = True
mark_head = True mark_head = True
logger.debug(f"添加消息到message_set: {bot_message}")
message_set.add_message(bot_message) message_set.add_message(bot_message)
# message_set 可以直接加入 message_manager # message_set 可以直接加入 message_manager
# print(f"\033[1;32m[回复]\033[0m 将回复载入发送容器") # print(f"\033[1;32m[回复]\033[0m 将回复载入发送容器")
logger.debug("添加message_set到message_manager")
message_manager.add_message(message_set) message_manager.add_message(message_set)
bot_response_time = thinking_time_point bot_response_time = tinking_time_point
if random() < global_config.emoji_chance: if random() < global_config.emoji_chance:
emoji_raw = await emoji_manager.get_emoji_for_text(response) emoji_raw = await emoji_manager.get_emoji_for_text(response)
# 检查是否 <没有找到> emoji # 检查是否 <没有找到> emoji
if emoji_raw != None: if emoji_raw != None:
emoji_path, description = emoji_raw emoji_path, discription = emoji_raw
emoji_cq = image_path_to_base64(emoji_path) emoji_cq = CQCode.create_emoji_cq(emoji_path)
if random() < 0.5: if random() < 0.5:
bot_response_time = thinking_time_point - 1 bot_response_time = tinking_time_point - 1
else: else:
bot_response_time = bot_response_time + 1 bot_response_time = bot_response_time + 1
message_segment = Seg(type="emoji", data=emoji_cq) bot_message = Message_Sending(
bot_message = MessageSending( group_id=event.group_id,
message_id=think_id, user_id=global_config.BOT_QQ,
chat_stream=chat, message_id=0,
bot_user_info=bot_user_info, raw_message=emoji_cq,
sender_info=userinfo, plain_text=emoji_cq,
message_segment=message_segment, processed_plain_text=emoji_cq,
reply=message, detailed_plain_text=discription,
is_head=False, user_nickname=global_config.BOT_NICKNAME,
group_name=message.group_name,
time=bot_response_time,
is_emoji=True, is_emoji=True,
translate_cq=False,
thinking_start_time=thinking_start_time,
# reply_message_id=message.message_id
) )
await bot_message.initialize()
message_manager.add_message(bot_message) message_manager.add_message(bot_message)
emotion = await self.gpt._get_emotion_tags(raw_content) emotion = await self.gpt._get_emotion_tags(raw_content)
logger.debug(f"'{response}' 获取到的情感标签为:{emotion}") logger.debug(f"'{response}' 获取到的情感标签为:{emotion}")
valuedict = { valuedict = {
"happy": 0.5, 'happy': 0.5,
"angry": -1, 'angry': -1,
"sad": -0.5, 'sad': -0.5,
"surprised": 0.2, 'surprised': 0.2,
"disgusted": -1.5, 'disgusted': -1.5,
"fearful": -0.7, 'fearful': -0.7,
"neutral": 0.1, 'neutral': 0.1
} }
await relationship_manager.update_relationship_value( await relationship_manager.update_relationship_value(message.user_id,
chat_stream=chat, relationship_value=valuedict[emotion[0]] 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( # willing_manager.change_reply_willing_after_sent(event.group_id)
# chat_stream=chat
# )
# 创建全局ChatBot实例 # 创建全局ChatBot实例