Skip to content

Commit 88f1300

Browse files
committed
perf: streamline message dispatching logic in AiocqhttpMessageEvent
1 parent 6f6a5b5 commit 88f1300

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ async def _parse_onebot_json(message_chain: MessageChain):
5858
ret.append(d)
5959
return ret
6060

61+
@classmethod
62+
async def _dispatch_send(
63+
cls,
64+
bot: CQHttp,
65+
event: Event | None,
66+
is_group: bool,
67+
session_id: str,
68+
messages: list[dict],
69+
):
70+
if event:
71+
await bot.send(event=event, message=messages)
72+
elif is_group:
73+
await bot.send_group_msg(group_id=session_id, message=messages)
74+
else:
75+
await bot.send_private_msg(user_id=session_id, message=messages)
76+
6177
@classmethod
6278
async def send_message(
6379
cls,
@@ -69,16 +85,6 @@ async def send_message(
6985
):
7086
"""发送消息"""
7187

72-
async def _send(
73-
event: Event, is_group: bool, session_id: str, messages: list[dict]
74-
):
75-
if event:
76-
await bot.send(event=event, message=messages)
77-
elif is_group:
78-
await bot.send_group_msg(group_id=session_id, message=messages)
79-
else:
80-
await bot.send_private_msg(user_id=session_id, message=messages)
81-
8288
# 转发消息、文件消息不能和普通消息混在一起发送
8389
send_one_by_one = any(
8490
isinstance(seg, (Node, Nodes, File)) for seg in message_chain.chain
@@ -87,7 +93,7 @@ async def _send(
8793
ret = await cls._parse_onebot_json(message_chain)
8894
if not ret:
8995
return
90-
await _send(event, is_group, session_id, ret)
96+
await cls._dispatch_send(bot, event, is_group, session_id, ret)
9197
return
9298
for seg in message_chain.chain:
9399
if isinstance(seg, (Node, Nodes)):
@@ -106,12 +112,12 @@ async def _send(
106112
await bot.call_action("send_private_forward_msg", **payload)
107113
elif isinstance(seg, File):
108114
d = await cls._from_segment_to_dict(seg)
109-
await _send(event, is_group, session_id, [d])
115+
await cls._dispatch_send(bot, event, is_group, session_id, [d])
110116
else:
111117
messages = await cls._parse_onebot_json(MessageChain([seg]))
112118
if not messages:
113119
continue
114-
await _send(event, is_group, session_id, messages)
120+
await cls._dispatch_send(bot, event, is_group, session_id, messages)
115121
await asyncio.sleep(0.5)
116122

117123
async def send(self, message: MessageChain):

0 commit comments

Comments
 (0)