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()) {
+28 -6
View File
@@ -9,7 +9,7 @@ import {
createMessageStoreMigrationTask,
estimateRemainingSeconds,
} from './message-store-migration-task';
import type { AdapterStatus, NetworkApplyError } from './network';
import type { AdapterStatus, NetworkApplyError, NetworkShutdownResult } from './network';
import type { OneBotConfig } from './types';
const log = createLogger('OneBot');
@@ -351,10 +351,20 @@ export class OneBotManager {
this.instances.set(uin, instance);
log.info('session started: UIN=%s', uin);
this.trackLifecycle(`network startup UIN=${uin}`, instance.waitUntilNetworkReady().then((result) => {
this.trackLifecycle(`network startup UIN=${uin}`, instance.waitUntilNetworkReady().then(async (result) => {
if (result.applied) log.info('network startup applied: UIN=%s adapters=%d', uin, result.statuses.length);
else log.warn('network startup degraded: UIN=%s failures=%d', uin, result.errors.length);
instance.startGroupRequestPolling();
if (this.disposed || this.instances.get(uin) !== instance) return;
try {
await instance.emitBotStatus('online');
} catch (error) {
log.warn(
'bot_status online failed: UIN=%s: %s',
uin,
error instanceof Error ? (error.stack ?? error.message) : String(error),
);
}
}));
void this.armLoginHistorySync(uin, instance, bridge);
}
@@ -412,15 +422,27 @@ export class OneBotManager {
this.retiringInstances.add(instance);
this.trackLifecycle(
`network shutdown UIN=${uin}`,
instance.dispose().then((result) => {
this.retirementSucceeded(instance);
return result;
}),
this.emitOfflineThenDispose(instance),
[instance],
);
log.info('session closed: UIN=%s', uin);
}
private async emitOfflineThenDispose(instance: OneBotInstance): Promise<NetworkShutdownResult> {
try {
await instance.emitBotStatus('offline');
} catch (error) {
log.warn(
'bot_status offline failed: UIN=%s: %s',
instance.uin,
error instanceof Error ? (error.stack ?? error.message) : String(error),
);
}
const result = await instance.dispose();
this.retirementSucceeded(instance);
return result;
}
private async finishRetiringBeforeStart(
uin: string,
pending: { bridge: BridgeInterface; cancelled: boolean },