@@ -12,21 +12,18 @@ export class DiscordUserClient {
12
12
elizaLogger . log ( '📱 Constructing new DiscordUserClient...' ) ;
13
13
this . runtime = runtime ;
14
14
15
- const allowedChannelsStr = runtime . getSetting ( 'DISCORD_ALLOWED_CHANNELS' ) ;
16
15
const token = runtime . getSetting ( 'DISCORD_USER_TOKEN' ) ;
17
16
18
- elizaLogger . log ( 'Config:' , { allowedChannels : allowedChannelsStr } ) ;
17
+ elizaLogger . log ( 'Config:' , { token } ) ;
19
18
20
19
if ( ! token ) {
21
20
throw new Error ( 'DISCORD_USER_TOKEN must be set in environment' ) ;
22
21
}
23
22
24
- // Initialize allowed channels from names - empty string or undefined means no channels allowed
25
- this . allowedChannels = new Set (
26
- allowedChannelsStr ?. trim ( )
27
- ? allowedChannelsStr . split ( ',' ) . map ( name => name . trim ( ) )
28
- : [ ]
29
- ) ;
23
+ this . allowedChannels = new Set ( ) ;
24
+
25
+ this . initializeAllowedChannels ( ) ;
26
+
30
27
elizaLogger . log ( 'Initialized allowed channels:' , Array . from ( this . allowedChannels ) ) ;
31
28
if ( this . allowedChannels . size === 0 ) {
32
29
elizaLogger . warn ( '⚠️ No allowed channels specified - bot will not respond to any messages' ) ;
@@ -65,20 +62,75 @@ export class DiscordUserClient {
65
62
elizaLogger . log ( '✅ DiscordUserClient constructor completed' ) ;
66
63
}
67
64
65
+ private initializeAllowedChannels ( ) : void {
66
+ const allowedChannelsStr = this . runtime . getSetting ( 'DISCORD_ALLOWED_CHANNELS' ) ;
67
+ if ( allowedChannelsStr ) {
68
+ this . allowedChannels = new Set (
69
+ allowedChannelsStr . split ( ',' )
70
+ . map ( c => c . trim ( ) . toLowerCase ( ) )
71
+ . filter ( c => c ) // Remove empty strings
72
+ ) ;
73
+ elizaLogger . log ( 'Initialized allowed channels:' , Array . from ( this . allowedChannels ) ) ;
74
+ } else {
75
+ elizaLogger . warn ( 'No allowed channels specified in DISCORD_ALLOWED_CHANNELS' ) ;
76
+ }
77
+ }
78
+
68
79
async start ( ) : Promise < void > {
69
80
try {
70
81
elizaLogger . log ( 'Starting Discord client...' ) ;
71
-
82
+
72
83
// Set up event handlers before login
73
84
this . setupEventHandlers ( ) ;
74
-
85
+
75
86
// Get token without quotes if present
76
87
const token = this . runtime . getSetting ( 'DISCORD_USER_TOKEN' ) ?. replace ( / [ ' " ] / g, '' ) ;
77
88
elizaLogger . log ( 'Attempting to login...' ) ;
78
-
89
+
90
+ // Add ready event handler
91
+ this . client . on ( 'ready' , async ( ) => {
92
+ elizaLogger . log ( 'Discord client ready!' , {
93
+ username : this . client . user ?. username ,
94
+ id : this . client . user ?. id ,
95
+ isBot : this . client . user ?. bot ,
96
+ isConnected : this . client . isReady ( )
97
+ } ) ;
98
+
99
+ // Log all available channels
100
+ const channels = await this . getChannels ( ) ;
101
+ elizaLogger . log ( 'Available channels:' , channels . map ( c => ( {
102
+ name : c . name ,
103
+ id : c . id ,
104
+ type : c . type ,
105
+ permissions : {
106
+ sendMessages : c . permissionsFor ( this . client . user ! ) ?. has ( 'SEND_MESSAGES' ) ,
107
+ viewChannel : c . permissionsFor ( this . client . user ! ) ?. has ( 'VIEW_CHANNEL' )
108
+ }
109
+ } ) ) ) ;
110
+
111
+ // Start marketing after client is ready
112
+ try {
113
+ elizaLogger . log ( 'Starting marketing functionality...' ) ;
114
+ await this . messageManager . startMarketing ( ) ;
115
+ elizaLogger . log ( 'Marketing functionality initialized' ) ;
116
+ } catch ( error ) {
117
+ elizaLogger . error ( 'Failed to start marketing:' , error ) ;
118
+ }
119
+ } ) ;
120
+
121
+ // Add error event handler
122
+ this . client . on ( 'error' , ( error ) => {
123
+ elizaLogger . error ( 'Discord client error:' , error ) ;
124
+ } ) ;
125
+
126
+ // Add debug event handler
127
+ this . client . on ( 'debug' , ( message ) => {
128
+ elizaLogger . log ( 'Discord debug:' , message ) ;
129
+ } ) ;
130
+
79
131
// Login to Discord
80
132
await this . client . login ( token ) ;
81
-
133
+
82
134
} catch ( error ) {
83
135
elizaLogger . error ( 'Failed to start Discord client:' , error ) ;
84
136
throw error ;
@@ -108,25 +160,13 @@ export class DiscordUserClient {
108
160
}
109
161
110
162
private setupEventHandlers ( ) : void {
111
- // Ready event
112
- this . client . on ( 'ready' , async ( ) => {
113
- elizaLogger . success ( `✅ Successfully logged in as ${ this . client . user ?. username } ` ) ;
114
-
115
- // Start marketing after successful login
116
- try {
117
- await this . messageManager . startMarketing ( ) ;
118
- } catch ( error ) {
119
- elizaLogger . error ( 'Failed to start marketing:' , error ) ;
120
- }
121
- } ) ;
122
-
123
163
// Message event
124
164
this . client . on ( 'messageCreate' , async ( message ) => {
125
165
if ( message . author . id === this . client . user ?. id ) return ; // Ignore own messages
126
166
if ( ! message . content ) return ; // Ignore messages without content
127
167
128
168
const channelName = message . channel instanceof TextChannel ? message . channel . name : 'unknown' ;
129
- const isAllowed = this . allowedChannels . has ( channelName ) ;
169
+ const isAllowed = this . allowedChannels . has ( channelName . toLowerCase ( ) ) ;
130
170
131
171
elizaLogger . log ( `Channel check - Name: ${ channelName } , Allowed: ${ isAllowed } ` ) ;
132
172
@@ -179,7 +219,33 @@ export class DiscordUserClient {
179
219
throw new Error ( `Could not find channel with ID: ${ channelId } ` ) ;
180
220
}
181
221
182
- await channel . send ( options . message ) ;
222
+ elizaLogger . log ( 'Attempting to send message:' , {
223
+ channelName : channel . name ,
224
+ channelId : channel . id ,
225
+ messageLength : options . message . length ,
226
+ isClientReady : this . client . isReady ( ) ,
227
+ permissions : {
228
+ sendMessages : channel . permissionsFor ( this . client . user ! ) ?. has ( 'SEND_MESSAGES' ) ,
229
+ viewChannel : channel . permissionsFor ( this . client . user ! ) ?. has ( 'VIEW_CHANNEL' )
230
+ }
231
+ } ) ;
232
+
233
+ try {
234
+ const sent = await channel . send ( options . message ) ;
235
+ elizaLogger . log ( 'Message sent successfully:' , {
236
+ messageId : sent . id ,
237
+ channelName : channel . name ,
238
+ timestamp : sent . createdTimestamp
239
+ } ) ;
240
+ } catch ( error ) {
241
+ elizaLogger . error ( 'Failed to send message:' , {
242
+ error : error instanceof Error ? error . message : String ( error ) ,
243
+ channelId,
244
+ channelName : channel . name ,
245
+ stack : error instanceof Error ? error . stack : undefined
246
+ } ) ;
247
+ throw error ;
248
+ }
183
249
}
184
250
185
251
async setTyping ( channelId : string , options : { typing ?: boolean } = { } ) : Promise < void > {
0 commit comments