Compare commits
2 Commits
encryption
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45c2aeea6d | ||
|
|
e16a4b44d5 |
31
index.js
31
index.js
@@ -1,5 +1,6 @@
|
|||||||
import * as dotenv from 'dotenv';
|
import * as dotenv from 'dotenv';
|
||||||
import { MatrixClient } from 'matrix-js-sdk';
|
import { MatrixClient } from 'matrix-js-sdk';
|
||||||
|
import { MemoryStore } from 'matrix-js-sdk/lib/store/memory.js';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
@@ -9,6 +10,7 @@ console.log('🚀 Starting Ancestor Quote Bot...\n');
|
|||||||
const client = new MatrixClient({
|
const client = new MatrixClient({
|
||||||
accessToken: process.env.MATRIX_ACCESS_TOKEN,
|
accessToken: process.env.MATRIX_ACCESS_TOKEN,
|
||||||
baseUrl: process.env.MATRIX_HOME_SERVER,
|
baseUrl: process.env.MATRIX_HOME_SERVER,
|
||||||
|
store: new MemoryStore(),
|
||||||
});
|
});
|
||||||
|
|
||||||
function loadQuotes() {
|
function loadQuotes() {
|
||||||
@@ -67,6 +69,7 @@ function loadQuotes() {
|
|||||||
console.log('');
|
console.log('');
|
||||||
|
|
||||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||||
|
console.log(`🔊 Listening for command: "${cmd}"`);
|
||||||
|
|
||||||
// Auto-accept invites so the bot can be invited into rooms.
|
// Auto-accept invites so the bot can be invited into rooms.
|
||||||
client.on('RoomMember.membership', (event, member) => {
|
client.on('RoomMember.membership', (event, member) => {
|
||||||
@@ -82,11 +85,12 @@ function loadQuotes() {
|
|||||||
// Respond to messages in any room this bot is a member of.
|
// Respond to messages in any room this bot is a member of.
|
||||||
client.on('event', (event) => {
|
client.on('event', (event) => {
|
||||||
const type = event.getType();
|
const type = event.getType();
|
||||||
|
const eventRoomId = event.getRoomId();
|
||||||
|
|
||||||
// Encrypted rooms: the bot has no E2EE support, so command text
|
// Encrypted rooms: the bot has no E2EE support, so command text
|
||||||
// never arrives as plaintext and cannot be read or answered.
|
// never arrives as plaintext and cannot be read or answered.
|
||||||
if (type === 'm.room.encrypted') {
|
if (type === 'm.room.encrypted') {
|
||||||
console.log(`🔒 [${event.getRoomId()}] encrypted message received. ` +
|
console.log(`🔒 [${eventRoomId}] encrypted message received. ` +
|
||||||
`The bot cannot read E2EE rooms; invite it to an unencrypted room instead.`);
|
`The bot cannot read E2EE rooms; invite it to an unencrypted room instead.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -94,24 +98,35 @@ function loadQuotes() {
|
|||||||
if (type !== 'm.room.message') return;
|
if (type !== 'm.room.message') return;
|
||||||
|
|
||||||
const sender = event.getSender();
|
const sender = event.getSender();
|
||||||
if (sender === userId) return;
|
if (sender === userId) return; // skip replies from ourselves
|
||||||
|
|
||||||
const room = client.getRoom(event.getRoomId());
|
const room = eventRoomId ? client.getRoom(eventRoomId) : null;
|
||||||
if (!room) return;
|
if (!room) {
|
||||||
|
console.log(`ℹ️ [debug] got m.room.message for unknown room: ${eventRoomId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const membership = room.getMyMembership();
|
const membership = room.getMyMembership();
|
||||||
if (membership !== 'join') return;
|
if (membership !== 'join') {
|
||||||
|
console.log(`ℹ️ [debug] [${room.roomId}] m.room.message while membership="${membership}" — not replying`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const content = event.getContent();
|
const content = event.getContent();
|
||||||
if (!content || typeof content.body !== 'string') return;
|
if (!content || typeof content.body !== 'string') {
|
||||||
|
console.log(`ℹ️ [debug] [${room.roomId}] m.room.message without text body`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const lower = content.body.toLowerCase().trim();
|
const lower = content.body.toLowerCase().trim();
|
||||||
if (lower === cmd || lower.startsWith(cmd + ' ')) {
|
const matches = lower === cmd || lower.startsWith(cmd + ' ');
|
||||||
|
console.log(`💬 [${room.roomId}] <${sender}> "${content.body}" (listening for "${cmd}", match=${matches})`);
|
||||||
|
if (!matches) return;
|
||||||
|
|
||||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||||
client.sendTextMessage(room.roomId, quote)
|
client.sendTextMessage(room.roomId, quote)
|
||||||
.then(() => console.log(`🎃 [${room.roomId}] ${quote}`))
|
.then(() => console.log(`🎃 [${room.roomId}] ${quote}`))
|
||||||
.catch((err) => console.error('Failed to send quote:', err.message));
|
.catch((err) => console.error('Failed to send quote:', err.message));
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('🤖 Bot is running!');
|
console.log('🤖 Bot is running!');
|
||||||
|
|||||||
Reference in New Issue
Block a user