Fix: listen for the low-level client 'event' event instead of Room.timeline re-emit
This commit is contained in:
30
index.js
30
index.js
@@ -73,24 +73,26 @@ function loadQuotes() {
|
||||
});
|
||||
|
||||
// Respond to messages in any room this bot is a member of.
|
||||
client.on('Room.timeline', (event, room) => {
|
||||
if (!room) return; // thread timeline events don't carry a room object
|
||||
|
||||
const type = event.getType();
|
||||
if (type !== 'm.room.message') return;
|
||||
// We use the low-level 'event' client event, which sync.js emits
|
||||
// directly for every event in every room (not a re-emit).
|
||||
client.on('event', (event) => {
|
||||
if (event.getType() !== 'm.room.message') return;
|
||||
|
||||
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 (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 + ' ')) {
|
||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||
client.sendTextMessage(room.roomId, quote)
|
||||
|
||||
Reference in New Issue
Block a user