0

While just starting out coding my bot, I suddenly receive the error Cannot read property 'id' of undefined whenever I private message the bot. The code and error stack are below.

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');

// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new 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..
         }
     }
});
TypeError: Cannot read property 'id' of undefined

    at new Channel (C:\Users\PC\Desktop\Discord-Bot\node_modules\discord.io\lib\index.js:2529:25)

    at DiscordClient.handleWSMessage (C:\Users\PC\Desktop\Discord-Bot\node_modules\discord.io\lib\index.js:1889:33)

    at WebSocket.emit (events.js:189:13)

    at Receiver.ontext (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\WebSocket.js:841:10)

    at C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:536:18

    at Receiver.applyExtensions (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:371:5)

    at C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:508:14

    at Receiver.flush (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:347:3)

    at Receiver.finish (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:541:12)

    at Receiver.expectHandler (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:499:31)```
slothiful
  • 5,548
  • 3
  • 12
  • 36
Xattics
  • 137
  • 1
  • 3
  • 14

1 Answers1

0

I dont really have anything to do with discord.io, but looking at your error, it seems, that

var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});

assigns undefined to your var bot. At least thats what your error message tells you.

Looking at the package's example code from from https://www.npmjs.com/package/discord.io

var Discord = require('discord.io');

var bot = new Discord.Client({
    token: "",
    autorun: true
});

bot.on('ready', function() {
    console.log('Logged in as %s - %s\n', bot.username, bot.id);
});

bot.on('message', function(user, userID, channelID, message, event) {
    if (message === "ping") {
        bot.sendMessage({
            to: channelID,
            message: "pong"
        });
    }
});

...I cant really find anything that you would've done wrong... so my guess is, that

var Discord = require('discord.io');

is getting the wrong path to discord.io

DigitalJedi
  • 1,577
  • 1
  • 10
  • 29
  • Thanks for trying to help, about the first thing, if i delete that line of text, it will say i dont have a bot, About the Discord.io fact, maybe i should link the location instead of just 'Discord.io'? – Xattics May 15 '19 at 19:08
  • @Xattics - yes i would suggest searching the location of your discord.io module. https://stackoverflow.com/questions/9901082/what-is-this-javascript-require the answer on this question is a very good explanation on what require does, and on how to use it - hopefully it will help you - unfortunately I have never done anything with discord.io so Im not really qualified to deliver any details - just a guess on what probably is going wrong... – DigitalJedi May 15 '19 at 19:29
  • Thanks for helping me out, just started off so alot of help is needed – Xattics May 16 '19 at 06:18