diff --git a/nonebot/adapters/mirai/message.py b/nonebot/adapters/mirai/message.py index 2c93cbee..645c919f 100644 --- a/nonebot/adapters/mirai/message.py +++ b/nonebot/adapters/mirai/message.py @@ -289,6 +289,19 @@ class MessageChain(BaseMessage): f'Type {type(message).__name__} is not supported in mirai adapter.' ) + @overrides(BaseMessage) + def reduce(self): + """ + :说明: + + 忽略为空的消息段, 合并相邻的纯文本消息段 + """ + for index, segment in enumerate(self): + segment: MessageSegment + if segment.is_text() and not str(segment).strip(): + self.pop(index) + super().reduce() + @overrides(BaseMessage) def _construct( self, message: Union[List[Dict[str, Any]], Iterable[MessageSegment]] @@ -310,6 +323,15 @@ class MessageChain(BaseMessage): ] def extract_first(self, *type: MessageType) -> Optional[MessageSegment]: + """ + :说明: + + 弹出该消息链的第一个消息 + + :参数: + + * `*type: MessageType`: 指定的消息类型, 当指定后如类型不匹配不弹出 + """ if not len(self): return None first: MessageSegment = self[0] diff --git a/nonebot/adapters/mirai/utils.py b/nonebot/adapters/mirai/utils.py index 385bd3c6..74ad9f6e 100644 --- a/nonebot/adapters/mirai/utils.py +++ b/nonebot/adapters/mirai/utils.py @@ -168,10 +168,11 @@ def process_reply(bot: "Bot", event: GroupMessage) -> GroupMessage: async def process_event(bot: "Bot", event: Event) -> None: if isinstance(event, MessageEvent): + event.message_chain.reduce() Log.debug(event.message_chain) event = process_source(bot, event) if isinstance(event, GroupMessage): event = process_nick(bot, event) - event = process_reply(bot, event) event = process_at(bot, event) + event = process_reply(bot, event) await handle_event(bot, event) \ No newline at end of file