fix(onebot): default forward node sender to self when user_id is 0/absent
A fake forward node (send_group_forward_msg / send_private_forward_msg) whose data.user_id was "0" or omitted got rejected with "forward node user_id/uin is required", even though the protocol端 is logged in and the core forward builder (bridge/apis/forward.ts) already defaults a zero sender to the bot's own uin. Upstream frameworks (AstrBot, etc.) don't manage QQ uins and legitimately send user_id "0"; NapCat and LLBot accept it. parseForwardNodes now falls back to ref.selfId, keeping the throw only for the not-logged-in case (selfId 0). Verified on-target (#203).
This commit is contained in:
2 files changed
+31
-1
No files matched your search
@@ -1193,7 +1193,13 @@ async function parseForwardNodes(
|
||||
continue;
|
||||
}
|
||||
|
||||
const userUin = toPositiveInt(nodeData.user_id ?? nodeData.uin);
|
||||
// [#203] A fake forward node may omit user_id or send "0" — upstream
|
||||
// frameworks (AstrBot, etc.) don't manage QQ uins. The protocol端 is logged
|
||||
// in and the core builder already defaults a zero sender to the bot's own
|
||||
// uin (bridge/apis/forward.ts buildForwardPushBody), so match that leniency
|
||||
// here instead of rejecting. The throw stays as a guard for the not-logged-in
|
||||
// case (selfId 0).
|
||||
const userUin = toPositiveInt(nodeData.user_id ?? nodeData.uin) || ref.selfId;
|
||||
if (userUin <= 0) throw new Error('forward node user_id/uin is required');
|
||||
|
||||
const nickname = String(nodeData.nickname ?? nodeData.name ?? userUin);
|
||||
|
||||
@@ -33,6 +33,7 @@ function fakeBridge(overrides: Partial<BridgeInterface> = {}): BridgeInterface {
|
||||
function makeCtx(bridge: BridgeInterface): OneBotInstanceContext {
|
||||
return {
|
||||
uin: '10001',
|
||||
selfId: 10001,
|
||||
bridge,
|
||||
messageStore: { findEvent: () => null } as any,
|
||||
cacheMessageMeta: vi.fn(),
|
||||
@@ -217,4 +218,27 @@ describe('forward — nested {type:"node"} content', () => {
|
||||
const elements = (nodes as any[])[0]!.elements;
|
||||
expect(elements.some((e: any) => e.type === 'text' && e.text === 'a sibling')).toBe(true);
|
||||
});
|
||||
|
||||
it('[#203] node with user_id "0" or missing falls back to the bot self_id', async () => {
|
||||
// Upstream frameworks (AstrBot etc.) send fake forward nodes with
|
||||
// user_id "0" or omit it; the protocol端 knows self_id, so default to it
|
||||
// instead of rejecting (matches NapCat/LLBot + the core builder).
|
||||
const uploadForwardNodes = vi.fn(async (_nodes: any[]) => 'RESID');
|
||||
const sendGroupMessage = vi.fn(async () => ({
|
||||
messageId: 1, sequence: 100, clientSequence: 0, random: 1, timestamp: 1700000000,
|
||||
}));
|
||||
const bridge = fakeBridge({ apis: { message: { sendGroup: sendGroupMessage }, forward: { upload: uploadForwardNodes } } } as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
const messages = [
|
||||
{ type: 'node', data: { user_id: '0', nickname: 'PixivBot', content: [{ type: 'text', data: { text: 'a' } }] } },
|
||||
{ type: 'node', data: { nickname: 'PixivBot', content: [{ type: 'text', data: { text: 'b' } }] } }, // user_id omitted
|
||||
];
|
||||
|
||||
await sendGroupForwardMessage(ctx, 12345, messages as any);
|
||||
const [nodes] = uploadForwardNodes.mock.calls[0]!;
|
||||
expect((nodes as any[])[0].userUin).toBe(10001); // "0" → self_id
|
||||
expect((nodes as any[])[0].nickname).toBe('PixivBot'); // custom nickname preserved
|
||||
expect((nodes as any[])[1].userUin).toBe(10001); // omitted → self_id
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user