diff --git a/index.js b/index.js index 1fc5b44..ca3e027 100644 --- a/index.js +++ b/index.js @@ -48,16 +48,25 @@ function loadQuotes() { console.log('⏳ Connecting to Matrix...'); await client.startClient(); console.log('✅ MatrixClient connected and authenticated\n'); - - console.log('⏳ Waiting for User Identity to resolve...'); - let attempts = 0; - while (!client.user || !client.user.userId) { - if (attempts > 30) { - throw new Error('Timeout waiting for Matrix User ID resolution (30s).'); - } - console.log(` ... Waiting for UserID (Attempt ${attempts + 1})`); - await new Promise(resolve => setTimeout(resolve, 1000)); - attempts++; + + 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.'); } console.log(`👤 Bot Identity: ${client.user.userId}`);