From 3afe38464085e49490e97cf1aa6d8a70fea8207a Mon Sep 17 00:00:00 2001 From: eskima Date: Mon, 8 Jun 2026 03:15:34 +0800 Subject: [PATCH] fix(room-manager): gracefully handle matrix membership errors and send summary messages to room --- src/bots/room-manager/room-manager.bot.ts | 86 ++++++++++++++++++----- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/src/bots/room-manager/room-manager.bot.ts b/src/bots/room-manager/room-manager.bot.ts index b5f2e03..39f7703 100644 --- a/src/bots/room-manager/room-manager.bot.ts +++ b/src/bots/room-manager/room-manager.bot.ts @@ -63,35 +63,87 @@ export class RoomManagerBot implements RoomBot { for (const [userId, power] of Object.entries(users)) { try { + let targetState = ''; + const actions: Array<{ action: 'kick' | 'invite'; roomName: string; roomId: string; reason?: string }> = []; + if (power >= levelA) { - await ctx.invite(workRoom1, userId); - await ctx.invite(workRoom2, userId); - this.logger.log(`同步 ${userId} -> A 级 (进1、2)`); + targetState = 'A级(高级)'; + actions.push({ action: 'invite', roomName: '工作房1', roomId: workRoom1 }); + actions.push({ action: 'invite', roomName: '工作房2', roomId: workRoom2 }); } else if (power === levelB) { - await ctx.kick(workRoom1, userId, '权限下降,移出高级群'); - await ctx.invite(workRoom2, userId); - this.logger.log(`同步 ${userId} -> B 级 (踢1、进2)`); + targetState = 'B级(普通)'; + actions.push({ action: 'kick', roomName: '工作房1', roomId: workRoom1, reason: '权限不足' }); + actions.push({ action: 'invite', roomName: '工作房2', roomId: workRoom2 }); } else if (power <= levelC) { - await this.punishUser(userId, ctx); - this.logger.log(`同步 ${userId} -> C 级 (踢1、2,进冷宫)`); + targetState = 'C级(失联)'; + actions.push({ action: 'kick', roomName: '工作房1', roomId: workRoom1, reason: '打入冷宫' }); + actions.push({ action: 'kick', roomName: '工作房2', roomId: workRoom2, reason: '打入冷宫' }); + actions.push({ action: 'invite', roomName: '失联房', roomId: lostRoom }); + } else { + continue; // 其他中间分数不处理 } + + await this.executeActions(ctx, ev.roomId, userId, targetState, actions, '👮 权限同步'); } catch (err) { - this.logger.error(`同步用户 ${userId} 失败: ${err}`); + this.logger.error(`处理用户 ${userId} 权限异常: ${err}`); } } } async onMemberLeave(ev: MemberEvent, ctx: BotContext): Promise { - this.logger.log(`检测到 ${ev.userId} 离开了签到群(主动退出或被签到程序踢出),执行失联处理...`); - await this.punishUser(ev.userId, ctx); - } - - private async punishUser(userId: string, ctx: BotContext): Promise { const { workRoom1, workRoom2, lostRoom } = this.cfg; if (!workRoom1 || !workRoom2 || !lostRoom) return; - await ctx.kick(workRoom1, userId, '打入冷宫'); - await ctx.kick(workRoom2, userId, '打入冷宫'); - await ctx.invite(lostRoom, userId); + + const actions: Array<{ action: 'kick' | 'invite'; roomName: string; roomId: string; reason?: string }> = [ + { action: 'kick', roomName: '工作房1', roomId: workRoom1, reason: '离开大本营' }, + { action: 'kick', roomName: '工作房2', roomId: workRoom2, reason: '离开大本营' }, + { action: 'invite', roomName: '失联房', roomId: lostRoom }, + ]; + + await this.executeActions(ctx, ev.roomId, ev.userId, '离群(失联)', actions, '🚪 离群联动'); + } + + private async executeActions( + ctx: BotContext, + controlRoomId: string, + userId: string, + targetState: string, + actions: Array<{ action: 'kick' | 'invite'; roomName: string; roomId: string; reason?: string }>, + title: string, + ): Promise { + const logs: string[] = []; + let hasActualChanges = false; + + for (const req of actions) { + try { + if (req.action === 'kick') { + await ctx.kick(req.roomId, userId, req.reason); + logs.push(`${req.roomName}: 移出成功`); + hasActualChanges = true; + } else { + await ctx.invite(req.roomId, userId); + logs.push(`${req.roomName}: 拉入成功`); + hasActualChanges = true; + } + } catch (err: any) { + const msg = err.message || ''; + if (req.action === 'kick' && msg.includes('not in the room')) { + // 本来就不在,无需处理,不产生噪音 + } else if (req.action === 'invite' && msg.includes('already in the room')) { + // 本来就在,无需处理,不产生噪音 + } else { + // 真正的意外失败 + logs.push(`${req.roomName}: ${req.action === 'kick' ? '移出' : '拉入'}失败 (${msg})`); + hasActualChanges = true; // 把失败当作一种状态变化汇报给群里 + } + } + } + + // 只有在真正踢了人、拉了人,或者遇到意外报错时,才在群里打字播报 + if (hasActualChanges && logs.length > 0) { + const msg = `${title}: 将 ${userId} 设为 ${targetState}\n` + logs.map(l => `- ${l}`).join('\n'); + await ctx.send(controlRoomId, msg); + } } }