Fix: resolve syntax errors in index.js and improve error handling
This commit is contained in:
28
index.js
28
index.js
@@ -33,7 +33,6 @@ function loadQuotes() {
|
||||
|
||||
console.log(`✅ Loaded ${quotes.length} Ancestor quotes`);
|
||||
|
||||
// Validate environment variables first
|
||||
const token = process.env.MATRIX_ACCESS_TOKEN;
|
||||
const server = process.env.MATRIX_HOME_SERVER;
|
||||
|
||||
@@ -51,12 +50,10 @@ function loadQuotes() {
|
||||
|
||||
let lastSentMessage = null;
|
||||
|
||||
// Listen for all membership events from homeserver
|
||||
client.on('RoomMember#joined', async (member) => {
|
||||
const room = member.room;
|
||||
console.log(`✅ Joined room: ${room.getCanonicalRoomName() || room.roomId}`);
|
||||
|
||||
// Add message listener to this room
|
||||
room.on('EventMessage', async (event, stateEvent) => {
|
||||
const content = event.getContent();
|
||||
if (!content || !content.body) return;
|
||||
@@ -78,13 +75,11 @@ function loadQuotes() {
|
||||
}
|
||||
});
|
||||
|
||||
// Handle leaving the room
|
||||
member.room.on('RoomMember#left', () => {
|
||||
console.log(`ℹ️ Left room: ${member.room.getCanonicalRoomName() || member.room.roomId}`);
|
||||
});
|
||||
});
|
||||
|
||||
// Also listen to messages from all known rooms (for cases where client is already joined)
|
||||
client.on('EventMessage', async (event, stateEvent) => {
|
||||
const room = event.sender.room;
|
||||
const content = event.getContent();
|
||||
@@ -93,7 +88,6 @@ function loadQuotes() {
|
||||
const body = content.body.toLowerCase().trim();
|
||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||
|
||||
// Only respond in rooms where we're a member
|
||||
const roomMembers = await room.getJoinedMemberCount();
|
||||
if (roomMembers > 0 && room.getMyMembership()) {
|
||||
if (body === cmd || body.startsWith(cmd)) {
|
||||
@@ -112,24 +106,18 @@ function loadQuotes() {
|
||||
});
|
||||
|
||||
console.log('🤖 Bot is running!');
|
||||
console.log(' Invite this bot to a Matrix room and type to get an Ancestor quote.\n');
|
||||
console.log(' Invite this bot to a Matrix room and type !quote to get an Ancestor quote.\n');
|
||||
|
||||
} catch (err) {
|
||||
console.error('Fatal error:', err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Add global error handler to catch any unhandled errors
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('🚨 Unhandled rejection:', reason?.message || reason);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// Keep the process alive
|
||||
setInterval(() => {}, 60000);
|
||||
} catch (err) {
|
||||
console.error('❌ Fatal error starting bot:', err.message);
|
||||
console.error(err.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('🚨 Unhandled rejection:', reason?.message || reason);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
setInterval(() => {}, 60000);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user