diff --git a/packages/napcat-core/apis/group.ts b/packages/napcat-core/apis/group.ts index 51d0733a5..8ec8fa9d5 100644 --- a/packages/napcat-core/apis/group.ts +++ b/packages/napcat-core/apis/group.ts @@ -562,6 +562,10 @@ export class NTQQGroupApi { return this.context.session.getGroupService().quitGroup(groupCode); } + async destroyGroup (groupCode: string) { + return this.context.session.getGroupService().destroyGroup(groupCode); + } + async kickMember (groupCode: string, kickUids: string[], refuseForever: boolean = false, kickReason: string = '') { return this.context.session.getGroupService().kickMember(groupCode, kickUids, refuseForever, kickReason); } diff --git a/packages/napcat-onebot/action/group/SetGroupLeave.ts b/packages/napcat-onebot/action/group/SetGroupLeave.ts index b7f0dc010..d9187657e 100644 --- a/packages/napcat-onebot/action/group/SetGroupLeave.ts +++ b/packages/napcat-onebot/action/group/SetGroupLeave.ts @@ -26,7 +26,13 @@ export default class SetGroupLeave extends OneBotAction override returnExample = GroupActionsExamples.SetGroupLeave.response; async _handle (payload: PayloadType): Promise { - await this.core.apis.GroupApi.quitGroup(payload.group_id.toString()); + const groupCode = payload.group_id.toString(); + // is_dismiss 既可能是布尔值,也可能是字符串 'true'(与 approve 等字段保持一致) + if (payload.is_dismiss === true || payload.is_dismiss === 'true') { + await this.core.apis.GroupApi.destroyGroup(groupCode); + } else { + await this.core.apis.GroupApi.quitGroup(groupCode); + } return null; } } diff --git a/packages/napcat-test/setGroupLeaveDismiss.test.ts b/packages/napcat-test/setGroupLeaveDismiss.test.ts new file mode 100644 index 000000000..eb64fa82a --- /dev/null +++ b/packages/napcat-test/setGroupLeaveDismiss.test.ts @@ -0,0 +1,66 @@ +// StreamBasic must be evaluated before OneBotAction: the two modules import each other, +// and loading OneBotAction first leaves BasicStream extending an uninitialized binding. +import '../napcat-onebot/action/stream/StreamBasic'; +import { describe, expect, test, vi } from 'vitest'; +import SetGroupLeave from '../napcat-onebot/action/group/SetGroupLeave'; + +function createAction () { + const quitGroup = vi.fn().mockResolvedValue(undefined); + const destroyGroup = vi.fn().mockResolvedValue(undefined); + const action = new SetGroupLeave( + undefined as never, + { + apis: { + GroupApi: { quitGroup, destroyGroup }, + }, + } as never + ); + return { action, quitGroup, destroyGroup }; +} + +describe('set_group_leave is_dismiss', () => { + test('leaves the group when is_dismiss is absent', async () => { + const { action, quitGroup, destroyGroup } = createAction(); + + await action._handle({ group_id: '123' }); + + expect(quitGroup).toHaveBeenCalledWith('123'); + expect(destroyGroup).not.toHaveBeenCalled(); + }); + + test('leaves the group when is_dismiss is false', async () => { + const { action, quitGroup, destroyGroup } = createAction(); + + await action._handle({ group_id: '123', is_dismiss: false }); + + expect(quitGroup).toHaveBeenCalledWith('123'); + expect(destroyGroup).not.toHaveBeenCalled(); + }); + + test("leaves the group when is_dismiss is the string 'false'", async () => { + const { action, quitGroup, destroyGroup } = createAction(); + + await action._handle({ group_id: '123', is_dismiss: 'false' }); + + expect(quitGroup).toHaveBeenCalledWith('123'); + expect(destroyGroup).not.toHaveBeenCalled(); + }); + + test('destroys the group when is_dismiss is true', async () => { + const { action, quitGroup, destroyGroup } = createAction(); + + await action._handle({ group_id: '123', is_dismiss: true }); + + expect(destroyGroup).toHaveBeenCalledWith('123'); + expect(quitGroup).not.toHaveBeenCalled(); + }); + + test("destroys the group when is_dismiss is the string 'true'", async () => { + const { action, quitGroup, destroyGroup } = createAction(); + + await action._handle({ group_id: '123', is_dismiss: 'true' }); + + expect(destroyGroup).toHaveBeenCalledWith('123'); + expect(quitGroup).not.toHaveBeenCalled(); + }); +});