Skip to content

Commit 1bf7f50

Browse files
working
1 parent 6dcc6be commit 1bf7f50

File tree

3 files changed

+348
-140
lines changed

3 files changed

+348
-140
lines changed

characters/neuronlink.character.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "NeuronLink",
33
"plugins": [],
4-
"clients": ["discord","telegram"],
4+
"clients": ["discord"],
55
"modelProvider": "ollama",
66
"settings": {
77
"secrets": {},

packages/client-discord/src/discordUserClient.ts

+92-26
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@ export class DiscordUserClient {
1212
elizaLogger.log('📱 Constructing new DiscordUserClient...');
1313
this.runtime = runtime;
1414

15-
const allowedChannelsStr = runtime.getSetting('DISCORD_ALLOWED_CHANNELS');
1615
const token = runtime.getSetting('DISCORD_USER_TOKEN');
1716

18-
elizaLogger.log('Config:', { allowedChannels: allowedChannelsStr });
17+
elizaLogger.log('Config:', { token });
1918

2019
if (!token) {
2120
throw new Error('DISCORD_USER_TOKEN must be set in environment');
2221
}
2322

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+
3027
elizaLogger.log('Initialized allowed channels:', Array.from(this.allowedChannels));
3128
if (this.allowedChannels.size === 0) {
3229
elizaLogger.warn('⚠️ No allowed channels specified - bot will not respond to any messages');
@@ -65,20 +62,75 @@ export class DiscordUserClient {
6562
elizaLogger.log('✅ DiscordUserClient constructor completed');
6663
}
6764

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+
6879
async start(): Promise<void> {
6980
try {
7081
elizaLogger.log('Starting Discord client...');
71-
82+
7283
// Set up event handlers before login
7384
this.setupEventHandlers();
74-
85+
7586
// Get token without quotes if present
7687
const token = this.runtime.getSetting('DISCORD_USER_TOKEN')?.replace(/['"]/g, '');
7788
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+
79131
// Login to Discord
80132
await this.client.login(token);
81-
133+
82134
} catch (error) {
83135
elizaLogger.error('Failed to start Discord client:', error);
84136
throw error;
@@ -108,25 +160,13 @@ export class DiscordUserClient {
108160
}
109161

110162
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-
123163
// Message event
124164
this.client.on('messageCreate', async (message) => {
125165
if (message.author.id === this.client.user?.id) return; // Ignore own messages
126166
if (!message.content) return; // Ignore messages without content
127167

128168
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());
130170

131171
elizaLogger.log(`Channel check - Name: ${channelName}, Allowed: ${isAllowed}`);
132172

@@ -179,7 +219,33 @@ export class DiscordUserClient {
179219
throw new Error(`Could not find channel with ID: ${channelId}`);
180220
}
181221

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+
}
183249
}
184250

185251
async setTyping(channelId: string, options: { typing?: boolean } = {}): Promise<void> {

0 commit comments

Comments
 (0)