mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-20 02:55:08 +08:00
fix: updates the aiocqhttp platform adapter and message event handler to include self_id routing parameters across multiple API calls (#8779)
Updates the aiocqhttp platform adapter and message event handler to include self_id routing parameters across multiple API calls, including message sending, forwarding, and fetching group/user details. It also adds handling to ignore mface message types. The review feedback suggests refactoring the duplicate self_id extraction logic in the message forwarding code to reduce redundancy and improve maintainability.
This commit is contained in:
@@ -99,11 +99,22 @@ class AiocqhttpMessageEvent(AstrMessageEvent):
|
||||
session_id_int = (
|
||||
int(session_id) if session_id and session_id.isdigit() else None
|
||||
)
|
||||
routing_params = {}
|
||||
if isinstance(event, Event) and event.get("self_id"):
|
||||
routing_params["self_id"] = event["self_id"]
|
||||
|
||||
if is_group and isinstance(session_id_int, int):
|
||||
await bot.send_group_msg(group_id=session_id_int, message=messages)
|
||||
await bot.send_group_msg(
|
||||
group_id=session_id_int,
|
||||
message=messages,
|
||||
**routing_params,
|
||||
)
|
||||
elif not is_group and isinstance(session_id_int, int):
|
||||
await bot.send_private_msg(user_id=session_id_int, message=messages)
|
||||
await bot.send_private_msg(
|
||||
user_id=session_id_int,
|
||||
message=messages,
|
||||
**routing_params,
|
||||
)
|
||||
elif isinstance(event, Event): # 最后兜底
|
||||
await bot.send(event=event, message=messages)
|
||||
else:
|
||||
@@ -151,9 +162,13 @@ class AiocqhttpMessageEvent(AstrMessageEvent):
|
||||
|
||||
if is_group:
|
||||
payload["group_id"] = session_id
|
||||
if isinstance(event, Event) and event.get("self_id"):
|
||||
payload["self_id"] = event["self_id"]
|
||||
await bot.call_action("send_group_forward_msg", **payload)
|
||||
else:
|
||||
payload["user_id"] = session_id
|
||||
if isinstance(event, Event) and event.get("self_id"):
|
||||
payload["self_id"] = event["self_id"]
|
||||
await bot.call_action("send_private_forward_msg", **payload)
|
||||
elif isinstance(seg, File):
|
||||
d = await cls._from_segment_to_dict(seg)
|
||||
@@ -225,14 +240,20 @@ class AiocqhttpMessageEvent(AstrMessageEvent):
|
||||
else:
|
||||
return None
|
||||
|
||||
routing_params = {}
|
||||
if getattr(self.message_obj, "self_id", None):
|
||||
routing_params["self_id"] = self.message_obj.self_id
|
||||
|
||||
info: dict = await self.bot.call_action(
|
||||
"get_group_info",
|
||||
group_id=group_id,
|
||||
**routing_params,
|
||||
)
|
||||
|
||||
members: list[dict] = await self.bot.call_action(
|
||||
"get_group_member_list",
|
||||
group_id=group_id,
|
||||
**routing_params,
|
||||
)
|
||||
|
||||
owner_id = None
|
||||
|
||||
@@ -239,6 +239,7 @@ class AiocqhttpAdapter(Platform):
|
||||
raise ValueError(err)
|
||||
|
||||
# 按消息段类型类型适配
|
||||
routing_params = {"self_id": event.self_id} if event.self_id else {}
|
||||
for t, m_group in itertools.groupby(event.message, key=lambda x: x["type"]):
|
||||
a = None
|
||||
if t == "text":
|
||||
@@ -272,11 +273,13 @@ class AiocqhttpAdapter(Platform):
|
||||
action="get_group_file_url",
|
||||
file_id=event.message[0]["data"]["file_id"],
|
||||
group_id=event.group_id,
|
||||
**routing_params,
|
||||
)
|
||||
elif abm.type == MessageType.FRIEND_MESSAGE:
|
||||
ret = await self.bot.call_action(
|
||||
action="get_private_file_url",
|
||||
file_id=event.message[0]["data"]["file_id"],
|
||||
**routing_params,
|
||||
)
|
||||
if ret and "url" in ret:
|
||||
file_url = ret["url"] # https
|
||||
@@ -307,6 +310,7 @@ class AiocqhttpAdapter(Platform):
|
||||
reply_event_data = await self.bot.call_action(
|
||||
action="get_msg",
|
||||
message_id=int(m["data"]["id"]),
|
||||
**routing_params,
|
||||
)
|
||||
# 添加必要的 post_type 字段,防止 Event.from_payload 报错
|
||||
reply_event_data["post_type"] = "message"
|
||||
@@ -353,6 +357,7 @@ class AiocqhttpAdapter(Platform):
|
||||
group_id=event.group_id,
|
||||
user_id=int(m["data"]["qq"]),
|
||||
no_cache=False,
|
||||
**routing_params,
|
||||
)
|
||||
if at_info:
|
||||
nickname = at_info.get("card", "")
|
||||
@@ -361,6 +366,7 @@ class AiocqhttpAdapter(Platform):
|
||||
action="get_stranger_info",
|
||||
user_id=int(m["data"]["qq"]),
|
||||
no_cache=False,
|
||||
**routing_params,
|
||||
)
|
||||
nickname = at_info.get("nick", "") or at_info.get(
|
||||
"nickname",
|
||||
@@ -389,6 +395,8 @@ class AiocqhttpAdapter(Platform):
|
||||
logger.error(f"获取 @ 用户信息失败: {e},此消息段将被忽略。")
|
||||
|
||||
message_str += "".join(at_parts)
|
||||
elif t == "mface":
|
||||
continue
|
||||
elif t == "markdown":
|
||||
for m in m_group:
|
||||
text = m["data"].get("markdown") or m["data"].get("content", "")
|
||||
|
||||
Reference in New Issue
Block a user