Home » Javascript » Discord bot showing offline, ReferenceError in bot.js

Discord bot showing offline, ReferenceError in bot.js

Posted by: admin August 19, 2018 Leave a comment

Questions:

Edit: I forgot to put the source for the code I used, here it is: https://medium.com/davao-js/tutorial-creating-a-simple-discord-bot-9465a2764dc0

I have been attempting to program a discord bot, and have run into a problem with the main file bot.js, here is the code:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});
bot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
    // Our bot needs to know if it will execute a command
    // It will listen for messages that will start with `!`
    if (message.substring(0, 1) == '!') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {
            // !ping
            case 'ping':
                bot.sendMessage({
                    to: channelID,
                    message: 'Pong!'
                });
            break;
            // Just add any case commands if you want to..
         }
     }
});

I tried running this by navigating to the directory in which I saved the file, and typing “node bot.js” in console. The program seemed to start fine, but stopped almost immediately, so I added this code to the bottom:

setInterval(function() {
    console.log("timer that keeps nodejs processing running");
},1000*60*60);

This keeps the program running, however in the discord app the bot still shows offline and is unresponsive to the !ping command. I am not sure how to troubleshoot this problem, but I ran node -p bot.js and got this output:

[eval]:1
bot.js
^

ReferenceError: bot is not defined
    at [eval]:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:652:30)
    at evalScript (bootstrap_node.js:463:27)
    at startup (bootstrap_node.js:164:9)
    at bootstrap_node.js:609:3

My package.json and auth.json files have the correct information, so I am not sure how to fix this problem, any help would be greatly appreciated!

Answers: