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
|
|
|
|
// 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;
|
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 15:20:12 -04:00
|
|
|
|
|
2026-09-13 14:14:38 -04:00
|
|
|
|
let lastSentMessage = null;
|
2026-09-13 16:17:14 -04:00
|
|
|
|
|
2026-09-13 14:39:02 -04:00
|
|
|
|
client.on('RoomMember#joined', async (member) => {
|
2026-09-13 14:14:38 -04:00
|
|
|
|
const room = member.room;
|
2026-09-13 16:17:14 -04:00
|
|
|
|
console.log(`✅ Room: ${room.getCanonicalRoomName() || room.roomId}`);
|
|
|
|
|
|
|
2026-09-13 14:39:02 -04:00
|
|
|
|
room.on('EventMessage', async (event, stateEvent) => {
|
|
|
|
|
|
const content = event.getContent();
|
|
|
|
|
|
if (!content || !content.body) return;
|
2026-09-13 16:17:14 -04:00
|
|
|
|
|
2026-09-13 14:39:02 -04:00
|
|
|
|
const body = content.body.toLowerCase().trim();
|
|
|
|
|
|
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
2026-09-13 16:17:14 -04:00
|
|
|
|
|
2026-09-13 14:39:02 -04:00
|
|
|
|
if (body === cmd || body.startsWith(cmd)) {
|
|
|
|
|
|
if (!lastSentMessage || Date.now() - lastSentMessage > 300) {
|
|
|
|
|
|
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
|
|
|
|
|
try {
|
2026-09-13 14:14:38 -04:00
|
|
|
|
await room.sendMessage(quote);
|
|
|
|
|
|
console.log(`🎃 Quote: ${quote}`);
|
|
|
|
|
|
lastSentMessage = Date.now();
|
2026-09-13 14:39:02 -04:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error('Failed to send quote:', err.message);
|
2026-09-13 14:14:38 -04:00
|
|
|
|
}
|
2026-09-13 00:51:26 -04:00
|
|
|
|
}
|
2026-09-13 14:39:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-09-13 16:17:14 -04:00
|
|
|
|
|
2026-09-13 14:39:02 -04:00
|
|
|
|
member.room.on('RoomMember#left', () => {
|
2026-09-13 16:17:14 -04:00
|
|
|
|
console.log(`ℹ️ Left: ${room.getCanonicalRoomName() || room.roomId}`);
|
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 15:15:21 -04:00
|
|
|
|
console.log(' Invite this bot to a Matrix room and type !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
|
|
|
|
})();
|