Fix: use correct matrix-js-sdk v27 event names (Room.timeline, RoomMember.membership) and auto-accept invites
This commit is contained in:
58
index.js
58
index.js
@@ -45,10 +45,6 @@ function loadQuotes() {
|
||||
console.log(`🏠 Server: ${server}`);
|
||||
|
||||
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 userId = whoami.user_id;
|
||||
|
||||
@@ -62,41 +58,41 @@ function loadQuotes() {
|
||||
|
||||
await client.startClient();
|
||||
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}`);
|
||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||
|
||||
room.on('EventMessage', async (event, stateEvent) => {
|
||||
const content = event.getContent();
|
||||
if (!content || !content.body) return;
|
||||
// Auto-accept invites so the bot can be invited into rooms.
|
||||
client.on('RoomMember.membership', (event, member) => {
|
||||
if (member.userId === userId && member.membership === 'invite') {
|
||||
console.log(`📩 Invited to ${member.room.roomId}, joining...`);
|
||||
client.joinRoom(member.room.roomId).catch((err) => {
|
||||
console.error('Failed to join room:', err.message);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const body = content.body.toLowerCase().trim();
|
||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||
// Respond to messages in any room this bot is a member of.
|
||||
client.on('Room.timeline', (event, room) => {
|
||||
if (event.getType() !== 'm.room.message') return;
|
||||
if (event.getSender() === userId) return;
|
||||
|
||||
if (body === cmd || body.startsWith(cmd)) {
|
||||
if (!lastSentMessage || Date.now() - lastSentMessage > 300) {
|
||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||
try {
|
||||
await room.sendMessage(quote);
|
||||
console.log(`🎃 Quote: ${quote}`);
|
||||
lastSentMessage = Date.now();
|
||||
} catch (err) {
|
||||
console.error('Failed to send quote:', err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const membership = room.getMyMembership();
|
||||
if (membership !== 'join') return;
|
||||
|
||||
member.room.on('RoomMember#left', () => {
|
||||
console.log(`ℹ️ Left: ${room.getCanonicalRoomName() || room.roomId}`);
|
||||
});
|
||||
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(' 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) {
|
||||
console.error('❌ Fatal error starting bot:', err.message);
|
||||
|
||||
Reference in New Issue
Block a user