Compare commits

...

2 Commits

2 changed files with 27 additions and 11 deletions

View File

@@ -5,5 +5,9 @@ MATRIX_HOME_SERVER=https://matrix.org
MATRIX_ACCESS_TOKEN=your_real_access_token_here
BOT_COMMAND=!quote
# Stable device ID for the bot's E2EE identity. Keep this consistent
# across restarts so the bot reuses its encryption keys.
MATRIX_DEVICE_ID=ANCESTORBOT
# If you want debug logging, set DEBUG=true
DEBUG=false

View File

@@ -6,9 +6,12 @@ dotenv.config();
console.log('🚀 Starting Ancestor Quote Bot...\n');
const deviceId = process.env.MATRIX_DEVICE_ID || 'ANCESTORBOT';
const client = new MatrixClient({
accessToken: process.env.MATRIX_ACCESS_TOKEN,
baseUrl: process.env.MATRIX_HOME_SERVER,
deviceId,
});
function loadQuotes() {
@@ -54,7 +57,14 @@ function loadQuotes() {
}
client.credentials.userId = userId;
console.log(`👤 Bot Identity: ${userId}`);
console.log(`👤 Bot Identity: ${userId} (device ${deviceId})`);
// Enable end-to-end encryption so the bot can read and reply in
// encrypted rooms. The Rust/Olm WASM backend needs no native
// compile. useIndexedDB=false keeps everything in memory (no
// IndexedDB in Node).
await client.initRustCrypto({ useIndexedDB: false });
console.log('🔐 E2EE crypto initialised (Rust/Olm WASM)');
await client.startClient();
console.log('✅ MatrixClient connected and authenticated\n');
@@ -68,11 +78,6 @@ function loadQuotes() {
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
// 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()}`);
});
// Auto-accept invites so the bot can be invited into rooms.
client.on('RoomMember.membership', (event, member) => {
if (member.userId === userId && member.membership === 'invite') {
@@ -85,10 +90,19 @@ function loadQuotes() {
});
// Respond to messages in any room this bot is a member of.
// 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;
const type = event.getType();
// If an event still arrives encrypted, decryption failed — the
// room is likely set to only share keys with verified sessions.
if (type === 'm.room.encrypted') {
console.log(`🔒 [${event.getRoomId()}] could not decrypt message. ` +
`The room may be set to share keys only with verified sessions; ` +
`change room encryption settings to include this bot's device, or verify it.`);
return;
}
if (type !== 'm.room.message') return;
const sender = event.getSender();
if (sender === userId) return;
@@ -102,8 +116,6 @@ function loadQuotes() {
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();
if (lower === cmd || lower.startsWith(cmd + ' ')) {
const quote = quotes[Math.floor(Math.random() * quotes.length)];