Fix: fetch bot's user ID via whoami so request URLs resolve
This commit is contained in:
82
index.js
82
index.js
@@ -45,46 +45,37 @@ function loadQuotes() {
|
||||
console.log(`🏠 Server: ${server}`);
|
||||
|
||||
try {
|
||||
console.log('⏳ Connecting to Matrix...');
|
||||
await client.startClient();
|
||||
console.log('✅ MatrixClient connected and authenticated\n');
|
||||
// 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;
|
||||
|
||||
console.log('⏳ Waiting for initial sync to complete...');
|
||||
await new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error('Sync timeout: Initial sync took too long.'));
|
||||
}, 45000);
|
||||
|
||||
client.on('sync', (sync) => {
|
||||
if (sync.result === 'ok') {
|
||||
clearTimeout(timeout);
|
||||
resolve();
|
||||
} else if (sync.result === 'error') {
|
||||
reject(new Error(`Sync failed with error: ${sync.error}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (!client.user || !client.user.userId) {
|
||||
throw new Error('User identity not resolved after sync.');
|
||||
if (!userId || !userId.startsWith('@')) {
|
||||
console.error(`❌ whoami returned invalid user ID: ${userId}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`👤 Bot Identity: ${client.user.userId}`);
|
||||
console.log(`🏠 Home Server: ${client.user.homeserver}`);
|
||||
client.credentials.userId = userId;
|
||||
console.log(`👤 Bot Identity: ${userId}`);
|
||||
|
||||
await client.startClient();
|
||||
console.log('✅ MatrixClient connected and authenticated\n');
|
||||
|
||||
let lastSentMessage = null;
|
||||
|
||||
|
||||
client.on('RoomMember#joined', async (member) => {
|
||||
const room = member.room;
|
||||
console.log(`✅ Joined room: ${room.getCanonicalRoomName() || room.roomId}`);
|
||||
|
||||
console.log(`✅ Room: ${room.getCanonicalRoomName() || room.roomId}`);
|
||||
|
||||
room.on('EventMessage', async (event, stateEvent) => {
|
||||
const content = event.getContent();
|
||||
if (!content || !content.body) return;
|
||||
|
||||
|
||||
const body = content.body.toLowerCase().trim();
|
||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||
|
||||
|
||||
if (body === cmd || body.startsWith(cmd)) {
|
||||
if (!lastSentMessage || Date.now() - lastSentMessage > 300) {
|
||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||
@@ -98,40 +89,15 @@ function loadQuotes() {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
member.room.on('RoomMember#left', () => {
|
||||
console.log(`ℹ️ Left room: ${member.room.getCanonicalRoomName() || member.room.roomId}`);
|
||||
console.log(`ℹ️ Left: ${room.getCanonicalRoomName() || room.roomId}`);
|
||||
});
|
||||
});
|
||||
|
||||
client.on('EventMessage', async (event, stateEvent) => {
|
||||
const room = event.sender.room;
|
||||
const content = event.getContent();
|
||||
if (!content || !content.body) return;
|
||||
|
||||
const body = content.body.toLowerCase().trim();
|
||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||
|
||||
const roomMembers = await room.getJoinedMemberCount();
|
||||
if (roomMembers > 0 && room.getMyMembership()) {
|
||||
if (body === cmd || body.startsWith(cmd)) {
|
||||
if (!lastSentMessage || Date.now() - lastSentMessage > 300) {
|
||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||
try {
|
||||
await room.sendMessage(quote);
|
||||
console.log(`🎃 Quote: ${quote}`);
|
||||
lastSentMessage = Date.now();
|
||||
} catch (err) {
|
||||
console.error('Failed to send quote:', err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
console.log('🤖 Bot is running!');
|
||||
console.log(' Invite this bot to a Matrix room and type !quote to get an Ancestor quote.\n');
|
||||
|
||||
|
||||
} catch (err) {
|
||||
console.error('❌ Fatal error starting bot:', err.message);
|
||||
console.error(err.stack);
|
||||
@@ -144,4 +110,4 @@ function loadQuotes() {
|
||||
});
|
||||
|
||||
setInterval(() => {}, 60000);
|
||||
})();
|
||||
})();
|
||||
Reference in New Issue
Block a user