From ad65aa5b1b89de684d24baa597a02091a91b1e3e Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 16 May 2026 11:55:36 +0800 Subject: [PATCH] fix(send): resolve at-mention display name from the group roster OneBot clients commonly send [CQ:at,qq=12345] without a name field; we were piping `@12345` into the text element so QQ rendered the bare uin instead of the member's card/nickname (napcat resolves via getGroupMember before encoding). Move the fallback into the element builder so it consults the IdentityService roster and only falls back to the uin when the roster has nothing. --- packages/core/src/bridge/actions/forward.ts | 8 ++++-- packages/core/src/bridge/element-builder.ts | 32 ++++++++++++++++++--- packages/core/src/onebot/message-parser.ts | 13 +++++---- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/packages/core/src/bridge/actions/forward.ts b/packages/core/src/bridge/actions/forward.ts index 0181a5fc..6558c4b7 100644 --- a/packages/core/src/bridge/actions/forward.ts +++ b/packages/core/src/bridge/actions/forward.ts @@ -28,12 +28,16 @@ const forwardResCache = new Map(); async function buildForwardPushBody( bridge: Bridge, node: ForwardNodePayload, + groupId?: number, ): Promise> { const fromUin = node.userUin > 0 ? node.userUin : toInt(bridge.identity.uin); if (fromUin <= 0) throw new Error('forward node user uin is invalid'); const nickname = node.nickname.trim() || String(fromUin); - const elems = await buildSendElems(node.elements); + const sendCtx = groupId !== undefined + ? { bridge, groupId } + : { bridge }; + const elems = await buildSendElems(node.elements, sendCtx); const now = Math.floor(Date.now() / 1000); const random = Math.floor(Math.random() * 0x7fffffff) >>> 0; const seq = Math.floor(Math.random() * 9000000) + 1000000; @@ -67,7 +71,7 @@ export async function uploadForwardNodes(bridge: Bridge, nodes: ForwardNodePaylo throw new Error('forward nodes are required'); } - const msgBody = await Promise.all(nodes.map(node => buildForwardPushBody(bridge, node))); + const msgBody = await Promise.all(nodes.map(node => buildForwardPushBody(bridge, node, groupId))); const longMsgResult = protoEncode({ action: [ { diff --git a/packages/core/src/bridge/element-builder.ts b/packages/core/src/bridge/element-builder.ts index b07f7980..74217fd4 100644 --- a/packages/core/src/bridge/element-builder.ts +++ b/packages/core/src/bridge/element-builder.ts @@ -37,19 +37,43 @@ function makeFaceElem(faceId: number): ProtoElem { }; } -function makeMentionElem(element: MessageElement): ProtoElem { +function resolveMentionDisplay(ctx: SendContext | undefined, targetUin: number): string { + if (!ctx || !targetUin) return ''; + if (ctx.groupId !== undefined) { + const member = ctx.bridge.identity.findGroupMember(ctx.groupId, targetUin); + return member?.card?.trim() || member?.nickname?.trim() || ''; + } + const friend = ctx.bridge.identity.findFriend(targetUin); + return friend?.remark?.trim() || friend?.nickname?.trim() || ''; +} + +function makeMentionElem(element: MessageElement, ctx?: SendContext): ProtoElem { const mentionAll = element.uid === 'all' || element.targetUin === 0; + const targetUin = element.targetUin ?? 0; const extra = protoEncode({ type: mentionAll ? 1 : 2, - uin: mentionAll ? 0 : (element.targetUin ?? 0), + uin: mentionAll ? 0 : targetUin, field5: 0, uid: mentionAll ? 'all' : (element.uid ?? ''), }, MentionExtraSendSchema); + // Prefer an explicit display string from the caller; otherwise look the + // target up in the roster so QQ renders `@昵称` instead of `@QQ号`. + // Falls back to the bare uin when the roster doesn't know them yet. + let str = element.text; + if (!str) { + if (mentionAll) { + str = '@全体成员 '; + } else { + const name = resolveMentionDisplay(ctx, targetUin); + str = name ? `@${name} ` : `@${targetUin} `; + } + } + return { text: { - str: element.text || (mentionAll ? '@全体成员 ' : `@${element.targetUin} `), + str, pbReserve: extra, } as any, }; @@ -260,7 +284,7 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte break; case 'at': - result.push(makeMentionElem(elem)); + result.push(makeMentionElem(elem, ctx)); break; case 'reply': diff --git a/packages/core/src/onebot/message-parser.ts b/packages/core/src/onebot/message-parser.ts index 89339ef1..4e085b82 100644 --- a/packages/core/src/onebot/message-parser.ts +++ b/packages/core/src/onebot/message-parser.ts @@ -105,12 +105,13 @@ async function segmentToElement(type: string, data: Record, opt uid = (await options.resolveMentionUid(uin))?.trim() ?? ''; } - return { - type: 'at', - targetUin: uin, - uid: uid || undefined, - text: name ? `@${name} ` : `@${uin} `, - }; + // Leave `text` undefined when the caller didn't supply a display + // name. The element-builder then resolves it from the group roster + // (so QQ renders `@昵称` instead of `@QQ号`). + const element: MessageElement = { type: 'at', targetUin: uin }; + if (uid) element.uid = uid; + if (name) element.text = `@${name} `; + return element; } case 'reply': { const id = parseInt(String(data.id ?? '0'), 10);