pull/70/merge
IMIG 2026-01-17 21:19:18 +00:00 committed by GitHub
commit d541b5c7b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 55 additions and 19 deletions

View File

@ -18,6 +18,7 @@ from src.utils import (
get_member_info,
get_self_info,
get_stranger_info,
get_image_base64,
read_ban_list,
)
@ -222,11 +223,11 @@ class NoticeHandler:
) -> Tuple[Seg | None, UserInfo | None]:
# sourcery skip: merge-comparisons, merge-duplicate-blocks, remove-redundant-if, remove-unnecessary-else, swap-if-else-branches
self_info: dict = await get_self_info(self.server_connection)
if not self_info:
logger.error("自身信息获取失败")
return None, None
self_id = raw_message.get("self_id")
target_id = raw_message.get("target_id")
target_name: str = None
@ -242,17 +243,15 @@ class NoticeHandler:
else:
user_name = "QQ用户"
user_cardname = "QQ用户"
logger.info("无法获取戳一戳对方的用户昵称")
logger.info("无法获取戳一戳对方的用户昵称,使用默认昵称")
# 计算Seg
if self_id == target_id:
display_name = ""
target_name = self_info.get("nickname")
elif self_id == user_id:
# 让ada不发送麦麦戳别人的消息
return None, None
else:
# 老实说这一步判定没啥意义,毕竟私聊是没有其他人之间的戳一戳,但是感觉可以有这个判定来强限制群聊环境
if group_id:
@ -261,31 +260,68 @@ class NoticeHandler:
target_name = fetched_member_info.get("nickname")
else:
target_name = "QQ用户"
logger.info("无法获取被戳一戳方的用户昵称")
logger.info("无法获取被戳一戳方的用户昵称,使用默认昵称")
display_name = user_name
else:
return None, None
first_txt: str = "戳了戳"
second_txt: str = ""
try:
first_txt = raw_info[2].get("txt", "戳了戳")
second_txt = raw_info[4].get("txt", "")
except Exception as e:
logger.warning(f"解析戳一戳消息失败: {str(e)},将使用默认文本")
#提前构造User info
user_info: UserInfo = UserInfo(
platform=global_config.maibot_server.platform_name,
user_id=user_id,
user_nickname=user_name,
user_cardname=user_cardname,
)
seg_data: Seg = Seg(
seg_data: List[Seg] = []
acton_seg: Seg = Seg(type="text", data="戳了戳")
action: str = "戳了戳"
second_txt: str = ""
submit_notice_seg: Seg = None
#catch意外越界errorTODO减少try catch使用尝试通过比较元素数量直接判断napcat的raw info普通戳一戳包含5个元素 自戳与特殊戳一戳包含四个元素(因为没有“戳一戳”、“拍一拍”等文字描述)
try:
#判断是否为特殊戳一戳
if raw_info[2].get("type") == "qq":
"""
#可选不处理任意用户自戳, 自戳tp项参数为1
if raw_info[2].get("tp") == "1":
logger.info("不处理用户自戳")
return None, None
"""
#获取并处理戳一戳动图
try:
image_base64 = await get_image_base64(raw_info[1].get("src"))
acton_seg = Seg(type="emoji", data=image_base64)
except Exception as e:
logger.error(f"会员/活动戳一戳动图处理失败: {str(e)}")
acton_seg = Seg(type="text", data="戳了戳")
second_txt = raw_info[3].get("txt", "")
suffix: str = f"{target_name}{second_txt}"
seg_data.append(Seg(type="text", data=display_name))
seg_data.append(acton_seg)
seg_data.append(Seg(type="text", data=suffix))
submit_notice_seg = Seg(
type="seglist",
data=seg_data,
)
return submit_notice_seg, user_info
else:
#可选不处理使用bot账号进行的自戳自戳tp项参数为1
if raw_info[3].get("tp") == "1":
logger.info("不处理bot自戳")
return None, None
action = raw_info[2].get("txt", "戳了戳")
second_txt = raw_info[4].get("txt", "")
except Exception as e:
logger.warning(f"解析戳一戳消息失败: {str(e)},将使用默认文本")
submit_notice_seg = Seg(
type="text",
data=f"{display_name}{first_txt}{target_name}{second_txt}这是QQ的一个功能用于提及某人但没那么明显",
data=f"{display_name}{action}{target_name}{second_txt}这是QQ的一个功能用于提及某人但没那么明显",
)
return seg_data, user_info
return submit_notice_seg, user_info
async def handle_friend_recall_notify(self, raw_message: dict) -> Tuple[Seg | None, UserInfo | None]:
"""处理好友消息撤回"""