diff --git a/packages/client-twitter/src/base.ts b/packages/client-twitter/src/base.ts index fd469a4dbd..6d09e4a48d 100644 --- a/packages/client-twitter/src/base.ts +++ b/packages/client-twitter/src/base.ts @@ -174,25 +174,36 @@ export class ClientBase extends EventEmitter { } elizaLogger.log("Waiting for Twitter login"); - while (true) { - await this.twitterClient.login( - username, - this.runtime.getSetting("TWITTER_PASSWORD"), - this.runtime.getSetting("TWITTER_EMAIL"), - this.runtime.getSetting("TWITTER_2FA_SECRET") || undefined - ); - - if (await this.twitterClient.isLoggedIn()) { - const cookies = await this.twitterClient.getCookies(); + let retries = 5; // Optional: Set a retry limit + while (retries > 0) { + const cookies = await this.twitterClient.getCookies(); + if (await this.twitterClient.isLoggedIn() || !!cookies) { await this.cacheCookies(username, cookies); + elizaLogger.info("Successfully logged in and cookies cached."); break; } - elizaLogger.error("Failed to login to Twitter trying again..."); + try { + await this.twitterClient.login( + username, + this.runtime.getSetting("TWITTER_PASSWORD"), + this.runtime.getSetting("TWITTER_EMAIL"), + this.runtime.getSetting("TWITTER_2FA_SECRET") || undefined + ); + } catch (error) { + elizaLogger.error(`Login attempt failed: ${error.message}`); + } + + retries--; + elizaLogger.error(`Failed to login to Twitter. Retrying... (${retries} attempts left)`); + + if (retries === 0) { + elizaLogger.error("Max retries reached. Exiting login process."); + throw new Error("Twitter login failed after maximum retries."); + } await new Promise((resolve) => setTimeout(resolve, 2000)); } - // Initialize Twitter profile this.profile = await this.fetchProfile(username);