|
1 | 1 | import asyncio |
2 | 2 | import re |
3 | 3 | from typing import AsyncGenerator, Dict, List |
4 | | -from aiocqhttp import CQHttp |
| 4 | +from aiocqhttp import CQHttp, Event |
5 | 5 | from astrbot.api.event import AstrMessageEvent, MessageChain |
6 | 6 | from astrbot.api.message_components import ( |
7 | 7 | Image, |
@@ -58,50 +58,85 @@ async def _parse_onebot_json(message_chain: MessageChain): |
58 | 58 | ret.append(d) |
59 | 59 | return ret |
60 | 60 |
|
61 | | - async def send(self, message: MessageChain): |
| 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 | + |
| 77 | + @classmethod |
| 78 | + async def send_message( |
| 79 | + cls, |
| 80 | + bot: CQHttp, |
| 81 | + message_chain: MessageChain, |
| 82 | + event: Event | None = None, |
| 83 | + is_group: bool = False, |
| 84 | + session_id: str = None, |
| 85 | + ): |
| 86 | + """发送消息""" |
| 87 | + |
62 | 88 | # 转发消息、文件消息不能和普通消息混在一起发送 |
63 | 89 | send_one_by_one = any( |
64 | | - isinstance(seg, (Node, Nodes, File)) for seg in message.chain |
| 90 | + isinstance(seg, (Node, Nodes, File)) for seg in message_chain.chain |
65 | 91 | ) |
66 | | - if send_one_by_one: |
67 | | - for seg in message.chain: |
68 | | - if isinstance(seg, (Node, Nodes)): |
69 | | - # 合并转发消息 |
70 | | - |
71 | | - if isinstance(seg, Node): |
72 | | - nodes = Nodes([seg]) |
73 | | - seg = nodes |
74 | | - |
75 | | - payload = await seg.to_dict() |
76 | | - |
77 | | - if self.get_group_id(): |
78 | | - payload["group_id"] = self.get_group_id() |
79 | | - await self.bot.call_action("send_group_forward_msg", **payload) |
80 | | - else: |
81 | | - payload["user_id"] = self.get_sender_id() |
82 | | - await self.bot.call_action( |
83 | | - "send_private_forward_msg", **payload |
84 | | - ) |
85 | | - elif isinstance(seg, File): |
86 | | - d = await AiocqhttpMessageEvent._from_segment_to_dict(seg) |
87 | | - await self.bot.send( |
88 | | - self.message_obj.raw_message, |
89 | | - [d], |
90 | | - ) |
91 | | - else: |
92 | | - await self.bot.send( |
93 | | - self.message_obj.raw_message, |
94 | | - await AiocqhttpMessageEvent._parse_onebot_json( |
95 | | - MessageChain([seg]) |
96 | | - ), |
97 | | - ) |
98 | | - await asyncio.sleep(0.5) |
99 | | - else: |
100 | | - ret = await AiocqhttpMessageEvent._parse_onebot_json(message) |
| 92 | + if not send_one_by_one: |
| 93 | + ret = await cls._parse_onebot_json(message_chain) |
101 | 94 | if not ret: |
102 | 95 | return |
103 | | - await self.bot.send(self.message_obj.raw_message, ret) |
| 96 | + await cls._dispatch_send(bot, event, is_group, session_id, ret) |
| 97 | + return |
| 98 | + for seg in message_chain.chain: |
| 99 | + if isinstance(seg, (Node, Nodes)): |
| 100 | + # 合并转发消息 |
| 101 | + if isinstance(seg, Node): |
| 102 | + nodes = Nodes([seg]) |
| 103 | + seg = nodes |
| 104 | + |
| 105 | + payload = await seg.to_dict() |
| 106 | + |
| 107 | + if is_group: |
| 108 | + payload["group_id"] = session_id |
| 109 | + await bot.call_action("send_group_forward_msg", **payload) |
| 110 | + else: |
| 111 | + payload["user_id"] = session_id |
| 112 | + await bot.call_action("send_private_forward_msg", **payload) |
| 113 | + elif isinstance(seg, File): |
| 114 | + d = await cls._from_segment_to_dict(seg) |
| 115 | + await cls._dispatch_send(bot, event, is_group, session_id, [d]) |
| 116 | + else: |
| 117 | + messages = await cls._parse_onebot_json(MessageChain([seg])) |
| 118 | + if not messages: |
| 119 | + continue |
| 120 | + await cls._dispatch_send(bot, event, is_group, session_id, messages) |
| 121 | + await asyncio.sleep(0.5) |
104 | 122 |
|
| 123 | + async def send(self, message: MessageChain): |
| 124 | + """发送消息""" |
| 125 | + event = self.message_obj.raw_message |
| 126 | + assert isinstance(event, Event), "Event must be an instance of aiocqhttp.Event" |
| 127 | + is_group = False |
| 128 | + if self.get_group_id(): |
| 129 | + is_group = True |
| 130 | + session_id = self.get_group_id() |
| 131 | + else: |
| 132 | + session_id = self.get_sender_id() |
| 133 | + await self.send_message( |
| 134 | + bot=self.bot, |
| 135 | + message_chain=message, |
| 136 | + event=event, |
| 137 | + is_group=is_group, |
| 138 | + session_id=session_id, |
| 139 | + ) |
105 | 140 | await super().send(message) |
106 | 141 |
|
107 | 142 | async def send_streaming( |
|
0 commit comments