fix: 添加群信息获取的错误处理 (#173)
This commit is contained in:
@@ -24,6 +24,7 @@ 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):
|
||||
self.storage = MessageStorage()
|
||||
@@ -46,8 +47,13 @@ class ChatBot:
|
||||
|
||||
self.bot = bot # 更新 bot 实例
|
||||
|
||||
# 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)
|
||||
try:
|
||||
group_info_api = await bot.get_group_info(group_id=event.group_id)
|
||||
logger.info(f"成功获取群信息: {group_info_api}")
|
||||
group_name = group_info_api["group_name"]
|
||||
except Exception as e:
|
||||
logger.error(f"获取群信息失败: {str(e)}")
|
||||
group_name = None
|
||||
|
||||
# 白名单设定由nontbot侧完成
|
||||
if event.group_id:
|
||||
@@ -60,13 +66,13 @@ class ChatBot:
|
||||
user_id=event.user_id,
|
||||
user_nickname=event.sender.nickname,
|
||||
user_cardname=event.sender.card or None,
|
||||
platform='qq'
|
||||
platform="qq",
|
||||
)
|
||||
|
||||
group_info = GroupInfo(
|
||||
group_id=event.group_id,
|
||||
group_name=None,
|
||||
platform='qq'
|
||||
group_name=group_name, # 使用获取到的群名称或None
|
||||
platform="qq",
|
||||
)
|
||||
|
||||
message_cq = MessageRecvCQ(
|
||||
@@ -75,7 +81,7 @@ class ChatBot:
|
||||
raw_message=str(event.original_message),
|
||||
group_info=group_info,
|
||||
reply_message=event.reply,
|
||||
platform='qq'
|
||||
platform="qq",
|
||||
)
|
||||
message_json = message_cq.to_dict()
|
||||
|
||||
@@ -88,17 +94,20 @@ class ChatBot:
|
||||
|
||||
# 消息过滤,涉及到config有待更新
|
||||
|
||||
chat = await chat_manager.get_or_create_stream(platform=messageinfo.platform, user_info=userinfo, group_info=groupinfo)
|
||||
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(
|
||||
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:
|
||||
if word in message.processed_plain_text:
|
||||
logger.info(
|
||||
f"[{groupinfo.group_name}]{userinfo.user_nickname}:{message.processed_plain_text}")
|
||||
logger.info(f"[群{groupinfo.group_id}]{userinfo.user_nickname}:{message.processed_plain_text}")
|
||||
logger.info(f"[过滤词识别]消息中含有{word},filtered")
|
||||
return
|
||||
|
||||
@@ -106,20 +115,18 @@ class ChatBot:
|
||||
for pattern in global_config.ban_msgs_regex:
|
||||
if re.search(pattern, message.raw_message):
|
||||
logger.info(
|
||||
f"[{message.group_name}]{message.user_nickname}:{message.raw_message}")
|
||||
f"[群{message.message_info.group_info.group_id}]{message.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 = ''
|
||||
topic = ""
|
||||
interested_rate = 0
|
||||
interested_rate = await hippocampus.memory_activate_value(message.processed_plain_text) / 100
|
||||
logger.debug(f"对{message.processed_plain_text}"
|
||||
f"的激活度:{interested_rate}")
|
||||
logger.debug(f"对{message.processed_plain_text}的激活度:{interested_rate}")
|
||||
# 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)
|
||||
@@ -131,12 +138,12 @@ class ChatBot:
|
||||
is_mentioned_bot=is_mentioned,
|
||||
config=global_config,
|
||||
is_emoji=message.is_emoji,
|
||||
interested_rate=interested_rate
|
||||
interested_rate=interested_rate,
|
||||
)
|
||||
current_willing = willing_manager.get_willing(chat_stream=chat)
|
||||
|
||||
logger.info(
|
||||
f"[{current_time}][{chat.group_info.group_name}]{chat.user_info.user_nickname}:"
|
||||
f"[{current_time}][群{chat.group_info.group_id}]{chat.user_info.user_nickname}:"
|
||||
f"{message.processed_plain_text}[回复意愿:{current_willing:.2f}][概率:{reply_probability * 100:.1f}%]"
|
||||
)
|
||||
|
||||
@@ -144,17 +151,12 @@ class ChatBot:
|
||||
|
||||
if random() < reply_probability:
|
||||
bot_user_info = UserInfo(
|
||||
user_id=global_config.BOT_QQ,
|
||||
user_nickname=global_config.BOT_NICKNAME,
|
||||
platform=messageinfo.platform
|
||||
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)
|
||||
think_id = "mt" + str(tinking_time_point)
|
||||
thinking_message = MessageThinking(
|
||||
message_id=think_id,
|
||||
chat_stream=chat,
|
||||
bot_user_info=bot_user_info,
|
||||
reply=message
|
||||
message_id=think_id, chat_stream=chat, bot_user_info=bot_user_info, reply=message
|
||||
)
|
||||
|
||||
message_manager.add_message(thinking_message)
|
||||
@@ -196,7 +198,7 @@ class ChatBot:
|
||||
print(f"typing_time: {typing_time}")
|
||||
accu_typing_time += typing_time
|
||||
timepoint = tinking_time_point + accu_typing_time
|
||||
message_segment = Seg(type='text', data=msg)
|
||||
message_segment = Seg(type="text", data=msg)
|
||||
print(f"message_segment: {message_segment}")
|
||||
bot_message = MessageSending(
|
||||
message_id=think_id,
|
||||
@@ -205,7 +207,7 @@ class ChatBot:
|
||||
message_segment=message_segment,
|
||||
reply=message,
|
||||
is_head=not mark_head,
|
||||
is_emoji=False
|
||||
is_emoji=False,
|
||||
)
|
||||
print(f"bot_message: {bot_message}")
|
||||
if not mark_head:
|
||||
@@ -234,7 +236,7 @@ class ChatBot:
|
||||
else:
|
||||
bot_response_time = bot_response_time + 1
|
||||
|
||||
message_segment = Seg(type='emoji', data=emoji_cq)
|
||||
message_segment = Seg(type="emoji", data=emoji_cq)
|
||||
bot_message = MessageSending(
|
||||
message_id=think_id,
|
||||
chat_stream=chat,
|
||||
@@ -242,22 +244,24 @@ class ChatBot:
|
||||
message_segment=message_segment,
|
||||
reply=message,
|
||||
is_head=False,
|
||||
is_emoji=True
|
||||
is_emoji=True,
|
||||
)
|
||||
message_manager.add_message(bot_message)
|
||||
|
||||
emotion = await self.gpt._get_emotion_tags(raw_content)
|
||||
logger.debug(f"为 '{response}' 获取到的情感标签为:{emotion}")
|
||||
valuedict = {
|
||||
'happy': 0.5,
|
||||
'angry': -1,
|
||||
'sad': -0.5,
|
||||
'surprised': 0.2,
|
||||
'disgusted': -1.5,
|
||||
'fearful': -0.7,
|
||||
'neutral': 0.1
|
||||
"happy": 0.5,
|
||||
"angry": -1,
|
||||
"sad": -0.5,
|
||||
"surprised": 0.2,
|
||||
"disgusted": -1.5,
|
||||
"fearful": -0.7,
|
||||
"neutral": 0.1,
|
||||
}
|
||||
await relationship_manager.update_relationship_value(chat_stream=chat, relationship_value=valuedict[emotion[0]])
|
||||
await relationship_manager.update_relationship_value(
|
||||
chat_stream=chat, relationship_value=valuedict[emotion[0]]
|
||||
)
|
||||
# 使用情绪管理器更新情绪
|
||||
self.mood_manager.update_mood_from_emotion(emotion[0], global_config.mood_intensity_factor)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user