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}`);
|
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;
|
||||||
|
|
||||||
@@ -62,41 +58,41 @@ 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 cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||||
const room = member.room;
|
|
||||||
console.log(`✅ Room: ${room.getCanonicalRoomName() || room.roomId}`);
|
|
||||||
|
|
||||||
room.on('EventMessage', async (event, stateEvent) => {
|
// Auto-accept invites so the bot can be invited into rooms.
|
||||||
const content = event.getContent();
|
client.on('RoomMember.membership', (event, member) => {
|
||||||
if (!content || !content.body) return;
|
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();
|
// Respond to messages in any room this bot is a member of.
|
||||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
client.on('Room.timeline', (event, room) => {
|
||||||
|
if (event.getType() !== 'm.room.message') return;
|
||||||
|
if (event.getSender() === userId) return;
|
||||||
|
|
||||||
if (body === cmd || body.startsWith(cmd)) {
|
const membership = room.getMyMembership();
|
||||||
if (!lastSentMessage || Date.now() - lastSentMessage > 300) {
|
if (membership !== 'join') return;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
member.room.on('RoomMember#left', () => {
|
const content = event.getContent();
|
||||||
console.log(`ℹ️ Left: ${room.getCanonicalRoomName() || room.roomId}`);
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user