增加对群名称变更通知的处理,支持Base64解码群公告标题和内容,优化音乐消息处理逻辑以支持多种数据格式
parent
0beb3dfdfa
commit
250be48ea8
|
|
@ -41,6 +41,7 @@ class NoticeType: # 通知事件
|
||||||
|
|
||||||
class Notify:
|
class Notify:
|
||||||
poke = "poke" # 戳一戳
|
poke = "poke" # 戳一戳
|
||||||
|
group_name = "group_name" # 群名称变更
|
||||||
|
|
||||||
class GroupBan:
|
class GroupBan:
|
||||||
ban = "ban" # 禁言
|
ban = "ban" # 禁言
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ from src.utils import (
|
||||||
get_self_info,
|
get_self_info,
|
||||||
get_message_detail,
|
get_message_detail,
|
||||||
)
|
)
|
||||||
|
import base64
|
||||||
from .qq_emoji_list import qq_face
|
from .qq_emoji_list import qq_face
|
||||||
from .message_sending import message_send_instance
|
from .message_sending import message_send_instance
|
||||||
from . import RealMessageType, MessageType, ACCEPT_FORMAT
|
from . import RealMessageType, MessageType, ACCEPT_FORMAT
|
||||||
|
|
@ -509,8 +510,22 @@ class MessageHandler:
|
||||||
if app == "com.tencent.mannounce":
|
if app == "com.tencent.mannounce":
|
||||||
meta = parsed_json.get("meta", {})
|
meta = parsed_json.get("meta", {})
|
||||||
mannounce = meta.get("mannounce", {})
|
mannounce = meta.get("mannounce", {})
|
||||||
title = mannounce.get("title", "")
|
title_encoded = mannounce.get("title", "")
|
||||||
text = mannounce.get("text", "")
|
text_encoded = mannounce.get("text", "")
|
||||||
|
|
||||||
|
# 解码Base64编码的标题和内容
|
||||||
|
title = ""
|
||||||
|
text = ""
|
||||||
|
try:
|
||||||
|
if title_encoded:
|
||||||
|
title = base64.b64decode(title_encoded).decode("utf-8")
|
||||||
|
if text_encoded:
|
||||||
|
text = base64.b64decode(text_encoded).decode("utf-8")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"群公告Base64解码失败: {e}")
|
||||||
|
# 降级使用原始值
|
||||||
|
title = title_encoded
|
||||||
|
text = text_encoded
|
||||||
|
|
||||||
# 构建群公告文本
|
# 构建群公告文本
|
||||||
announce_text = "[群公告]"
|
announce_text = "[群公告]"
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,12 @@ class NoticeHandler:
|
||||||
handled_message, user_info = await self.handle_poke_notify(raw_message, group_id, user_id)
|
handled_message, user_info = await self.handle_poke_notify(raw_message, group_id, user_id)
|
||||||
else:
|
else:
|
||||||
logger.warning("戳一戳消息被禁用,取消戳一戳处理")
|
logger.warning("戳一戳消息被禁用,取消戳一戳处理")
|
||||||
|
case NoticeType.Notify.group_name:
|
||||||
|
if not await message_handler.check_allow_to_chat(user_id, group_id, True, False):
|
||||||
|
return None
|
||||||
|
logger.info("处理群名称变更")
|
||||||
|
handled_message, user_info = await self.handle_group_name_notify(raw_message, group_id, user_id)
|
||||||
|
system_notice = True
|
||||||
case _:
|
case _:
|
||||||
logger.warning(f"不支持的notify类型: {notice_type}.{sub_type}")
|
logger.warning(f"不支持的notify类型: {notice_type}.{sub_type}")
|
||||||
case NoticeType.group_ban:
|
case NoticeType.group_ban:
|
||||||
|
|
@ -1111,5 +1117,47 @@ class NoticeHandler:
|
||||||
|
|
||||||
return notify_seg, user_info
|
return notify_seg, user_info
|
||||||
|
|
||||||
|
async def handle_group_name_notify(
|
||||||
|
self, raw_message: dict, group_id: int, user_id: int
|
||||||
|
) -> Tuple[Seg | None, UserInfo | None]:
|
||||||
|
"""
|
||||||
|
处理群名称变更通知
|
||||||
|
"""
|
||||||
|
new_name = raw_message.get("name_new")
|
||||||
|
|
||||||
|
if not new_name:
|
||||||
|
logger.warning("群名称变更通知缺少新名称")
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
# 获取操作者信息
|
||||||
|
user_info_dict: dict = await get_member_info(self.server_connection, group_id, user_id)
|
||||||
|
if user_info_dict:
|
||||||
|
user_name = user_info_dict.get("nickname")
|
||||||
|
user_cardname = user_info_dict.get("card")
|
||||||
|
else:
|
||||||
|
logger.warning("无法获取修改群名称的用户信息")
|
||||||
|
user_name = "QQ用户"
|
||||||
|
user_cardname = None
|
||||||
|
|
||||||
|
user_info = UserInfo(
|
||||||
|
platform=global_config.maibot_server.platform_name,
|
||||||
|
user_id=user_id,
|
||||||
|
user_nickname=user_name,
|
||||||
|
user_cardname=user_cardname,
|
||||||
|
)
|
||||||
|
|
||||||
|
action_text = f"修改群名称为: {new_name}"
|
||||||
|
|
||||||
|
notify_seg = Seg(
|
||||||
|
type="notify",
|
||||||
|
data={
|
||||||
|
"sub_type": "group_name",
|
||||||
|
"action": action_text,
|
||||||
|
"new_name": new_name,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return notify_seg, user_info
|
||||||
|
|
||||||
|
|
||||||
notice_handler = NoticeHandler()
|
notice_handler = NoticeHandler()
|
||||||
|
|
|
||||||
|
|
@ -54,8 +54,8 @@ class SendMessageHandleClass:
|
||||||
voice_url = seg.data
|
voice_url = seg.data
|
||||||
new_payload = cls.build_payload(payload, cls.handle_voiceurl_message(voice_url), False)
|
new_payload = cls.build_payload(payload, cls.handle_voiceurl_message(voice_url), False)
|
||||||
elif seg.type == "music":
|
elif seg.type == "music":
|
||||||
song_id = seg.data
|
music_data = seg.data
|
||||||
new_payload = cls.build_payload(payload, cls.handle_music_message(song_id), False)
|
new_payload = cls.build_payload(payload, cls.handle_music_message(music_data), False)
|
||||||
elif seg.type == "videourl":
|
elif seg.type == "videourl":
|
||||||
video_url = seg.data
|
video_url = seg.data
|
||||||
new_payload = cls.build_payload(payload, cls.handle_videourl_message(video_url), False)
|
new_payload = cls.build_payload(payload, cls.handle_videourl_message(video_url), False)
|
||||||
|
|
@ -170,13 +170,43 @@ class SendMessageHandleClass:
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def handle_music_message(song_id: str) -> dict:
|
def handle_music_message(music_data) -> dict:
|
||||||
"""处理音乐消息"""
|
"""
|
||||||
|
处理音乐消息
|
||||||
|
music_data 可以是:
|
||||||
|
1. 字符串:默认为网易云音乐ID
|
||||||
|
2. 字典:{"type": "163"/"qq", "id": "歌曲ID"}
|
||||||
|
"""
|
||||||
|
# 兼容旧格式:直接传入歌曲ID字符串
|
||||||
|
if isinstance(music_data, str):
|
||||||
return {
|
return {
|
||||||
"type": "music",
|
"type": "music",
|
||||||
"data": {"type": "163", "id": song_id},
|
"data": {"type": "163", "id": music_data},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 新格式:字典包含平台和ID
|
||||||
|
if isinstance(music_data, dict):
|
||||||
|
platform = music_data.get("type", "163") # 默认网易云
|
||||||
|
song_id = music_data.get("id", "")
|
||||||
|
|
||||||
|
# 验证平台类型
|
||||||
|
if platform not in ["163", "qq"]:
|
||||||
|
logger.warning(f"不支持的音乐平台: {platform},使用默认平台163")
|
||||||
|
platform = "163"
|
||||||
|
|
||||||
|
# 确保ID是字符串
|
||||||
|
if not isinstance(song_id, str):
|
||||||
|
song_id = str(song_id)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "music",
|
||||||
|
"data": {"type": platform, "id": song_id},
|
||||||
|
}
|
||||||
|
|
||||||
|
# 其他情况返回空
|
||||||
|
logger.error(f"不支持的音乐数据格式: {type(music_data)}")
|
||||||
|
return {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def handle_videourl_message(video_url: str) -> dict:
|
def handle_videourl_message(video_url: str) -> dict:
|
||||||
"""处理视频链接消息"""
|
"""处理视频链接消息"""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue