@@ -69,6 +69,9 @@ export class MessageManager {
69
69
private lastMarketingTimes : Map < string , number > = new Map ( ) ;
70
70
private channelMessageCounts : Map < string , number > = new Map ( ) ;
71
71
private channelTimeReductions : Map < string , number > = new Map ( ) ;
72
+ private readonly MIN_MARKETING_INTERVAL = 15 * 60 * 1000 ; // 15 minutes
73
+ private readonly MAX_MARKETING_INTERVAL = 45 * 60 * 1000 ; // 45 minutes
74
+ private readonly MAX_MARKETING_MESSAGES_PER_CHANNEL = 96 ; // Max messages per channel per day
72
75
private marketingEnabled : boolean = false ;
73
76
74
77
constructor ( runtime : IAgentRuntime , client : DiscordUserClient ) {
@@ -78,9 +81,52 @@ export class MessageManager {
78
81
}
79
82
80
83
async startMarketing ( ) : Promise < void > {
81
- elizaLogger . log ( 'Starting Discord marketing...' ) ;
82
- this . marketingEnabled = true ;
83
- await this . initializeChannelMarketing ( ) ;
84
+ try {
85
+ elizaLogger . log ( 'Starting Discord marketing...' ) ;
86
+
87
+ // Get allowed channels from settings
88
+ const allowedChannelsStr = this . runtime . getSetting ( 'DISCORD_ALLOWED_CHANNELS' ) ;
89
+ if ( allowedChannelsStr ?. trim ( ) ) {
90
+ const channels = allowedChannelsStr . split ( ',' ) . map ( name => name . trim ( ) ) ;
91
+ this . targetChannels = new Set ( channels ) ;
92
+ elizaLogger . log ( '📢 Marketing initialized for channels:' , Array . from ( this . targetChannels ) ) ;
93
+ }
94
+
95
+ this . marketingEnabled = true ;
96
+ elizaLogger . log ( '✅ Marketing started successfully' ) ;
97
+
98
+ // Start marketing for each target channel
99
+ for ( const channelName of this . targetChannels ) {
100
+ this . scheduleNextMarketingMessage ( channelName ) ;
101
+ }
102
+ } catch ( error ) {
103
+ elizaLogger . error ( '❌ Failed to start marketing:' , error ) ;
104
+ throw error ;
105
+ }
106
+ }
107
+
108
+ private scheduleNextMarketingMessage ( channelName : string ) : void {
109
+ if ( ! this . marketingEnabled ) return ;
110
+
111
+ const interval = Math . floor (
112
+ Math . random ( ) * ( this . MAX_MARKETING_INTERVAL - this . MIN_MARKETING_INTERVAL ) + this . MIN_MARKETING_INTERVAL
113
+ ) ;
114
+
115
+ setTimeout ( async ( ) => {
116
+ try {
117
+ if ( ! this . marketingEnabled ) return ;
118
+
119
+ const channel = await this . client . getChannelByName ( channelName ) ;
120
+ if ( channel && channel instanceof TextChannel ) {
121
+ await this . sendMarketingMessage ( channel ) ;
122
+ }
123
+ this . scheduleNextMarketingMessage ( channelName ) ;
124
+ } catch ( error ) {
125
+ elizaLogger . error ( 'Error in marketing message schedule:' , error ) ;
126
+ // Retry after a delay
127
+ setTimeout ( ( ) => this . scheduleNextMarketingMessage ( channelName ) , this . MIN_MARKETING_INTERVAL ) ;
128
+ }
129
+ } , interval ) ;
84
130
}
85
131
86
132
async stopMarketing ( ) : Promise < void > {
@@ -123,7 +169,7 @@ export class MessageManager {
123
169
MARKETING_CONSTANTS . BASE_WAIT_TIME - timeReduction
124
170
) ;
125
171
126
- return timeSinceLastMessage >= waitTime &&
172
+ return timeSinceLastMessage >= waitTime &&
127
173
messageCount >= MARKETING_CONSTANTS . MIN_MESSAGES_BEFORE_REPLY ;
128
174
}
129
175
@@ -165,7 +211,7 @@ export class MessageManager {
165
211
166
212
async getChatState ( message : Message ) : Promise < State > {
167
213
const channelId = message . channel . id ;
168
-
214
+
169
215
// Format character data
170
216
const character = this . runtime . character ;
171
217
const characterData = {
@@ -280,10 +326,10 @@ export class MessageManager {
280
326
}
281
327
282
328
const text = messageText . toLowerCase ( ) ;
283
-
329
+
284
330
// Check if message contains character's name or aliases
285
331
const nameMatch = text . includes ( this . runtime . character . name . toLowerCase ( ) ) ;
286
-
332
+
287
333
// Check if message matches character's topics
288
334
const topics = this . runtime . character . topics || [ ] ;
289
335
const topicMatch = topics . some ( topic => {
@@ -295,7 +341,7 @@ export class MessageManager {
295
341
296
342
// Get character's response rules
297
343
const responseRules = this . runtime . character . style ?. response_rules || [ ] ;
298
-
344
+
299
345
// Check against response patterns
300
346
const respondPatterns = responseRules
301
347
. filter ( rule => rule . toLowerCase ( ) . startsWith ( 'respond:' ) )
0 commit comments