@@ -23,12 +23,12 @@ import { DiscordUserClient } from './discordUserClient';
23
23
24
24
// Constants
25
25
const MARKETING_CONSTANTS = {
26
- MIN_MARKETING_INTERVAL : 15 * 60 * 1000 , // 15 minutes
27
- MAX_MARKETING_INTERVAL : 45 * 60 * 1000 , // 45 minutes
28
- BASE_WAIT_TIME : 6 * 60 * 60 * 1000 , // 6 hours
29
- MIN_MESSAGES_BEFORE_REPLY : 20 ,
30
- TIME_REDUCTION_PER_MESSAGE : 15 * 60 * 1000 , // 15 minutes
31
- MIN_WAIT_TIME : 30 * 60 * 1000 , // 30 minutes
26
+ MIN_MARKETING_INTERVAL : 2 * 60 * 1000 , // 2 minutes
27
+ MAX_MARKETING_INTERVAL : 2 * 60 * 1000 , // 2 minutes
28
+ BASE_WAIT_TIME : 4 * 60 * 1000 , // 4 minutes
29
+ MIN_MESSAGES_BEFORE_REPLY : 2 , // Reduced for testing
30
+ TIME_REDUCTION_PER_MESSAGE : 1 * 60 * 1000 , // 1 minute
31
+ MIN_WAIT_TIME : 2 * 60 * 1000 , // 2 minutes
32
32
MAX_MARKETING_MESSAGES_PER_GROUP : 96
33
33
} ;
34
34
@@ -69,8 +69,8 @@ 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
72
+ private readonly MIN_MARKETING_INTERVAL = 2 * 60 * 1000 ; // 2 minutes
73
+ private readonly MAX_MARKETING_INTERVAL = 2 * 60 * 1000 ; // 2 minutes
74
74
private readonly MAX_MARKETING_MESSAGES_PER_CHANNEL = 96 ; // Max messages per channel per day
75
75
private marketingEnabled : boolean = false ;
76
76
@@ -108,23 +108,26 @@ export class MessageManager {
108
108
private scheduleNextMarketingMessage ( channelName : string ) : void {
109
109
if ( ! this . marketingEnabled ) return ;
110
110
111
- const interval = Math . floor (
112
- Math . random ( ) * ( this . MAX_MARKETING_INTERVAL - this . MIN_MARKETING_INTERVAL ) + this . MIN_MARKETING_INTERVAL
113
- ) ;
111
+ const interval = this . MIN_MARKETING_INTERVAL ; // Use fixed 2-minute interval for testing
112
+ elizaLogger . log ( `Scheduling next marketing message for ${ channelName } in ${ Math . floor ( interval / 1000 ) } seconds` ) ;
114
113
115
114
setTimeout ( async ( ) => {
116
115
try {
117
116
if ( ! this . marketingEnabled ) return ;
118
117
119
- const channel = await this . client . getChannelByName ( channelName ) ;
118
+ const channels = await this . client . getChannels ( ) ;
119
+ const channel = channels . find ( c => c . name === channelName ) ;
120
+
120
121
if ( channel && channel instanceof TextChannel ) {
121
122
await this . sendMarketingMessage ( channel ) ;
122
123
}
124
+
125
+ // Schedule next message immediately after sending
123
126
this . scheduleNextMarketingMessage ( channelName ) ;
124
127
} catch ( error ) {
125
128
elizaLogger . error ( 'Error in marketing message schedule:' , error ) ;
126
- // Retry after a delay
127
- setTimeout ( ( ) => this . scheduleNextMarketingMessage ( channelName ) , this . MIN_MARKETING_INTERVAL ) ;
129
+ // Retry after a short delay
130
+ setTimeout ( ( ) => this . scheduleNextMarketingMessage ( channelName ) , 5000 ) ;
128
131
}
129
132
} , interval ) ;
130
133
}
@@ -157,58 +160,73 @@ export class MessageManager {
157
160
}
158
161
159
162
private canSendMarketingMessage ( channel : TextChannel ) : boolean {
160
- const channelName = this . normalizeChannelName ( channel . name ) ;
161
- const lastMessageTime = this . lastMarketingTimes . get ( channelName ) || 0 ;
162
- const messageCount = this . channelMessageCounts . get ( channelName ) || 0 ;
163
- const timeReduction = this . channelTimeReductions . get ( channelName ) || 0 ;
164
-
163
+ const channelName = channel . name ;
165
164
const now = Date . now ( ) ;
166
- const timeSinceLastMessage = now - lastMessageTime ;
167
- const waitTime = Math . max (
168
- MARKETING_CONSTANTS . MIN_WAIT_TIME ,
169
- MARKETING_CONSTANTS . BASE_WAIT_TIME - timeReduction
170
- ) ;
171
-
172
- return timeSinceLastMessage >= waitTime &&
173
- messageCount >= MARKETING_CONSTANTS . MIN_MESSAGES_BEFORE_REPLY ;
174
- }
175
-
176
- private updateChannelActivity ( channel : TextChannel ) : void {
177
- const channelName = this . normalizeChannelName ( channel . name ) ;
178
- const messageCount = ( this . channelMessageCounts . get ( channelName ) || 0 ) + 1 ;
179
- this . channelMessageCounts . set ( channelName , messageCount ) ;
180
-
181
- const timeReduction = Math . min (
182
- messageCount * MARKETING_CONSTANTS . TIME_REDUCTION_PER_MESSAGE ,
183
- MARKETING_CONSTANTS . BASE_WAIT_TIME - MARKETING_CONSTANTS . MIN_WAIT_TIME
184
- ) ;
185
- this . channelTimeReductions . set ( channelName , timeReduction ) ;
186
- }
165
+ const lastMessageTime = this . lastMarketingTimes . get ( channelName ) || 0 ;
166
+
167
+ // For testing: Only check if 2 minutes have passed since last message
168
+ const timeOk = ( now - lastMessageTime ) >= this . MIN_MARKETING_INTERVAL ;
169
+
170
+ elizaLogger . log ( `Marketing check for ${ channelName } :` , {
171
+ timePassedSeconds : Math . floor ( ( now - lastMessageTime ) / 1000 ) ,
172
+ requiredWaitTimeSeconds : Math . floor ( this . MIN_MARKETING_INTERVAL / 1000 ) ,
173
+ canSend : timeOk
174
+ } ) ;
187
175
188
- private resetChannelCounters ( channel : TextChannel ) : void {
189
- const channelName = this . normalizeChannelName ( channel . name ) ;
190
- this . channelMessageCounts . set ( channelName , 0 ) ;
191
- this . channelTimeReductions . set ( channelName , 0 ) ;
192
- this . lastMarketingTimes . set ( channelName , Date . now ( ) ) ;
176
+ return timeOk ;
193
177
}
194
178
195
179
async sendMarketingMessage ( channel : TextChannel ) : Promise < void > {
196
180
try {
197
- const response = await generateMessageResponse ( this . runtime , {
198
- text : '' ,
199
- fromId : '' ,
181
+ elizaLogger . log ( `Attempting to send marketing message to ${ channel . name } ` ) ;
182
+
183
+ // Check if we can send a message
184
+ if ( ! this . canSendMarketingMessage ( channel ) ) {
185
+ elizaLogger . log ( `Cannot send marketing message to ${ channel . name } yet` ) ;
186
+ return ;
187
+ }
188
+
189
+ // Generate marketing message using character's marketing style
190
+ const marketingPrompt = {
191
+ text : "Generate a marketing message" ,
192
+ context : {
193
+ channelName : channel . name ,
194
+ channelTopic : channel . topic || '' ,
195
+ marketingGoal : "Engage users and promote discussion"
196
+ } ,
197
+ fromId : 'marketing' ,
200
198
timestamp : new Date ( ) . toISOString ( )
201
- } ) ;
199
+ } ;
202
200
201
+ const response = await generateMessageResponse ( this . runtime , marketingPrompt ) ;
202
+
203
203
if ( response ) {
204
- await channel . send ( response . text ) ;
204
+ elizaLogger . log ( `Sending marketing message to ${ channel . name } : ${ response } ` ) ;
205
+ await channel . send ( response ) ;
205
206
this . resetChannelCounters ( channel ) ;
207
+ } else {
208
+ elizaLogger . error ( 'Failed to generate marketing message' ) ;
206
209
}
207
210
} catch ( error ) {
208
- elizaLogger . error ( 'Error sending marketing message:' , error ) ;
211
+ elizaLogger . error ( 'Error sending marketing message:' , {
212
+ error : error instanceof Error ? error . message : String ( error ) ,
213
+ stack : error instanceof Error ? error . stack : undefined ,
214
+ channel : channel . name
215
+ } ) ;
209
216
}
210
217
}
211
218
219
+ private updateChannelActivity ( channel : TextChannel ) : void {
220
+ // Simplified version for testing
221
+ const channelName = channel . name ;
222
+ this . lastMarketingTimes . set ( channelName , Date . now ( ) ) ;
223
+ }
224
+
225
+ private resetChannelCounters ( channel : TextChannel ) : void {
226
+ const channelName = channel . name ;
227
+ this . lastMarketingTimes . set ( channelName , Date . now ( ) ) ;
228
+ }
229
+
212
230
async getChatState ( message : Message ) : Promise < State > {
213
231
const channelId = message . channel . id ;
214
232
@@ -393,57 +411,29 @@ export class MessageManager {
393
411
394
412
async handleMessage ( message : Message ) : Promise < { text : string } | null > {
395
413
try {
396
- // Skip messages from the bot itself
397
- const botId = this . client . getUserId ( ) ;
398
- if ( message . author . id === botId ) {
399
- return null ;
400
- }
401
-
402
414
elizaLogger . log ( '🔄 Starting message processing:' , {
403
415
text : message . content ,
404
416
channelId : message . channel . id ,
405
417
userId : message . author . id
406
418
} ) ;
407
419
408
- // Check if we should respond
409
- const shouldRespond = await this . shouldRespondToMessage ( message . content , message . channel . id ) ;
410
- if ( ! shouldRespond ) {
411
- // Update channel activity for marketing even if we don't respond
412
- if ( message . channel instanceof TextChannel ) {
413
- this . updateChannelActivity ( message . channel ) ;
414
- }
415
- return null ;
420
+ // Update channel activity for marketing
421
+ if ( message . channel instanceof TextChannel ) {
422
+ this . updateChannelActivity ( message . channel ) ;
416
423
}
417
424
418
- // Create memory for the message
419
- await this . createMessageMemory ( message ) ;
420
-
421
- // Prepare chat state
425
+ // Get chat state
422
426
const state = await this . getChatState ( message ) ;
423
-
424
- elizaLogger . log ( '🧠 Preparing response with context:' , {
425
- messageLength : message . content . length ,
426
- contextLength : state . context ?. length || 0 ,
427
- characterName : this . runtime . character ?. name
428
- } ) ;
429
-
430
- // Generate response using character's personality
427
+
428
+ // Generate response
431
429
const response = await this . generateResponse ( message , state ) ;
430
+
432
431
if ( ! response ) {
433
- elizaLogger . warn ( ' No response generated ') ;
432
+ elizaLogger . log ( 'ℹ️ No response to send ') ;
434
433
return null ;
435
434
}
436
435
437
- // Update chat state with the new message and response
438
- this . updateChatState ( message , response ) ;
439
-
440
- // If this is a marketing channel, reset marketing counters after response
441
- if ( message . channel instanceof TextChannel && this . marketingEnabled ) {
442
- this . resetChannelCounters ( message . channel ) ;
443
- }
444
-
445
436
return { text : response } ;
446
-
447
437
} catch ( error ) {
448
438
elizaLogger . error ( '❌ Error handling message:' , {
449
439
error : error instanceof Error ? error . message : String ( error ) ,
0 commit comments