Fix: listen for the low-level client 'event' event instead of Room.timeline re-emit

This commit is contained in:
unfunny
2026-09-13 16:45:45 -04:00
parent 59a94036d0
commit 5b431912e5

View File

@@ -73,24 +73,26 @@ function loadQuotes() {
}); });
// Respond to messages in any room this bot is a member of. // Respond to messages in any room this bot is a member of.
client.on('Room.timeline', (event, room) => { // We use the low-level 'event' client event, which sync.js emits
if (!room) return; // thread timeline events don't carry a room object // directly for every event in every room (not a re-emit).
client.on('event', (event) => {
const type = event.getType(); if (event.getType() !== 'm.room.message') return;
if (type !== 'm.room.message') return;
const sender = event.getSender(); const sender = event.getSender();
const membership = room.getMyMembership();
const content = event.getContent();
const body = typeof content?.body === 'string' ? content.body : '(no body)';
console.log(`✉️ [${room.roomId}] membership="${membership}" sender="${sender}" body="${body}"`);
if (sender === userId) return; if (sender === userId) return;
if (membership !== 'join') return;
if (body === '(no body)') return;
const lower = body.toLowerCase().trim(); const room = client.getRoom(event.getRoomId());
if (!room) return;
const membership = room.getMyMembership();
if (membership !== 'join') return;
const content = event.getContent();
if (!content || typeof content.body !== 'string') return;
console.log(`✉️ [${room.roomId}] membership="${membership}" sender="${sender}" body="${content.body}"`);
const lower = content.body.toLowerCase().trim();
if (lower === cmd || lower.startsWith(cmd + ' ')) { if (lower === cmd || lower.startsWith(cmd + ' ')) {
const quote = quotes[Math.floor(Math.random() * quotes.length)]; const quote = quotes[Math.floor(Math.random() * quotes.length)];
client.sendTextMessage(room.roomId, quote) client.sendTextMessage(room.roomId, quote)