refractor: 几乎写完了,进入测试阶段
This commit is contained in:
@@ -10,18 +10,19 @@ from .config import global_config
|
||||
from .cq_code import CQCode,cq_code_tool # 导入CQCode模块
|
||||
from .emoji_manager import emoji_manager # 导入表情包管理器
|
||||
from .llm_generator import ResponseGenerator
|
||||
from .message import MessageSending, MessageRecv, MessageThinking, MessageSet
|
||||
from .message_cq import (
|
||||
Message,
|
||||
Message_Sending,
|
||||
Message_Thinking, # 导入 Message_Thinking 类
|
||||
MessageSet,
|
||||
MessageRecvCQ,
|
||||
MessageSendCQ,
|
||||
)
|
||||
from .chat_stream import chat_manager
|
||||
from .message_sender import message_manager # 导入新的消息管理器
|
||||
from .relationship_manager import relationship_manager
|
||||
from .storage import MessageStorage
|
||||
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 .message_base import UserInfo, GroupInfo, Seg
|
||||
|
||||
class ChatBot:
|
||||
def __init__(self):
|
||||
@@ -43,12 +44,9 @@ class ChatBot:
|
||||
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 实例
|
||||
|
||||
if event.user_id in global_config.ban_user_id:
|
||||
return
|
||||
|
||||
|
||||
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)
|
||||
@@ -56,25 +54,42 @@ class ChatBot:
|
||||
await relationship_manager.update_relationship(user_id = event.user_id, data = sender_info)
|
||||
await relationship_manager.update_relationship_value(user_id = event.user_id, relationship_value = 0.5)
|
||||
|
||||
message = Message(
|
||||
group_id=event.group_id,
|
||||
user_id=event.user_id,
|
||||
message_cq=MessageRecvCQ(
|
||||
message_id=event.message_id,
|
||||
user_cardname=sender_info['card'],
|
||||
raw_message=str(event.original_message),
|
||||
plain_text=event.get_plaintext(),
|
||||
user_id=event.user_id,
|
||||
raw_message=str(event.original_message),
|
||||
group_id=event.group_id,
|
||||
reply_message=event.reply,
|
||||
platform='qq'
|
||||
)
|
||||
await message.initialize()
|
||||
|
||||
message_json=message_cq.to_dict()
|
||||
message=MessageRecv(**message_json)
|
||||
await message.process()
|
||||
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)
|
||||
if groupinfo:
|
||||
if groupinfo.group_id not in global_config.talk_allowed_groups:
|
||||
return
|
||||
else:
|
||||
if userinfo:
|
||||
if userinfo.user_id in []:
|
||||
pass
|
||||
else:
|
||||
return
|
||||
else:
|
||||
return
|
||||
if userinfo.user_id in global_config.ban_user_id:
|
||||
return
|
||||
# 过滤词
|
||||
for word in global_config.ban_words:
|
||||
if word in message.detailed_plain_text:
|
||||
logger.info(f"\033[1;32m[{message.group_name}]{message.user_nickname}:\033[0m {message.processed_plain_text}")
|
||||
if word in message.processed_plain_text:
|
||||
logger.info(f"\033[1;32m[{groupinfo.group_name}]{userinfo.user_nickname}:\033[0m {message.processed_plain_text}")
|
||||
logger.info(f"\033[1;32m[过滤词识别]\033[0m 消息中含有{word},filtered")
|
||||
return
|
||||
|
||||
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(message.time))
|
||||
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(messageinfo.time))
|
||||
|
||||
|
||||
|
||||
@@ -88,44 +103,58 @@ class ChatBot:
|
||||
await self.storage.store_message(message, topic[0] if topic else None)
|
||||
|
||||
is_mentioned = is_mentioned_bot_in_txt(message.processed_plain_text)
|
||||
reply_probability = willing_manager.change_reply_willing_received(
|
||||
event.group_id,
|
||||
topic[0] if topic else None,
|
||||
is_mentioned,
|
||||
global_config,
|
||||
event.user_id,
|
||||
message.is_emoji,
|
||||
interested_rate
|
||||
reply_probability = await willing_manager.change_reply_willing_received(
|
||||
platform=messageinfo.platform,
|
||||
user_info=userinfo,
|
||||
group_info=groupinfo,
|
||||
topic=topic[0] if topic else None,
|
||||
is_mentioned_bot=is_mentioned,
|
||||
config=global_config,
|
||||
is_emoji=message.is_emoji,
|
||||
interested_rate=interested_rate
|
||||
)
|
||||
current_willing = willing_manager.get_willing(
|
||||
platform=messageinfo.platform,
|
||||
user_info=userinfo,
|
||||
group_info=groupinfo
|
||||
)
|
||||
current_willing = willing_manager.get_willing(event.group_id)
|
||||
|
||||
|
||||
print(f"\033[1;32m[{current_time}][{message.group_name}]{message.user_nickname}:\033[0m {message.processed_plain_text}\033[1;36m[回复意愿:{current_willing:.2f}][概率:{reply_probability * 100:.1f}%]\033[0m")
|
||||
print(f"\033[1;32m[{current_time}][{groupinfo.group_name}]{userinfo.user_nickname}:\033[0m {message.processed_plain_text}\033[1;36m[回复意愿:{current_willing:.2f}][概率:{reply_probability * 100:.1f}%]\033[0m")
|
||||
|
||||
response = ""
|
||||
|
||||
if random() < reply_probability:
|
||||
|
||||
|
||||
bot_user_info=UserInfo(
|
||||
user_id=global_config.BOT_QQ,
|
||||
user_nickname=global_config.BOT_NICKNAME,
|
||||
platform=messageinfo.platform
|
||||
)
|
||||
tinking_time_point = round(time.time(), 2)
|
||||
think_id = 'mt' + str(tinking_time_point)
|
||||
thinking_message = Message_Thinking(message=message,message_id=think_id)
|
||||
thinking_message = MessageThinking.from_chat_stream(
|
||||
chat_stream=chat,
|
||||
message_id=think_id,
|
||||
reply=message
|
||||
)
|
||||
|
||||
message_manager.add_message(thinking_message)
|
||||
|
||||
willing_manager.change_reply_willing_sent(thinking_message.group_id)
|
||||
willing_manager.change_reply_willing_sent(
|
||||
platform=messageinfo.platform,
|
||||
user_info=userinfo,
|
||||
group_info=groupinfo
|
||||
)
|
||||
|
||||
response,raw_content = await self.gpt.generate_response(message)
|
||||
|
||||
if response:
|
||||
container = message_manager.get_container(event.group_id)
|
||||
container = message_manager.get_container(chat.stream_id)
|
||||
thinking_message = None
|
||||
# 找到message,删除
|
||||
for msg in container.messages:
|
||||
if isinstance(msg, Message_Thinking) and msg.message_id == think_id:
|
||||
if isinstance(msg, MessageThinking) and msg.message_id == think_id:
|
||||
thinking_message = msg
|
||||
container.messages.remove(msg)
|
||||
# print(f"\033[1;32m[思考消息删除]\033[0m 已找到思考消息对象,开始删除")
|
||||
break
|
||||
|
||||
# 如果找不到思考消息,直接返回
|
||||
@@ -135,11 +164,10 @@ class ChatBot:
|
||||
|
||||
#记录开始思考的时间,避免从思考到回复的时间太久
|
||||
thinking_start_time = thinking_message.thinking_start_time
|
||||
message_set = MessageSet(event.group_id, global_config.BOT_QQ, think_id) # 发送消息的id和产生发送消息的message_thinking是一致的
|
||||
message_set = MessageSet(chat, think_id)
|
||||
#计算打字时间,1是为了模拟打字,2是避免多条回复乱序
|
||||
accu_typing_time = 0
|
||||
|
||||
# print(f"\033[1;32m[开始回复]\033[0m 开始将回复1载入发送容器")
|
||||
mark_head = False
|
||||
for msg in response:
|
||||
# print(f"\033[1;32m[回复内容]\033[0m {msg}")
|
||||
@@ -148,22 +176,16 @@ class ChatBot:
|
||||
accu_typing_time += typing_time
|
||||
timepoint = tinking_time_point + accu_typing_time
|
||||
|
||||
bot_message = Message_Sending(
|
||||
group_id=event.group_id,
|
||||
user_id=global_config.BOT_QQ,
|
||||
message_segment = Seg(type='text', data=msg)
|
||||
bot_message = MessageSending(
|
||||
message_id=think_id,
|
||||
raw_message=msg,
|
||||
plain_text=msg,
|
||||
processed_plain_text=msg,
|
||||
user_nickname=global_config.BOT_NICKNAME,
|
||||
group_name=message.group_name,
|
||||
time=timepoint, #记录了回复生成的时间
|
||||
thinking_start_time=thinking_start_time, #记录了思考开始的时间
|
||||
reply_message_id=message.message_id
|
||||
chat_stream=chat,
|
||||
message_segment=message_segment,
|
||||
reply=message,
|
||||
is_head=not mark_head,
|
||||
is_emoji=False
|
||||
)
|
||||
await bot_message.initialize()
|
||||
if not mark_head:
|
||||
bot_message.is_head = True
|
||||
mark_head = True
|
||||
message_set.add_message(bot_message)
|
||||
|
||||
@@ -180,30 +202,22 @@ class ChatBot:
|
||||
if emoji_raw != None:
|
||||
emoji_path,discription = emoji_raw
|
||||
|
||||
emoji_cq = cq_code_tool.create_emoji_cq(emoji_path)
|
||||
emoji_cq = image_path_to_base64(emoji_path)
|
||||
|
||||
if random() < 0.5:
|
||||
bot_response_time = tinking_time_point - 1
|
||||
else:
|
||||
bot_response_time = bot_response_time + 1
|
||||
|
||||
bot_message = Message_Sending(
|
||||
group_id=event.group_id,
|
||||
user_id=global_config.BOT_QQ,
|
||||
message_id=0,
|
||||
raw_message=emoji_cq,
|
||||
plain_text=emoji_cq,
|
||||
processed_plain_text=emoji_cq,
|
||||
detailed_plain_text=discription,
|
||||
user_nickname=global_config.BOT_NICKNAME,
|
||||
group_name=message.group_name,
|
||||
time=bot_response_time,
|
||||
is_emoji=True,
|
||||
translate_cq=False,
|
||||
thinking_start_time=thinking_start_time,
|
||||
# reply_message_id=message.message_id
|
||||
message_segment = Seg(type='emoji', data=emoji_cq)
|
||||
bot_message = MessageSending(
|
||||
message_id=think_id,
|
||||
chat_stream=chat,
|
||||
message_segment=message_segment,
|
||||
reply=message,
|
||||
is_head=False,
|
||||
is_emoji=True
|
||||
)
|
||||
await bot_message.initialize()
|
||||
message_manager.add_message(bot_message)
|
||||
emotion = await self.gpt._get_emotion_tags(raw_content)
|
||||
print(f"为 '{response}' 获取到的情感标签为:{emotion}")
|
||||
@@ -219,8 +233,12 @@ class ChatBot:
|
||||
await relationship_manager.update_relationship_value(message.user_id, relationship_value=valuedict[emotion[0]])
|
||||
# 使用情绪管理器更新情绪
|
||||
self.mood_manager.update_mood_from_emotion(emotion[0], global_config.mood_intensity_factor)
|
||||
|
||||
# willing_manager.change_reply_willing_after_sent(event.group_id)
|
||||
|
||||
willing_manager.change_reply_willing_after_sent(
|
||||
platform=messageinfo.platform,
|
||||
user_info=userinfo,
|
||||
group_info=groupinfo
|
||||
)
|
||||
|
||||
# 创建全局ChatBot实例
|
||||
chat_bot = ChatBot()
|
||||
Reference in New Issue
Block a user