start-discord.js•3.37 kB
// Load environment variables from .env file
import { config } from 'dotenv';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { spawn } from 'child_process';
import { Client, GatewayIntentBits } from 'discord.js';
// Get the directory of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Load environment variables from .env file
config();
// Add default server and channel IDs
process.env.DEFAULT_SERVER_ID = process.env.DEFAULT_SERVER_ID || '1367327881545384027';
process.env.DEFAULT_CHANNEL_ID = process.env.DEFAULT_CHANNEL_ID || '1367343923730583562';
process.env.CLIENT_ID = process.env.CLIENT_ID || '1367324432980512899';
// Redirect console.log to stderr to avoid JSON parsing issues in Claude Desktop
const originalConsoleLog = console.log;
console.log = function() {
console.error.apply(console, arguments);
};
// Check if DISCORD_TOKEN is available
if (!process.env.DISCORD_TOKEN) {
console.error('Error: DISCORD_TOKEN not found in environment variables');
console.error('Make sure you have a .env file with DISCORD_TOKEN=your_token');
process.exit(1);
}
// Log the token length for verification
console.error(`Discord token loaded, length: ${process.env.DISCORD_TOKEN.length}`);
console.error(`Default server ID: ${process.env.DEFAULT_SERVER_ID}`);
console.error(`Default channel ID: ${process.env.DEFAULT_CHANNEL_ID}`);
console.error(`Client ID: ${process.env.CLIENT_ID}`);
// Test Discord connection directly
console.error('Testing Discord connection directly...');
const testClient = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
testClient.once('ready', async () => {
console.error(`Logged in as ${testClient.user?.tag}`);
// List all available guilds
console.error('Available guilds:');
testClient.guilds.cache.forEach(guild => {
console.error(`- ${guild.name} (${guild.id})`);
});
// Try to fetch the default guild
try {
const guild = await testClient.guilds.fetch(process.env.DEFAULT_SERVER_ID);
console.error(`Successfully fetched default guild: ${guild.name}`);
// List channels in the default guild
console.error('Channels in default guild:');
const channels = await guild.channels.fetch();
channels.forEach(channel => {
if (channel) {
console.error(`- ${channel.name} (${channel.id}) [Type: ${channel.type}]`);
}
});
} catch (error) {
console.error(`Error fetching default guild: ${error}`);
}
// Disconnect test client
testClient.destroy();
// Start the MCP-Discord server
const buildPath = join(__dirname, 'build', 'index.js');
console.error(`Starting MCP-Discord server from: ${buildPath}`);
// Import the built server
import('./build/index.js');
});
testClient.on('error', (error) => {
console.error(`Discord connection error: ${error}`);
});
// Login with the token
try {
testClient.login(process.env.DISCORD_TOKEN);
} catch (error) {
console.error(`Failed to login to Discord: ${error}`);
// Continue with MCP server anyway
const buildPath = join(__dirname, 'build', 'index.js');
console.error(`Starting MCP-Discord server from: ${buildPath}`);
// Import the built server
import('./build/index.js');
}