From 4593ba334056b152ce2e8a569816fe858b32f4c7 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 22 Jun 2026 20:53:10 +0800 Subject: [PATCH] fix(protocol): strip reply auto-mention on receive; align send to uin=0 (#127) A QQ NT reply carries the replied sender as a structural auto-mention (MentionExtra.type=2, uin=0) right after srcMsg, plus a blank separator text. SnowLuma reported both as a real [CQ:at] + empty/space segment, so downstreams saw a phantom @ (#127). A real user @ carries a non-zero MentionExtra.uin, so it is preserved. - receive (rich-body-decoder): after a reply, drop a type=2/uin=0 mention and the blank text that follows it; keep non-zero-uin mentions. - send (element-builder, #129 follow-up): the reply auto-mention added for reply+@ now uses uin=0 (real uid stays in `uid`) to match QQ's native wire shape, so the bot's own replies are stripped the same way by receivers and don't surface a phantom @ either. Validated on-target (Docker, group): reply w/o @ -> [reply, text] (no phantom @/empty); reply w/ @ -> [reply, at(real), text]; bot reply+@ still delivers the @ (uin=0). --- packages/protocol/src/element-builder.ts | 6 +++++- .../src/msg-push/rich-body-decoder.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/protocol/src/element-builder.ts b/packages/protocol/src/element-builder.ts index 43be2636..a8f5265d 100644 --- a/packages/protocol/src/element-builder.ts +++ b/packages/protocol/src/element-builder.ts @@ -115,7 +115,11 @@ async function makeReplyMentionElem(ctx: SendContext, uin: number): Promise ''); if (!uid) return null; const name = member?.card?.trim() || member?.nickname?.trim() || String(uin); - const extra = protobuf_encode({ type: 2, uin, field5: 0, uid }); + // uin=0 matches QQ NT's native reply auto-mention (the real uid lives in + // `uid`). It does not itself notify — the user's own @ does — and the receive + // side strips a type=2/uin=0 mention as structural (#127), so the bot's own + // replies don't surface a spurious @ either. + const extra = protobuf_encode({ type: 2, uin: 0, field5: 0, uid }); return { text: { str: `@${name} `, pbReserve: extra } }; } diff --git a/packages/protocol/src/msg-push/rich-body-decoder.ts b/packages/protocol/src/msg-push/rich-body-decoder.ts index d0bc4b3d..cf5a5c3b 100644 --- a/packages/protocol/src/msg-push/rich-body-decoder.ts +++ b/packages/protocol/src/msg-push/rich-body-decoder.ts @@ -33,6 +33,13 @@ export function decodeRichBody(body: PushMsgBody | undefined, isGroup: boolean): function convertElements(elems: ElemDecoded[]): MessageElement[] { const result: MessageElement[] = []; let skipNext = false; + // [#127] A QQ NT reply carries the replied sender as a structural auto-mention + // (MentionExtra.type=2, uin=0) right after srcMsg, followed by a blank + // separator text. Both are part of the reply wire shape, not user content — + // drop them so they aren't reported as a spurious @ + empty segment. A real + // user @ carries a non-zero MentionExtra.uin, so it's preserved. + let sawReply = false; + let dropNextBlankText = false; for (const elem of elems) { if (skipNext) { skipNext = false; continue; } @@ -55,6 +62,7 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] { // back empty. const replySeq = elem.srcMsg.origSeqs?.[0] ?? 0; if (replySeq > 0) result.push({ type: 'reply', replySeq }); + sawReply = true; } // Text (with possible @ detection) @@ -67,6 +75,17 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] { const hasAttr6 = t.attr6Buf && t.attr6Buf.length > 11; const hasMention = mention && (mention.type === 1 || mention.type === 2); + // [#127] drop the reply's structural auto-mention (type=2, uin=0) and the + // blank separator text right after it; keep real @s (non-zero uin). + if (sawReply && mention && mention.type === 2 && (mention.uin ?? 0) === 0) { + dropNextBlankText = true; + continue; + } + if (dropNextBlankText) { + dropNextBlankText = false; + if (!hasMention && (t.str ?? '').trim() === '') continue; + } + if (hasAttr6 || hasMention) { const me: MessageElement = { type: 'at', text: t.str ?? '' }; if (hasAttr6) {