diff --git a/packages/core/src/bridge/actions/profile.ts b/packages/core/src/bridge/actions/profile.ts index 793b058d..93cceb06 100644 --- a/packages/core/src/bridge/actions/profile.ts +++ b/packages/core/src/bridge/actions/profile.ts @@ -32,11 +32,39 @@ export async function setOnlineStatus( extStatus: number = 0, batteryStatus: number = 100, ): Promise { - const request = protoEncode({ - status, - extStatus, - batteryStatus, - }, SetStatusReqSchema); + await dispatchSetStatus(bridge, { status, extStatus, batteryStatus }); +} + +/** + * DIY (custom) online status. napcat fixes status=10 / extStatus=2000 + * — the values QQ associates with "I have a custom status string" — + * and threads the faceId / wording / faceType through the customExt + * sub-message of the same SetStatus wire call. + */ +export async function setDiyOnlineStatus( + bridge: Bridge, + faceId: number, + wording: string, + faceType: number, +): Promise { + await dispatchSetStatus(bridge, { + status: 10, + extStatus: 2000, + batteryStatus: 0, + customExt: { faceId, text: wording, faceType }, + }); +} + +async function dispatchSetStatus( + bridge: Bridge, + value: { + status: number; + extStatus: number; + batteryStatus: number; + customExt?: { faceId: number; text: string; faceType: number }; + }, +): Promise { + const request = protoEncode(value, SetStatusReqSchema); const result = await bridge.sendRawPacket('trpc.qq_new_tech.status_svc.StatusService.SetStatus', request); diff --git a/packages/core/src/bridge/bridge-interface.ts b/packages/core/src/bridge/bridge-interface.ts index 97b237cb..666c0dec 100644 --- a/packages/core/src/bridge/bridge-interface.ts +++ b/packages/core/src/bridge/bridge-interface.ts @@ -120,6 +120,7 @@ export interface BridgeInterface { // ─── Personal profile ─── setOnlineStatus(status: number, extStatus?: number, batteryStatus?: number): Promise; + setDiyOnlineStatus(faceId: number, wording: string, faceType: number): Promise; setProfile(nickname?: string, personalNote?: string): Promise; setSelfLongNick(longNick: string): Promise; setInputStatus(userId: number, eventType: number): Promise; diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index 00973246..b407440a 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -85,6 +85,7 @@ import { } from './actions/friend'; import { setOnlineStatus as setOnlineStatus_, + setDiyOnlineStatus as setDiyOnlineStatus_, setProfile as setProfile_, setSelfLongNick as setSelfLongNick_, setInputStatus as setInputStatus_, @@ -503,6 +504,9 @@ export class Bridge implements BridgeInterface { async setOnlineStatus(status: number, extStatus: number = 0, batteryStatus: number = 100): Promise { return setOnlineStatus_(this, status, extStatus, batteryStatus); } + async setDiyOnlineStatus(faceId: number, wording: string, faceType: number): Promise { + return setDiyOnlineStatus_(this, faceId, wording, faceType); + } async setProfile(nickname?: string, personalNote?: string): Promise { return setProfile_(this, nickname, personalNote); } diff --git a/packages/core/src/bridge/proto/oidb-action.ts b/packages/core/src/bridge/proto/oidb-action.ts index 83635c64..b75e77ff 100644 --- a/packages/core/src/bridge/proto/oidb-action.ts +++ b/packages/core/src/bridge/proto/oidb-action.ts @@ -861,10 +861,25 @@ export const OidbGetPskeyRespSchema = { } satisfies ProtoSchema; +// customExt sub-message of SetStatus. Populated only by +// set_diy_online_status (status=10 + extStatus=2000 + this payload); +// regular set_online_status leaves field 4 unset. +// +// Wire (from napcat packet/transformer/proto/action/action.ts SetStatusCustomExt): +// 1 faceId (uint32) — face icon id, e.g. "Q我吧" sticker +// 2 text (string) — wording shown next to the icon +// 3 field3 (uint32) — face_type / template id; observed values 1..3 +export const SetStatusCustomExtSchema = { + faceId: { field: 1, type: 'uint32' as const }, + text: { field: 2, type: 'string' as const }, + faceType: { field: 3, type: 'uint32' as const }, +} satisfies ProtoSchema; + export const SetStatusReqSchema = { status: { field: 1, type: 'int32' as const }, extStatus: { field: 2, type: 'int32' as const }, batteryStatus: { field: 3, type: 'int32' as const }, + customExt: { field: 4, type: 'message' as const, schema: SetStatusCustomExtSchema }, } satisfies ProtoSchema; export const SetStatusRespSchema = { diff --git a/packages/core/src/onebot/actions/extended.ts b/packages/core/src/onebot/actions/extended.ts index 4de48262..82fd4f99 100644 --- a/packages/core/src/onebot/actions/extended.ts +++ b/packages/core/src/onebot/actions/extended.ts @@ -601,6 +601,24 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { } }); + // DIY status — same wire packet as set_online_status, only the + // customExt sub-message is populated and status/extStatus are forced + // to the QQ-defined "I have a custom status" values (10 / 2000). + // face_id / face_type accept either number or numeric string (napcat + // parity); wording is the human-readable text shown next to the icon. + h.registerAction('set_diy_online_status', async (params) => { + const faceId = asNumber(params.face_id); + const faceType = asNumber(params.face_type) || 1; + const wording = asString(params.wording); + if (!faceId) return failedResponse(RETCODE.BAD_REQUEST, 'face_id is required'); + try { + await ctx.bridge.setDiyOnlineStatus(faceId, wording, faceType); + return okResponse(); + } catch (err) { + return failedResponse(RETCODE.ACTION_FAILED, err instanceof Error ? err.message : String(err)); + } + }); + // Filtered (机器人/被忽略) group join requests. SnowLuma already // implements the underlying oidb 0x10c8_2 fetch via fetchGroupRequests; // these three actions just rename / project the same data for the diff --git a/packages/core/tests/actions/profile.test.ts b/packages/core/tests/actions/profile.test.ts index e1342b1d..126215c3 100644 --- a/packages/core/tests/actions/profile.test.ts +++ b/packages/core/tests/actions/profile.test.ts @@ -35,6 +35,54 @@ describe('actions/profile', () => { expect(serviceCmd).toBe('trpc.qq_new_tech.status_svc.StatusService.SetStatus'); }); + it('setOnlineStatus does NOT include the customExt field 4 (varint 0x22 absent in body)', async () => { + const bridge = mockBridge(); + await profile.setOnlineStatus(bridge as any, 11, 0, 100); + const [, body] = bridge.sendRawPacket.mock.calls[0]!; + // proto field 4 length-delimited tag = (4 << 3) | 2 = 0x22. The + // body should not contain that byte; if encoder ever leaks an + // empty customExt it'd appear here. + expect(Buffer.from(body).includes(0x22)).toBe(false); + }); + + it('setDiyOnlineStatus hardcodes status=10 / extStatus=2000 and packs faceId+wording+faceType into customExt', async () => { + const bridge = mockBridge(); + await profile.setDiyOnlineStatus(bridge as any, 1234, '摸鱼中', 2); + const [serviceCmd, body] = bridge.sendRawPacket.mock.calls[0]!; + expect(serviceCmd).toBe('trpc.qq_new_tech.status_svc.StatusService.SetStatus'); + // Decode the wire bytes back through the same schema to assert + // every field landed in the right place. + const { protoDecode } = await import('../../src/protobuf/decode'); + const { SetStatusReqSchema } = await import('../../src/bridge/proto/oidb-action'); + const decoded = protoDecode(body as Uint8Array, SetStatusReqSchema); + expect(decoded).toMatchObject({ + status: 10, + extStatus: 2000, + batteryStatus: 0, + customExt: { faceId: 1234, text: '摸鱼中', faceType: 2 }, + }); + }); + + it('setDiyOnlineStatus surfaces server errors via the same path as setOnlineStatus', async () => { + const bridge = mockBridge(); + // Build a response that decodes to errCode != 0. + const { protoEncode } = await import('../../src/protobuf/decode'); + const { SetStatusRespSchema } = await import('../../src/bridge/proto/oidb-action'); + const respBuf = Buffer.from(protoEncode({ errCode: 1, errMsg: 'denied' }, SetStatusRespSchema)); + bridge.sendRawPacket.mockResolvedValueOnce({ + success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: respBuf, + } as any); + await expect(profile.setDiyOnlineStatus(bridge as any, 1, 't', 1)).rejects.toThrow(/denied/); + }); + + it('setDiyOnlineStatus rejects when the transport itself fails', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockResolvedValueOnce({ + success: false, gotResponse: false, errorCode: -1, errorMessage: 'pipe closed', responseData: null, + } as any); + await expect(profile.setDiyOnlineStatus(bridge as any, 1, 't', 1)).rejects.toThrow(/pipe closed/); + }); + it('setProfile is a no-op when both arguments are undefined', async () => { const bridge = mockBridge(); await profile.setProfile(bridge as any); diff --git a/packages/core/tests/onebot/extended-actions.test.ts b/packages/core/tests/onebot/extended-actions.test.ts index 74f0b588..a016df31 100644 --- a/packages/core/tests/onebot/extended-actions.test.ts +++ b/packages/core/tests/onebot/extended-actions.test.ts @@ -456,3 +456,44 @@ describe('extended-actions / send_group_ai_record', () => { expect(res).toMatchObject({ status: 'ok', data: { message_id: 0 } }); }); }); + +// ─── Tier 3: set_diy_online_status ─── + +describe('extended-actions / set_diy_online_status', () => { + it('coerces face_id / face_type from string-or-number and forwards to bridge.setDiyOnlineStatus', async () => { + const setDiyOnlineStatus = vi.fn(async () => {}); + const bridge = fakeBridge({ setDiyOnlineStatus: setDiyOnlineStatus as any }); + const res = await makeHandler(fakeCtx(bridge)).handle('set_diy_online_status', { + face_id: '1234', + face_type: '2', + wording: '摸鱼中', + }); + expect(res.status).toBe('ok'); + expect(setDiyOnlineStatus).toHaveBeenCalledWith(1234, '摸鱼中', 2); + }); + + it('defaults face_type to 1 when omitted, wording to empty string', async () => { + const setDiyOnlineStatus = vi.fn(async () => {}); + const bridge = fakeBridge({ setDiyOnlineStatus: setDiyOnlineStatus as any }); + await makeHandler(fakeCtx(bridge)).handle('set_diy_online_status', { face_id: 99 }); + expect(setDiyOnlineStatus).toHaveBeenCalledWith(99, '', 1); + }); + + it('rejects missing face_id', async () => { + const setDiyOnlineStatus = vi.fn(); + const bridge = fakeBridge({ setDiyOnlineStatus: setDiyOnlineStatus as any }); + const res = await makeHandler(fakeCtx(bridge)).handle('set_diy_online_status', { wording: 'x' }); + expect(res).toMatchObject({ status: 'failed', retcode: 1400 }); + expect(setDiyOnlineStatus).not.toHaveBeenCalled(); + }); + + it('surfaces bridge errors as action_failed with the original message', async () => { + const bridge = fakeBridge({ + setDiyOnlineStatus: (async () => { throw new Error('denied'); }) as any, + }); + const res = await makeHandler(fakeCtx(bridge)).handle('set_diy_online_status', { + face_id: 1, wording: 'x', + }); + expect(res).toMatchObject({ status: 'failed', retcode: 100, wording: 'denied' }); + }); +});