feat(sdk): add onBotStatus for account session notices
Let bots subscribe to account up/down without using the operator notification channels.
This commit is contained in:
3 files changed
+42
No files matched your search
@@ -91,6 +91,10 @@ bot.onRequest('friend', async (_event, ctx) => {
|
||||
await ctx.approve();
|
||||
});
|
||||
|
||||
bot.onBotStatus((event) => {
|
||||
console.log(event.sub_type, event.user_id);
|
||||
});
|
||||
|
||||
await bot.connect();
|
||||
await bot.sendPrivateMessage(10001, text('ready'));
|
||||
```
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
import { Emitter } from '../emitter';
|
||||
import {
|
||||
createEventContext,
|
||||
isBotStatusEvent,
|
||||
isGroupMessageEvent,
|
||||
isMessageEvent,
|
||||
isMetaEvent,
|
||||
@@ -41,6 +42,7 @@ import type {
|
||||
ApiResponse,
|
||||
JsonObject,
|
||||
JsonValue,
|
||||
OneBotBotStatusEvent,
|
||||
OneBotMessageEvent,
|
||||
OneBotMetaEvent,
|
||||
OneBotNoticeEvent,
|
||||
@@ -179,6 +181,10 @@ export class SnowLumaWebSocketClient extends SnowLumaApiClient {
|
||||
return this.when(isNoticeEvent, typeOrHandler);
|
||||
}
|
||||
|
||||
onBotStatus(handler: EventHandler<OneBotBotStatusEvent>): () => void {
|
||||
return this.when(isBotStatusEvent, handler);
|
||||
}
|
||||
|
||||
onRequest(handler: EventHandler<OneBotRequestEvent>): () => void;
|
||||
onRequest(type: string, handler: EventHandler<OneBotRequestEvent>): () => void;
|
||||
onRequest(typeOrHandler: string | EventHandler<OneBotRequestEvent>, handler?: EventHandler<OneBotRequestEvent>): () => void {
|
||||
|
||||
@@ -140,4 +140,36 @@ describe('SnowLumaWebSocketClient', () => {
|
||||
|
||||
await expect(request).rejects.toBeInstanceOf(SnowLumaAbortError);
|
||||
});
|
||||
|
||||
it('dispatches bot_status notices to onBotStatus', async () => {
|
||||
MockWebSocket.instances = [];
|
||||
const client = new SnowLumaWebSocketClient({
|
||||
url: 'ws://127.0.0.1:3001/',
|
||||
webSocket: MockWebSocket,
|
||||
});
|
||||
const handler = vi.fn();
|
||||
client.onBotStatus(handler);
|
||||
|
||||
const connecting = client.connect();
|
||||
const socket = MockWebSocket.instances[0]!;
|
||||
socket.open();
|
||||
await connecting;
|
||||
socket.message(JSON.stringify({
|
||||
time: 1_710_000_000,
|
||||
self_id: 10000,
|
||||
post_type: 'notice',
|
||||
notice_type: 'bot_status',
|
||||
sub_type: 'offline',
|
||||
user_id: 10000,
|
||||
}));
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
expect(handler.mock.calls[0]![0]).toMatchObject({
|
||||
notice_type: 'bot_status',
|
||||
sub_type: 'offline',
|
||||
user_id: 10000,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user