fix(send-private): split c2c file segments out to sendC2cFileMessage (was shipping "[空消息]")

User report: `send_private_msg` with a `{type:'file', file_id}` segment
fired `[ElemBuilder] file send via elems[] is group-only; use bridge.
sendC2cFileMessage for c2c` and the message went out as "[空消息]".

Root cause: c2c files live on `RichText.notOnlineFile` (parallel to the
elems array, see proto/proton/message.ts), not inside `elems[]`. The
element-builder explicitly drops c2c `{type:'file'}` elements with a
warning. `sendPrivateMessage` was passing the file straight to
`bridge.sendPrivateMessage(userId, [fileElement])` which then ran
through `buildSendElems`, lost the file at the drop, and shipped a
0-element wire send — rendered as "[空消息]" by every receiver.

Fix: split at the OneBot layer in `sendPrivateMessage`. Pull file
elements out of the batch and dispatch each via
`bridge.sendC2cFileMessage`; route the rest through the regular
elems[] path. Matches NapCat's `dev/NapCatQQ/.../SendMsg.ts:404-415`
where FILE / VIDEO / ARK / PTT each get their own sendMsg call and
never mix with the regular elems[] batch.

- File-only message: only the c2c file API is called (single roundtrip)
- Mixed text + file: two sends, text first then file
- File without file_id: dropped with a warn (was silently shipping
  empty messages)
- UID resolution failure: surfaced as throw instead of empty send

Also rewrote the element-builder fallback warn from "use
sendC2cFileMessage for c2c" (now stale advice) to "BUG: c2c file
reached element-builder — should be split out at the OneBot layer".

Tests: 4 new regression cases under
`tests/onebot/send-private-c2c-file.test.ts`. 646/646 pass.
This commit is contained in:
DrSmoothl committed 2026-05-19 23:36:12 +08:00
1 parent cec38b4c1b
commit f65fe959bb
3 files changed
+214 -11

No files matched your search

+9 -4
View File
@@ -393,13 +393,18 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
case 'file':
// The group `TransElem(24)` shape works for group chats only.
// C2C files live on `RichText.notOnlineFile` instead of in the
// elems array, so `sendPrivateMessage` short-circuits before it
// ever lands here — see `bridge.sendPrivateMessage`.
// C2C files live on `RichText.notOnlineFile` (parallel to elems,
// see proto/proton/message.ts:notOnlineFile) and are routed
// out at the OneBot layer in `sendPrivateMessage` — split off
// from elems[] and dispatched via `bridge.sendC2cFileMessage`.
// If a c2c file segment still reaches us here, the caller
// forgot to route — log loudly and drop, otherwise the message
// ships as "[空消息]" (only-file case) or quietly loses the
// file (mixed case).
if (ctx?.groupId !== undefined) {
result.push(makeGroupFileElem(elem));
} else {
console.warn('[ElemBuilder] file send via elems[] is group-only; use bridge.sendC2cFileMessage for c2c');
console.warn('[ElemBuilder] BUG: c2c {type:"file"} reached element-builder — should be split out at the OneBot layer (see modules/message-actions.ts::sendPrivateMessage)');
}
break;