Skip to content
Advertisement

How to fix cannot read property “send” of undefined

This question might have a few duplicates, but the code there is different and none of the answers work for me. I’m new to discord.js so it might just be a silly mistake.

This is my 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(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 + ')');
});
client.on('message', function(message) {
    if (message.startsWith('^')) { //condition is false, even though message starts with ^
        if (message.startsWith('^ping')) {
            message.mentions.users.forEach((k, v) => {
                message.channel.send('Hello,' + v + '!');
            });
        } else if (message.startsWith('^dice')) {
            message.channel.send('You rolled a a' + Math.floor((Math.random() * 100) + 1) + "!");
        } else {
            message.channel.send("Command not found.");
        }
    } else {
        message.channel.send("debug") //error in this line
    }
});

I really don’t know how to fix this, so all the solutions I have tried lead to this error. Also if you see any other fixes to this code please point it out. Here is the error:

        message.channel.send("debug")
                        ^

TypeError: Cannot read property 'send' of undefined
    at DiscordClient.<anonymous> (C:UsersuserDocumentsUltraBotbot.js:32:25)
    at DiscordClient.emit (events.js:315:20)
    at DiscordClient.handleWSMessage (C:UsersuserDocumentsUltraBotnode_modulesdiscord.iolibindex.js:1854:11)
    at WebSocket.emit (events.js:315:20)
    at Receiver.ontext (C:UsersuserDocumentsUltraBotnode_moduleswslibWebSocket.js:841:10)
    at C:UsersuserDocumentsUltraBotnode_moduleswslibReceiver.js:536:18
    at Receiver.applyExtensions (C:UsersuserDocumentsUltraBotnode_moduleswslibReceiver.js:371:5)
    at C:UsersuserDocumentsUltraBotnode_moduleswslibReceiver.js:508:14
    at Receiver.flush (C:UsersuserDocumentsUltraBotnode_moduleswslibReceiver.js:347:3)
    at Receiver.finish (C:UsersuserDocumentsUltraBotnode_moduleswslibReceiver.js:541:12)

Advertisement

Answer

The problem is that you are using code for Discord.js, while using the Discord.io package. The ways that you send and receive messages is vastly different. I suggest that you switch to discord.js, as that is what you are writing the code to work for.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement