Merge branch 'main-fix' of github.com:KX76/MaiMBot into main-fix

This commit is contained in:
AL76
2025-03-15 12:49:06 +08:00
12 changed files with 411 additions and 285 deletions

View File

@@ -55,112 +55,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. 更新情绪
"""
await message_cq.initialize()
message_json = message_cq.to_dict()
# 哦我嘞个json
@@ -181,7 +84,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()
@@ -203,11 +108,15 @@ 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 = ""
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}")
@@ -216,7 +125,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,
@@ -264,7 +172,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)
@@ -358,79 +269,171 @@ 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 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",
)
if isinstance(event, GroupRecallNoticeEvent):
group_info = GroupInfo(
group_id=event.group_id, group_name=None, platform="qq"
)
else:
group_info = None
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实例
chat_bot = ChatBot()

View File

@@ -249,6 +249,13 @@ class CQCode:
if self.reply_message is None:
return None
if hasattr(self.reply_message, "group_id"):
group_info = GroupInfo(
platform="qq", group_id=self.reply_message.group_id, group_name=""
)
else:
group_info = None
if self.reply_message.sender.user_id:
message_obj = MessageRecvCQ(
user_info=UserInfo(
@@ -256,7 +263,7 @@ class CQCode:
),
message_id=self.reply_message.message_id,
raw_message=str(self.reply_message.message),
group_info=GroupInfo(group_id=self.reply_message.group_id),
group_info=group_info,
)
await message_obj.initialize()

View File

@@ -50,7 +50,6 @@ class Message_Sender:
if not is_recalled:
message_json = message.to_dict()
message_send = MessageSendCQ(data=message_json)
# logger.debug(message_send.message_info,message_send.raw_message)
message_preview = truncate_message(message.processed_plain_text)
if message_send.message_info.group_info and message_send.message_info.group_info.group_id:
try:
@@ -188,16 +187,17 @@ class MessageManager:
else:
if (
message_earliest.is_head
and message_earliest.update_thinking_time() > 30
and message_earliest.update_thinking_time() > 10
and not message_earliest.is_private_message() # 避免在私聊时插入reply
):
message_earliest.set_reply()
await message_sender.send_message(message_earliest)
await message_earliest.process()
await message_sender.send_message(message_earliest)
print(
f"\033[1;34m[调试]\033[0m 消息“{truncate_message(message_earliest.processed_plain_text)}”正在发送中"
)
await self.storage.store_message(message_earliest, message_earliest.chat_stream, None)
@@ -217,11 +217,11 @@ class MessageManager:
and not message_earliest.is_private_message() # 避免在私聊时插入reply
):
msg.set_reply()
await msg.process()
await message_sender.send_message(msg)
# if msg.is_emoji:
# msg.processed_plain_text = "[表情包]"
await msg.process()
await self.storage.store_message(msg, msg.chat_stream, None)
if not container.remove_message(msg):

View File

@@ -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:
"""获取指定聊天流的回复意愿"""
@@ -28,7 +27,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 +35,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 +51,8 @@ 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)
# 检查群组权限(如果是群聊)
if chat_stream.group_info and config:
@@ -63,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