feat: use native Matrix Power Level to control check-in immunity and cross-room syncing
1. Added RoomManagerBot for cross-room permission and leave syncing based on native power levels. 3. Added invite() to MatrixSession and added onMemberLeave/onPowerLevel hooks to Dispatcher. 4. Updated README and example configs.
This commit is contained in:
1 parent
440f572834
commit
0a3843630a
16 files changed
+257
-154
No files matched your search
@@ -26,6 +26,10 @@ export class DefaultBotContext implements BotContext {
|
||||
return this.transport.kick(roomId, userId, reason);
|
||||
}
|
||||
|
||||
invite(roomId: string, userId: string): Promise<void> {
|
||||
return this.transport.invite(roomId, userId);
|
||||
}
|
||||
|
||||
getMemberCount(roomId: string): Promise<number> {
|
||||
return this.transport.getMemberCount(roomId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InboundMessage, MemberEvent } from '../command/types';
|
||||
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';
|
||||
@@ -49,12 +49,14 @@ export class Dispatcher implements MatrixEventSink {
|
||||
}
|
||||
|
||||
async onMember(ev: MemberEvent): Promise<void> {
|
||||
if (ev.membership !== 'join') return;
|
||||
const targets = this.registry.resolve(ev.account, ev.roomId);
|
||||
for (const { bot, ctx } of targets) {
|
||||
if (!bot.onMemberJoin) continue;
|
||||
try {
|
||||
await bot.onMemberJoin(ev, ctx);
|
||||
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}`,
|
||||
@@ -62,4 +64,18 @@ export class Dispatcher implements MatrixEventSink {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
InboundMessage,
|
||||
MemberEvent,
|
||||
ParsedCommand,
|
||||
PowerLevelEvent,
|
||||
} from '../command/types';
|
||||
|
||||
/**
|
||||
@@ -15,6 +16,7 @@ export interface BotContext {
|
||||
send(roomId: string, text: string, replyToEventId?: string): Promise<string>;
|
||||
reply(msg: InboundMessage, text: string): Promise<string>;
|
||||
kick(roomId: string, userId: string, reason?: string): Promise<void>;
|
||||
invite(roomId: string, userId: string): Promise<void>;
|
||||
getMemberCount(roomId: string): Promise<number>;
|
||||
getJoinedMembers(roomId: string): Promise<string[]>;
|
||||
/** 读取用户在房间内的 power level,用于按房间权限判断管理员(读不到返回 0)。 */
|
||||
@@ -39,5 +41,7 @@ export interface RoomBot {
|
||||
onMessage?(msg: InboundMessage, ctx: BotContext): Promise<void>;
|
||||
onCommand?(cmd: ParsedCommand, ctx: BotContext): Promise<void>;
|
||||
onMemberJoin?(ev: MemberEvent, ctx: BotContext): Promise<void>;
|
||||
onMemberLeave?(ev: MemberEvent, ctx: BotContext): Promise<void>;
|
||||
onPowerLevel?(ev: PowerLevelEvent, ctx: BotContext): Promise<void>;
|
||||
registerCrons?(ctx: BotContext): CronSpec[];
|
||||
}
|
||||
Reference in new issue
Block a user