-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(aiocqhttp): 修复 At 组件与后续内容的间距问题 #8561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NayukiChiba
wants to merge
3
commits into
AstrBotDevs:master
Choose a base branch
from
NayukiChiba:fix/8519-aiocqhttp-newline
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+46
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -68,22 +68,61 @@ async def _from_segment_to_dict(segment: BaseMessageComponent) -> dict: | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||||||||||||||||
| async def _parse_onebot_json(message_chain: MessageChain): | ||||||||||||||||||||||||||||||||||||||||||||||||
| """解析成 OneBot json 格式""" | ||||||||||||||||||||||||||||||||||||||||||||||||
| """解析成 OneBot json 格式 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| 将消息链转换为 OneBot 协议的消息段数组. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 特别处理 At 组件与后续内容的间距: | ||||||||||||||||||||||||||||||||||||||||||||||||
| - At + Plain 文本:确保一个空格分隔,避免粘连或双空格 | ||||||||||||||||||||||||||||||||||||||||||||||||
| - At + 非 Plain(图片/文件等):插入空格文本段分隔 | ||||||||||||||||||||||||||||||||||||||||||||||||
| - At 在链末尾:不添加多余空格 | ||||||||||||||||||||||||||||||||||||||||||||||||
| - 纯空白 Plain(如仅含换行/空格):跳过,不重置 At 标志位 | ||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||
| ret = [] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # 标记前一个段是否为 At 组件,用于决定是否需要在当前段前插入空格 | ||||||||||||||||||||||||||||||||||||||||||||||||
| prev_is_at = False | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| for segment in message_chain.chain: | ||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(segment, At): | ||||||||||||||||||||||||||||||||||||||||||||||||
| # At 组件后插入一个空格,避免与后续文本粘连 | ||||||||||||||||||||||||||||||||||||||||||||||||
| # At 组件:记录到结果,并设置标志位 | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 空格由后续段决定如何插入(避免无条件插入导致末尾多余空格) | ||||||||||||||||||||||||||||||||||||||||||||||||
| d = await AiocqhttpMessageEvent._from_segment_to_dict(segment) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ret.append(d) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ret.append({"type": "text", "data": {"text": " "}}) | ||||||||||||||||||||||||||||||||||||||||||||||||
| prev_is_at = True | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| elif isinstance(segment, Plain): | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 跳过纯空白文本(如单独的换行符、空格等) | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 注意:不重置 prev_is_at,避免 [At, Plain("\n"), Plain("你好")] | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 这种场景下空白 Plain 阻断空格插入 | ||||||||||||||||||||||||||||||||||||||||||||||||
| if not segment.text.strip(): | ||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if prev_is_at: | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 前一个是 At:去除 Plain 的前导空白后,统一在前面加一个空格 | ||||||||||||||||||||||||||||||||||||||||||||||||
| # .lstrip() 的作用: | ||||||||||||||||||||||||||||||||||||||||||||||||
| # result_decorate 阶段可能已在文本前加了空格或换行, | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 直接拼接会导致 "@用户 \n你好" 这样的双空白 | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 统一用 " " 替换所有前导空白,确保 @ 与正文之间仅有一个空格 | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 注意:不修改 segment.text,避免污染原始 MessageChain(影响 hook 等消费者) | ||||||||||||||||||||||||||||||||||||||||||||||||
| text = " " + segment.text.lstrip() | ||||||||||||||||||||||||||||||||||||||||||||||||
| prev_is_at = False | ||||||||||||||||||||||||||||||||||||||||||||||||
| ret.append({"type": "text", "data": {"text": text}}) | ||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| d = await AiocqhttpMessageEvent._from_segment_to_dict(segment) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ret.append(d) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+100
to
113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 直接修改 建议在调用
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 非 At、非 Plain 的组件(Image、Record、Video、File 等) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if prev_is_at: | ||||||||||||||||||||||||||||||||||||||||||||||||
| # At 后紧跟媒体组件,插入一个空格文本段防止粘连 | ||||||||||||||||||||||||||||||||||||||||||||||||
| # 例如:[At] [Image] → [At] [空格] [Image] | ||||||||||||||||||||||||||||||||||||||||||||||||
| ret.append({"type": "text", "data": {"text": " "}}) | ||||||||||||||||||||||||||||||||||||||||||||||||
| prev_is_at = False | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| d = await AiocqhttpMessageEvent._from_segment_to_dict(segment) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ret.append(d) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return ret | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.