From ecaf55411957f985c1ee288a8a6e37393196cd57 Mon Sep 17 00:00:00 2001 From: odilitime Date: Sat, 28 Dec 2024 02:10:50 +0000 Subject: [PATCH] prefer this.client.twitterConfig over getSettings --- packages/client-twitter/src/post.ts | 60 +++++++++-------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/packages/client-twitter/src/post.ts b/packages/client-twitter/src/post.ts index cd7b8b9d6e..41466c5ba3 100644 --- a/packages/client-twitter/src/post.ts +++ b/packages/client-twitter/src/post.ts @@ -6,7 +6,6 @@ import { IAgentRuntime, ModelClass, stringToUuid, - parseBooleanFromText, UUID, } from "@elizaos/core"; import { elizaLogger } from "@elizaos/core"; @@ -106,10 +105,8 @@ export class TwitterPostClient { constructor(client: ClientBase, runtime: IAgentRuntime) { this.client = client; this.runtime = runtime; - this.twitterUsername = runtime.getSetting("TWITTER_USERNAME"); - this.isDryRun = parseBooleanFromText( - runtime.getSetting("TWITTER_DRY_RUN") ?? "false" - ); + this.twitterUsername = this.client.twitterConfig.TWITTER_USERNAME; + this.isDryRun = this.client.twitterConfig.TWITTER_DRY_RUN // Log configuration on initialization elizaLogger.log("Twitter Client Configuration:"); @@ -118,34 +115,34 @@ export class TwitterPostClient { `- Dry Run Mode: ${this.isDryRun ? "enabled" : "disabled"}` ); elizaLogger.log( - `- Post Interval: ${runtime.getSetting("POST_INTERVAL_MIN") || "90"}-${runtime.getSetting("POST_INTERVAL_MAX") || "180"} minutes` + `- Post Interval: ${this.client.twitterConfig.POST_INTERVAL_MIN}-${this.client.twitterConfig.POST_INTERVAL_MAX} minutes` ); elizaLogger.log( - `- Action Processing: ${parseBooleanFromText(runtime.getSetting("ENABLE_ACTION_PROCESSING") ?? "false") ? "enabled" : "disabled"}` + `- Action Processing: ${this.client.twitterConfig.ENABLE_ACTION_PROCESSING ? "enabled" : "disabled"}` ); elizaLogger.log( - `- Action Interval: ${(parseInt(runtime.getSetting("ACTION_INTERVAL") ?? "300000") / 1000).toFixed(0)} seconds` + `- Action Interval: ${this.client.twitterConfig.ACTION_INTERVAL} seconds` ); elizaLogger.log( - `- Post Immediately: ${parseBooleanFromText(runtime.getSetting("POST_IMMEDIATELY") ?? "false") ? "enabled" : "disabled"}` + `- Post Immediately: ${this.client.twitterConfig.POST_IMMEDIATELY ? "enabled" : "disabled"}` ); elizaLogger.log( - `- Search Enabled: ${parseBooleanFromText(runtime.getSetting("TWITTER_SEARCH_ENABLE") ?? "false") ? "enabled" : "disabled"}` + `- Search Enabled: ${this.client.twitterConfig.TWITTER_SEARCH_ENABLE ? "enabled" : "disabled"}` ); - const targetUsers = runtime.getSetting("TWITTER_TARGET_USERS"); + const targetUsers = this.client.twitterConfig.TWITTER_TARGET_USERS; if (targetUsers) { elizaLogger.log(`- Target Users: ${targetUsers}`); } if (this.isDryRun) { elizaLogger.log( - "Twitter client initialized in dry run mode - no actual tweets will be posted" + "Twitter client initialized in dry run mode - no actual tweets should be posted" ); } } - async start(postImmediately: boolean = false) { + async start() { if (!this.client.profile) { await this.client.init(); } @@ -156,10 +153,8 @@ export class TwitterPostClient { }>("twitter/" + this.twitterUsername + "/lastPost"); const lastPostTimestamp = lastPost?.timestamp ?? 0; - const minMinutes = - parseInt(this.runtime.getSetting("POST_INTERVAL_MIN")) || 90; - const maxMinutes = - parseInt(this.runtime.getSetting("POST_INTERVAL_MAX")) || 180; + const minMinutes = this.client.twitterConfig.POST_INTERVAL_MIN; + const maxMinutes = this.client.twitterConfig.POST_INTERVAL_MAX; const randomMinutes = Math.floor(Math.random() * (maxMinutes - minMinutes + 1)) + minMinutes; @@ -177,8 +172,7 @@ export class TwitterPostClient { }; const processActionsLoop = async () => { - const actionInterval = - parseInt(this.runtime.getSetting("ACTION_INTERVAL")) || 300000; // Default to 5 minutes + const actionInterval = this.client.twitterConfig.ACTION_INTERVAL; // Defaults to 5 minutes while (!this.stopProcessingActions) { try { @@ -190,7 +184,7 @@ export class TwitterPostClient { ); // Wait for the full interval before next processing await new Promise((resolve) => - setTimeout(resolve, actionInterval) + setTimeout(resolve, actionInterval * 60 * 1000) // now in minutes ); } } catch (error) { @@ -204,16 +198,7 @@ export class TwitterPostClient { } }; - if ( - this.runtime.getSetting("POST_IMMEDIATELY") != null && - this.runtime.getSetting("POST_IMMEDIATELY") !== "" - ) { - // Retrieve setting, default to false if not set or if the value is not "true" - postImmediately = - this.runtime.getSetting("POST_IMMEDIATELY") === "true" || false; - } - - if (postImmediately) { + if (this.client.twitterConfig.POST_IMMEDIATELY) { await this.generateNewTweet(); } @@ -225,12 +210,7 @@ export class TwitterPostClient { elizaLogger.log("Tweet generation loop disabled (dry run mode)"); } - // Add check for ENABLE_ACTION_PROCESSING before starting the loop - const enableActionProcessing = parseBooleanFromText( - this.runtime.getSetting("ENABLE_ACTION_PROCESSING") ?? "false" - ); - - if (enableActionProcessing && !this.isDryRun) { + if (this.client.twitterConfig.ENABLE_ACTION_PROCESSING && !this.isDryRun) { processActionsLoop().catch((error) => { elizaLogger.error( "Fatal error in process actions loop:", @@ -333,8 +313,7 @@ export class TwitterPostClient { // Note Tweet failed due to authorization. Falling back to standard Tweet. const truncateContent = truncateToCompleteSentence( content, - parseInt(runtime.getSetting("MAX_TWEET_LENGTH")) || - DEFAULT_MAX_TWEET_LENGTH + this.client.twitterConfig.MAX_TWEET_LENGTH ); return await this.sendStandardTweet( client, @@ -496,10 +475,7 @@ export class TwitterPostClient { } // Truncate the content to the maximum tweet length specified in the environment settings, ensuring the truncation respects sentence boundaries. - const maxTweetLength = parseInt( - this.runtime.getSetting("MAX_TWEET_LENGTH"), - 10 - ); + const maxTweetLength = this.client.twitterConfig.MAX_TWEET_LENGTH if (maxTweetLength) { cleanedContent = truncateToCompleteSentence( cleanedContent,