2026-09-13 00:55:46 -04:00
|
|
|
import * as dotenv from 'dotenv';
|
|
|
|
|
import { MatrixClient } from 'matrix-js-sdk';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
|
|
dotenv.config();
|
2026-09-12 23:57:45 -04:00
|
|
|
|
2026-09-13 14:14:38 -04:00
|
|
|
console.log('🚀 Starting Ancestor Quote Bot...\n');
|
|
|
|
|
|
2026-09-13 00:53:39 -04:00
|
|
|
const client = new MatrixClient({
|
2026-09-12 23:57:45 -04:00
|
|
|
accessToken: process.env.MATRIX_ACCESS_TOKEN,
|
2026-09-13 14:14:38 -04:00
|
|
|
baseUrl: process.env.MATRIX_HOME_SERVER,
|
2026-09-12 23:57:45 -04:00
|
|
|
});
|
|
|
|
|
|
2026-09-13 00:51:26 -04:00
|
|
|
function loadQuotes() {
|
2026-09-13 14:14:38 -04:00
|
|
|
try {
|
|
|
|
|
const quotesFile = './quotes.json';
|
|
|
|
|
if (fs.existsSync(quotesFile)) {
|
|
|
|
|
return JSON.parse(fs.readFileSync(quotesFile, 'utf8'));
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error loading quotes:', err.message);
|
|
|
|
|
}
|
|
|
|
|
return [];
|
2026-09-13 00:48:25 -04:00
|
|
|
}
|
|
|
|
|
|
2026-09-13 14:14:38 -04:00
|
|
|
(async () => {
|
|
|
|
|
const quotes = loadQuotes();
|
|
|
|
|
|
2026-09-13 00:51:26 -04:00
|
|
|
if (quotes.length === 0) {
|
2026-09-13 14:14:38 -04:00
|
|
|
console.error('❌ No quotes found! Please ensure quotes.json is present.');
|
2026-09-13 00:51:26 -04:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2026-09-13 00:48:25 -04:00
|
|
|
|
2026-09-13 14:14:38 -04:00
|
|
|
console.log(`✅ Loaded ${quotes.length} Ancestor quotes`);
|
2026-09-13 15:15:21 -04:00
|
|
|
|
2026-09-13 14:52:17 -04:00
|
|
|
const token = process.env.MATRIX_ACCESS_TOKEN;
|
|
|
|
|
const server = process.env.MATRIX_HOME_SERVER;
|
2026-09-13 00:58:22 -04:00
|
|
|
|
2026-09-13 14:52:17 -04:00
|
|
|
if (!token || !server) {
|
|
|
|
|
console.error('❌ Missing required environment variables!');
|
|
|
|
|
console.error(' Required: MATRIX_ACCESS_TOKEN, MATRIX_HOME_SERVER');
|
|
|
|
|
console.error(' See .env.example for guidance.');
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
console.log(`🏠 Server: ${server}`);
|
2026-09-13 15:15:21 -04:00
|
|
|
|
2026-09-13 14:14:38 -04:00
|
|
|
try {
|
2026-09-13 16:17:14 -04:00
|
|
|
const whoami = await client.whoami();
|
|
|
|
|
const userId = whoami.user_id;
|
2026-09-13 15:41:46 -04:00
|
|
|
|
2026-09-13 16:17:14 -04:00
|
|
|
if (!userId || !userId.startsWith('@')) {
|
|
|
|
|
console.error(`❌ whoami returned invalid user ID: ${userId}`);
|
|
|
|
|
process.exit(1);
|
2026-09-13 15:20:12 -04:00
|
|
|
}
|
|
|
|
|
|
2026-09-13 16:17:14 -04:00
|
|
|
client.credentials.userId = userId;
|
|
|
|
|
console.log(`👤 Bot Identity: ${userId}`);
|
|
|
|
|
|
|
|
|
|
await client.startClient();
|
|
|
|
|
console.log('✅ MatrixClient connected and authenticated\n');
|
2026-09-13 16:27:27 -04:00
|
|
|
|
2026-09-13 16:50:55 -04:00
|
|
|
const joinedRooms = client.getRooms();
|
|
|
|
|
console.log(`🏠 Current rooms: ${joinedRooms.length}`);
|
|
|
|
|
joinedRooms.forEach((r) => {
|
|
|
|
|
console.log(` - ${r.roomId} membership="${r.getMyMembership()}"`);
|
|
|
|
|
});
|
|
|
|
|
console.log('');
|
|
|
|
|
|
2026-09-13 16:27:27 -04:00
|
|
|
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
|
|
|
|
|
2026-09-13 16:50:55 -04:00
|
|
|
// Diagnostic: log EVERY event the client receives, regardless of type.
|
|
|
|
|
client.on('event', (event) => {
|
|
|
|
|
console.log(`EVENT type="${event.getType()}" room=${event.getRoomId()} sender=${event.getSender()}`);
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-13 16:27:27 -04:00
|
|
|
// Auto-accept invites so the bot can be invited into rooms.
|
|
|
|
|
client.on('RoomMember.membership', (event, member) => {
|
|
|
|
|
if (member.userId === userId && member.membership === 'invite') {
|
2026-09-13 16:30:54 -04:00
|
|
|
const roomId = event.getRoomId();
|
|
|
|
|
console.log(`📩 Invited to ${roomId}, joining...`);
|
|
|
|
|
client.joinRoom(roomId).catch((err) => {
|
2026-09-13 16:27:27 -04:00
|
|
|
console.error('Failed to join room:', err.message);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Respond to messages in any room this bot is a member of.
|
2026-09-13 16:45:45 -04:00
|
|
|
// 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;
|
2026-09-13 16:27:27 -04:00
|
|
|
|
2026-09-13 16:35:12 -04:00
|
|
|
const sender = event.getSender();
|
2026-09-13 16:45:45 -04:00
|
|
|
if (sender === userId) return;
|
2026-09-13 16:35:12 -04:00
|
|
|
|
2026-09-13 16:45:45 -04:00
|
|
|
const room = client.getRoom(event.getRoomId());
|
|
|
|
|
if (!room) return;
|
2026-09-13 16:35:12 -04:00
|
|
|
|
2026-09-13 16:45:45 -04:00
|
|
|
const membership = room.getMyMembership();
|
2026-09-13 16:35:12 -04:00
|
|
|
if (membership !== 'join') return;
|
2026-09-13 16:27:27 -04:00
|
|
|
|
2026-09-13 16:45:45 -04:00
|
|
|
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();
|
2026-09-13 16:35:12 -04:00
|
|
|
if (lower === cmd || lower.startsWith(cmd + ' ')) {
|
2026-09-13 16:27:27 -04:00
|
|
|
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));
|
|
|
|
|
}
|
2026-09-13 14:39:02 -04:00
|
|
|
});
|
2026-09-13 16:17:14 -04:00
|
|
|
|
2026-09-13 14:14:38 -04:00
|
|
|
console.log('🤖 Bot is running!');
|
2026-09-13 16:27:27 -04:00
|
|
|
console.log(` Invite this bot to a room and type ${process.env.BOT_COMMAND || '!quote'} to get an Ancestor quote.\n`);
|
2026-09-13 16:17:14 -04:00
|
|
|
|
2026-09-13 14:52:17 -04:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error('❌ Fatal error starting bot:', err.message);
|
|
|
|
|
console.error(err.stack);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2026-09-13 15:15:21 -04:00
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, promise) => {
|
|
|
|
|
console.error('🚨 Unhandled rejection:', reason?.message || reason);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setInterval(() => {}, 60000);
|
2026-09-13 16:17:14 -04:00
|
|
|
})();
|