Discord bot appears offline and throws ReferenceError in main script

I’m working on creating a Discord bot and running into issues with my main JavaScript file. When I execute the script using node main.js, it starts but then stops right away. Even when I add a timer to keep it running, the bot still shows as offline in Discord and doesn’t respond to commands.

Here’s my current code:

const Discord = require('discord.io');
const winston = require('winston');
const config = require('./config.json');

// Setup logging
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
    colorize: true
});
winston.level = 'debug';

// Create bot instance
const client = new Discord.Client({
   token: config.token,
   autorun: true
});

client.on('ready', function (event) {
    winston.info('Bot connected successfully');
    winston.info('Running as: ');
    winston.info(client.username + ' - (' + client.id + ')');
});

client.on('message', function (author, authorID, channelID, content, event) {
    if (content.substring(0, 1) == '?') {
        const parameters = content.substring(1).split(' ');
        const command = parameters[0];
        const remaining = parameters.splice(1);
        
        switch(command) {
            case 'hello':
                client.sendMessage({
                    to: channelID,
                    message: 'Hi there!'
                });
            break;
         }
     }
});

I added this timer to prevent the script from stopping:

setInterval(function() {
    console.log("keeping process alive");
}, 60000);

When I run node -p main.js to debug, I get a ReferenceError saying the client variable is not defined. My configuration files seem correct, so I’m not sure what’s causing this problem. Any suggestions would be helpful!

discord.io is outdated and breaks constantly. switch to discord.js - it’s actively maintained and actually works. also, -p isn’t for debugging. use --inspect for that.

The real issue isn’t your code structure - it’s your library choice. I hit this exact problem last year with an older bot project. The discord.io package hasn’t been updated since 2018, and Discord’s gateway requirements have changed a lot since then.

Your bot shows offline because the authentication handshake fails with Discord’s current API. That ReferenceError with node -p is normal - that flag evaluates expressions, it doesn’t run files.

The connection problem is discord.io being incompatible with current Discord infrastructure. Even if you get it connected temporarily, it’ll disconnect constantly due to protocol mismatches. The keepalive timer won’t fix the underlying auth issues.

Honestly, this is a perfect excuse to upgrade to discord.js. It handles modern Discord requirements properly and has way better error handling for debugging connection problems.

discord.io has been dead for years - Discord’s API changed way too much since then. Your bot shows offline because discord.io can’t authenticate with Discord’s current system anymore. I hit this same problem with an old bot. It’d connect for a second then immediately drop. That ReferenceError with node -p happens because that flag prints results, not runs files. Your token and event handlers look fine syntax-wise, but you’re stuck with a broken library. Even if you somehow got discord.io working, it’d break again fast since Discord constantly updates their API. The setInterval won’t fix the connection issues either. Just migrate to discord.js - it handles modern Discord auth properly and keeps connections stable without needing manual timers.

You’re using the old discord.io library - that’s your problem. It’s been dead for years and breaks with newer Node.js versions. I made this same mistake when I started and wasted hours debugging the same errors. For the ReferenceError: node -p evaluates expressions, it doesn’t run files. Use node main.js instead, or just add some console.log statements to debug. The timer won’t help - you’ve got connection issues, not timing problems. Once your bot connects properly to Discord’s gateway, it should stay alive on its own. Switch to discord.js and these weird disconnection issues will disappear. I had the exact same problems until I made the switch. The syntax is a bit different but it’s way more reliable and handles errors much better.

yeah, discord.io is so outdated now. even if you get it connected, it’ll drop because discord updated their gateway stuff. ur code looks alright, but you’re just making it harder with that old lib.