Fix: use correct matrix-js-sdk v27 event names (Room.timeline, RoomMember.membership) and auto-accept invites

This commit is contained in:
unfunny
2026-09-13 16:27:27 -04:00
parent 979eaaab13
commit bef1ddb723

View File

@@ -45,10 +45,6 @@ function loadQuotes() {
console.log(`🏠 Server: ${server}`); console.log(`🏠 Server: ${server}`);
try { try {
// The SDK needs to know the bot's own user ID to build request URLs
// (e.g. /user/$userId/filter). It would otherwise use the literal
// "$userId" placeholder, which the homeserver rejects. Fetch it via
// the whoami endpoint before connecting.
const whoami = await client.whoami(); const whoami = await client.whoami();
const userId = whoami.user_id; const userId = whoami.user_id;
@@ -63,40 +59,40 @@ function loadQuotes() {
await client.startClient(); await client.startClient();
console.log('✅ MatrixClient connected and authenticated\n'); console.log('✅ MatrixClient connected and authenticated\n');
let lastSentMessage = null;
client.on('RoomMember#joined', async (member) => {
const room = member.room;
console.log(`✅ Room: ${room.getCanonicalRoomName() || room.roomId}`);
room.on('EventMessage', async (event, stateEvent) => {
const content = event.getContent();
if (!content || !content.body) return;
const body = content.body.toLowerCase().trim();
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase(); const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
if (body === cmd || body.startsWith(cmd)) { // Auto-accept invites so the bot can be invited into rooms.
if (!lastSentMessage || Date.now() - lastSentMessage > 300) { client.on('RoomMember.membership', (event, member) => {
const quote = quotes[Math.floor(Math.random() * quotes.length)]; if (member.userId === userId && member.membership === 'invite') {
try { console.log(`📩 Invited to ${member.room.roomId}, joining...`);
await room.sendMessage(quote); client.joinRoom(member.room.roomId).catch((err) => {
console.log(`🎃 Quote: ${quote}`); console.error('Failed to join room:', err.message);
lastSentMessage = Date.now(); });
} catch (err) {
console.error('Failed to send quote:', err.message);
}
}
} }
}); });
member.room.on('RoomMember#left', () => { // Respond to messages in any room this bot is a member of.
console.log(`ℹ️ Left: ${room.getCanonicalRoomName() || room.roomId}`); client.on('Room.timeline', (event, room) => {
}); if (event.getType() !== 'm.room.message') return;
if (event.getSender() === userId) return;
const membership = room.getMyMembership();
if (membership !== 'join') return;
const content = event.getContent();
if (!content || typeof content.body !== 'string') return;
const body = content.body.toLowerCase().trim();
if (body === cmd || body.startsWith(cmd + ' ')) {
const quote = quotes[Math.floor(Math.random() * quotes.length)];
client.sendTextMessage(room.roomId, quote)
.then(() => console.log(`🎃 [${room.roomId}] ${quote}`))
.catch((err) => console.error('Failed to send quote:', err.message));
}
}); });
console.log('🤖 Bot is running!'); console.log('🤖 Bot is running!');
console.log(' Invite this bot to a Matrix room and type !quote to get an Ancestor quote.\n'); console.log(` Invite this bot to a room and type ${process.env.BOT_COMMAND || '!quote'} to get an Ancestor quote.\n`);
} catch (err) { } catch (err) {
console.error('❌ Fatal error starting bot:', err.message); console.error('❌ Fatal error starting bot:', err.message);