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.
This commit is contained in:
DrSmoothl committed 2026-05-16 11:55:36 +08:00
1 parent a1a49944df
commit ad65aa5b1b
3 files changed
+41 -12

No files matched your search

+6 -2
View File
@@ -28,12 +28,16 @@ const forwardResCache = new Map<string, ForwardNodePayload[]>();
async function buildForwardPushBody(
bridge: Bridge,
node: ForwardNodePayload,
groupId?: number,
): Promise<Record<string, unknown>> {
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: [
{
+28 -4
View File
@@ -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':
+7 -6
View File
@@ -105,12 +105,13 @@ async function segmentToElement(type: string, data: Record<string, unknown>, 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);