Add room-scoped Matrix plugins and bilingual configuration commands

This commit is contained in:
Matrix Bot Project committed 2026-11-20 23:29:33 +08:00
1 parent 061bfbd12a
commit f4f30c3b5c
75 files changed
+2559 -1975

No files matched your search

+11 -70
View File
@@ -1,81 +1,22 @@
import { Injectable, Logger } from '@nestjs/common';
import { InboundMessage, MemberEvent, PowerLevelEvent } from '../command/types';
import { CommandError, parseCommand } from '../command/command-parser';
import { MatrixEventSink } from '../matrix/types';
import { BotRegistry } from './bot-registry.service';
import { PluginManager } from '../plugin/plugin-manager.service';
/** 命令前缀。非此前缀的消息只走 onMessage,不解析为命令。 */
const COMMAND_PREFIX = '!';
/**
* 把归一化事件按 (account, roomId) 路由到对应 bot 的钩子。
* 实现 MatrixEventSink,注册进每个 MatrixSession。
*/
@Injectable()
export class Dispatcher implements MatrixEventSink {
private readonly logger = new Logger(Dispatcher.name);
constructor(private readonly registry: BotRegistry) {}
async onMessage(msg: InboundMessage): Promise<void> {
const targets = this.registry.resolve(msg.account, msg.roomId);
if (targets.length === 0) return;
// 通用归一化:全角叹号→半角、换行→空格,便于中文输入触发命令。
const text = msg.text.replace(/!/g, '!').replace(/[\r\n]+/g, ' ').trim();
const commandText = text.startsWith(COMMAND_PREFIX)
? text.slice(COMMAND_PREFIX.length)
: undefined;
for (const { bot, ctx } of targets) {
try {
if (bot.onMessage) {
await bot.onMessage(msg, ctx);
}
if (commandText !== undefined && bot.onCommand) {
const cmd = parseCommand(msg, commandText);
await bot.onCommand(cmd, ctx);
}
} catch (err) {
if (err instanceof CommandError) {
this.logger.debug(`命令解析失败 (${bot.name}): ${err.message}`);
} else {
this.logger.warn(
`bot ${bot.name} 处理消息失败 (room=${msg.roomId}): ${(err as Error).message}`,
);
}
}
}
constructor(private readonly plugins: PluginManager) {}
async onMessage(message: InboundMessage): Promise<void> {
try { await this.plugins.onMessage(message); }
catch { this.logger.error('Message processing failed'); }
}
async onMember(ev: MemberEvent): Promise<void> {
const targets = this.registry.resolve(ev.account, ev.roomId);
for (const { bot, ctx } of targets) {
try {
if (ev.membership === 'join' && bot.onMemberJoin) {
await bot.onMemberJoin(ev, ctx);
} else if (ev.membership === 'leave' && bot.onMemberLeave) {
await bot.onMemberLeave(ev, ctx);
}
} catch (err) {
this.logger.warn(
`bot ${bot.name} 处理成员事件失败: ${(err as Error).message}`,
);
}
}
async onMember(event: MemberEvent): Promise<void> {
try { await this.plugins.onEvent(event); }
catch { this.logger.error('Member event processing failed'); }
}
async onPowerLevel(ev: PowerLevelEvent): Promise<void> {
const targets = this.registry.resolve(ev.account, ev.roomId);
for (const { bot, ctx } of targets) {
if (!bot.onPowerLevel) continue;
try {
await bot.onPowerLevel(ev, ctx);
} catch (err) {
this.logger.warn(
`bot ${bot.name} 处理权限变更事件失败: ${(err as Error).message}`,
);
}
}
async onPowerLevel(event: PowerLevelEvent): Promise<void> {
try { await this.plugins.onEvent(event); }
catch { this.logger.error('Power event processing failed'); }
}
}