fix(matrix-bot-common): ignore messages from other bots

Prevents infinite message loops when multiple bots are in the same room.
Checks if sender has '-bot' in their Matrix user ID localpart.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Till-JS 2026-02-14 13:40:59 +01:00
parent c8ab4bbcbe
commit 2e8b3b903b

View file

@ -158,6 +158,18 @@ export abstract class BaseMatrixService implements OnModuleInit, OnModuleDestroy
}
}
/**
* Check if a sender is a bot (has "-bot" in the localpart)
* Bots should not respond to each other to avoid infinite loops
*/
protected isBot(sender: string): boolean {
// Extract localpart from @user:server format
const match = sender.match(/^@([^:]+):/);
if (!match) return false;
const localpart = match[1].toLowerCase();
return localpart.includes('-bot') || localpart.endsWith('bot');
}
/**
* Handle incoming room message
*/
@ -165,6 +177,9 @@ export abstract class BaseMatrixService implements OnModuleInit, OnModuleDestroy
// Ignore own messages
if (event.sender === this.botUserId) return;
// Ignore messages from other bots to prevent infinite loops
if (this.isBot(event.sender)) return;
// Check room permissions
if (this.allowedRooms.length > 0 && !this.allowedRooms.includes(roomId)) {
return;