Refactor OneBot networks and event dispatch

Unify per-adapter network configuration into a single `networks` structure and add migration/normalization for legacy on-disk formats. Introduce per-adapter options (messageFormat, reportSelfMessage, enabled, name) and stronger typing in types.ts. Add event-filter utilities to prebuild two serialized payload variants and pick the right one per adapter to avoid repeated JSON.stringify calls. Update HTTP/WS transports to use adapter names/signatures for hot-reload, respect per-adapter options, and improve logging; WS transport also improves client/server lifecycle/reconnect handling. Mark self-sent messages as `message_sent` in the event converter and wire instance dispatch to use the new DispatchPayload. Misc: README edits and accompanying tests added/updated for the refactor.
This commit is contained in:
手瓜一十雪 committed 2026-05-07 16:01:28 +08:00
1 parent 19a564991c
commit 2a4ca46a99
14 files changed
+1732 -758

No files matched your search

+12 -6
View File
@@ -12,6 +12,7 @@ import type { JsonObject, MessageMeta, OneBotConfig } from './types';
import { HttpTransport } from './http-transport';
import { HttpPostTransport } from './http-post-transport';
import { WsTransport } from './ws-transport';
import { buildDispatchPayload } from './event-filter';
import { createLogger } from '../utils/logger';
const log = createLogger('Event');
@@ -157,12 +158,16 @@ export class OneBotInstance {
private dispatchEvent(event: JsonObject): void {
this.cacheMessageEvent(event);
this.logReceivedMessage(event);
this.wsTransport.publishEvent(event);
this.httpPostTransport.publishEvent(event);
// Produce one canonical event, serialize the two format variants once,
// then let each adapter pick its slot at zero allocation cost.
const payload = buildDispatchPayload(event);
this.wsTransport.publishEvent(event, payload);
this.httpPostTransport.publishEvent(event, payload);
}
private logReceivedMessage(event: JsonObject): void {
if (event.post_type !== 'message') return;
const isSelf = event.post_type === 'message_sent';
if (event.post_type !== 'message' && !isSelf) return;
const messageId = toInt(event.message_id);
const isGroup = event.message_type === 'group';
@@ -203,23 +208,24 @@ export class OneBotInstance {
const content = parts.join(' ').trim() || '[空消息]';
const idStr = `ID:${messageId}`;
const selfTag = isSelf ? '[自身] ' : '';
if (isGroup) {
const groupId = toInt(event.group_id);
const userId = toInt(event.user_id);
const sender = event.sender as any;
const nickname = sender?.card || sender?.nickname || String(userId);
log.success(`群 ${groupId} | ${nickname}(${userId}): ${idStr} ${content}`);
log.success(`${selfTag}群 ${groupId} | ${nickname}(${userId}): ${idStr} ${content}`);
} else {
const userId = toInt(event.user_id);
const sender = event.sender as any;
const nickname = sender?.nickname || String(userId);
log.success(`私聊 ${nickname}(${userId}): ${idStr} ${content}`);
log.success(`${selfTag}私聊 ${nickname}(${userId}): ${idStr} ${content}`);
}
}
private cacheMessageEvent(event: JsonObject): void {
if (event.post_type !== 'message') return;
if (event.post_type !== 'message' && event.post_type !== 'message_sent') return;
const messageId = toInt(event.message_id);
if (messageId === 0) return;