feat(onebot): emit bot_status on account session edges
Give downstream bots a notice when an account comes up or goes down, so they can handle their own alerts. Operator notification channels are unchanged.
This commit is contained in:
9 files changed
+307
-10
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import type {
|
||||
OneBotBotStatusEvent,
|
||||
OneBotGroupMessageEvent,
|
||||
OneBotMessageEvent,
|
||||
OneBotMetaEvent,
|
||||
@@ -26,6 +27,13 @@ export function isNoticeEvent(event: SnowLumaEvent): event is OneBotNoticeEvent
|
||||
return event.post_type === 'notice' && typeof event.notice_type === 'string';
|
||||
}
|
||||
|
||||
export function isBotStatusEvent(event: SnowLumaEvent): event is OneBotBotStatusEvent {
|
||||
return isNoticeEvent(event)
|
||||
&& event.notice_type === 'bot_status'
|
||||
&& (event.sub_type === 'online' || event.sub_type === 'offline')
|
||||
&& typeof event.user_id === 'number';
|
||||
}
|
||||
|
||||
export function isRequestEvent(event: SnowLumaEvent): event is OneBotRequestEvent {
|
||||
return event.post_type === 'request' && typeof event.request_type === 'string' && typeof event.flag === 'string';
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ export {
|
||||
} from './errors';
|
||||
export {
|
||||
createEventContext,
|
||||
isBotStatusEvent,
|
||||
isGroupMessageEvent,
|
||||
isMessageEvent,
|
||||
isMetaEvent,
|
||||
|
||||
@@ -50,6 +50,15 @@ export interface OneBotNoticeEvent extends OneBotBaseEvent {
|
||||
notice_type: string;
|
||||
}
|
||||
|
||||
export type BotStatusSubType = 'online' | 'offline';
|
||||
|
||||
/** Bridge session edge. Not KickNT `bot_offline`. */
|
||||
export interface OneBotBotStatusEvent extends OneBotNoticeEvent {
|
||||
notice_type: 'bot_status';
|
||||
sub_type: BotStatusSubType;
|
||||
user_id: number;
|
||||
}
|
||||
|
||||
export interface OneBotRequestEvent extends OneBotBaseEvent {
|
||||
post_type: 'request';
|
||||
request_type: string;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
isBotStatusEvent,
|
||||
isGroupMessageEvent,
|
||||
isMessageEvent,
|
||||
isMetaEvent,
|
||||
@@ -248,6 +249,34 @@ describe('isNoticeEvent', () => {
|
||||
expect(isNoticeEvent(nulled)).toBe(false);
|
||||
});
|
||||
|
||||
it('accepts bot_status notices and rejects incomplete ones', () => {
|
||||
expect(isBotStatusEvent({
|
||||
time: 19,
|
||||
self_id: 10000,
|
||||
post_type: 'notice',
|
||||
notice_type: 'bot_status',
|
||||
sub_type: 'online',
|
||||
user_id: 10000,
|
||||
})).toBe(true);
|
||||
expect(isBotStatusEvent({
|
||||
time: 20,
|
||||
self_id: 10000,
|
||||
post_type: 'notice',
|
||||
notice_type: 'bot_status',
|
||||
sub_type: 'offline',
|
||||
user_id: 10000,
|
||||
})).toBe(true);
|
||||
expect(isBotStatusEvent(NOTICE)).toBe(false);
|
||||
expect(isBotStatusEvent({
|
||||
time: 21,
|
||||
self_id: 10000,
|
||||
post_type: 'notice',
|
||||
notice_type: 'bot_status',
|
||||
sub_type: 'flap',
|
||||
user_id: 10000,
|
||||
})).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects other post types', () => {
|
||||
expect(isNoticeEvent(PRIVATE_MESSAGE)).toBe(false);
|
||||
expect(isNoticeEvent(REQUEST)).toBe(false);
|
||||
|
||||
Reference in new issue
Block a user