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:
DrSmoothl committed 2026-09-16 17:04:06 +08:00
1 parent f2d58d2f79
commit ad36d47cf0
9 files changed
+307 -10

No files matched your search

+27 -4
View File
@@ -197,6 +197,12 @@ export class OneBotInstance {
return this.networkReady;
}
/** Bridge session edge → OneBot notice. Awaited so offline can leave
* adapters before this generation is disposed. */
emitBotStatus(subType: 'online' | 'offline'): Promise<void> {
return this.dispatchEvent(makeBotStatusEvent(parseInt(this.uin, 10) || 0, subType));
}
/** Begin observing request-list-only group invitations after adapters exist. */
startGroupRequestPolling(): void {
if (this.disposeRequested) return;
@@ -355,11 +361,11 @@ export class OneBotInstance {
return this.pids.size === 0;
}
private dispatchEvent(
private async dispatchEvent(
event: JsonObject,
source: 'bridge' | 'send' = 'bridge',
startedAt = Date.now(),
): void {
): Promise<void> {
if (source === 'bridge') {
this.cacheMessageEvent(event);
if (this.consumePendingSelfSentEcho(event)) {
@@ -386,9 +392,11 @@ export class OneBotInstance {
return;
}
this.traceEventHandoffTerminal(event, startedAt, 'reporting_started');
void this.networkManager.emitEvent(event).catch((err) => {
try {
await this.networkManager.emitEvent(event);
} catch (err) {
this.log.warn('emitEvent failed: %s', err instanceof Error ? (err.stack ?? err.message) : String(err));
});
}
}
private traceEventHandoffTerminal(
@@ -654,6 +662,21 @@ export class OneBotInstance {
}
}
export function makeBotStatusEvent(
selfId: number,
subType: 'online' | 'offline',
time = Math.floor(Date.now() / 1000),
): JsonObject {
return {
time,
self_id: selfId,
post_type: 'notice',
notice_type: 'bot_status',
sub_type: subType,
user_id: selfId,
};
}
function toInt(value: unknown): number {
if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value);
if (typeof value === 'string' && value.trim()) {