Botkit: random crashes on "Cannot set property 'channel' of undefined"

Created on 1 Apr 2016  路  11Comments  路  Source: howdyai/botkit

node_modules/botkit/lib/Slackbot_worker.js:468
        msg.channel = src.channel;
                    ^

TypeError: Cannot set property 'channel' of undefined

Sometimes my bot will randomly crash with the error above.

The code is really simple:

controller.hears(
    ['joke', 'humor'],
    ['direct_message', 'direct_mention' , 'mention', 'ambient'],
    function(bot, message) {
        require('./modules/joking')(bot, message)
    });

Where joking/index.js has:

'use strict';

module.exports = function(bot, message) {
    bot.reply(message, "I work to buy a car to go to work.");
}

As you can see the message is being properly passed around.

Using:

  • botkit: 0.0.15
  • node: 5.0.0
  • OSX El Capitan 10.11.2 (though this script will also run in a linux box)

Most helpful comment

I intended to close it.

The response is taken from an array of phrases with a random index. The logic for the random index has to be the issue then :)

Thanks for the help!

All 11 comments

There must be a place in your code where you're calling reply() with resp == undefined. It's the only way for this error to occur.

Ok, I'll take a look. Thanks!

@marcobarbosa Did you intend to close this issue? Or did you find the problem/is this issue now resolved?

I intended to close it.

The response is taken from an array of phrases with a random index. The logic for the random index has to be the issue then :)

Thanks for the help!

Sure thing! Glad I could help. Gotta keep an eye out for those off-by-one errors... ;-)

Hey @dossy , decided to pick up where I left and the bot still crashes.

Take this full example:

// modules/laughing.js
'use strict';

module.exports = function(bot, message) {
    var randomInt = require('./../utils/randomInt.js');
    var replies = [
        'hahaha',
        'haha',
        'LOL',
        ':joy:',
        ':laughing:',
        ':grin:'
    ];
    bot.reply(message, replies[ randomInt(0, replies.length - 1) ]);
}
// randomInt.js
'use strict';

module.exports = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
// index.js
// (...)
controller.hears(
    ['haha', 'hehe', 'lol'],
    ['ambient'],
    function(bot,message) {
        require('./modules/laughing')(bot, message)
    });

It will crash with the same error as I mentioned before. The response is not undefined?

Um, your randomInt() function doesn't do what you probably think it does. Given a randomInt(0, n), it can return values from 0 to n. For example, imagine if n = 10:

Math.floor(0.999999 * (10 - 0 + 1)) + 0 = Math.floor(0.999999 * 11) = 10.

In which case, replies[n] will result in undefined as JavaScript arrays are zero-indexed, e.g., only contains elements from 0 to n - 1 where n is the length of the array.

And, as I said, the error you're getting can _only_ occur if you're invoking reply() with an undefined message.

Hi Dossy,

I appreciate your thorough reply. Thanks for that.

If you look at bot.reply you can see that I'm decrementing n by one to account for the zero-indexed array:

bot.reply(message, replies[ randomInt(0, replies.length - 1) ]);

But I think this issue should remain closed. As you said, the message must be undefined for this to occur. I'll use another random function for this and check for the message before calling reply.

Thanks again for the help.

Ah, you're right, you are calling randomInt(0, n - 1) -- and you're able to reproduce the problem with that exact code you provided, and nothing else, against the latest version of BotKit? That does sound like a bug that ought to be fixed, if it is indeed a bug?

Currently running 0.0.15 but AFAIK I'm the only one with this issue?

I'll test a few more times when I have the time and let you know if I find something else.

Meanwhile, I'll just check for the message before posting it, like:

var reply = replies[randomInt(0, replies.length - 1)];
if (reply) bot.reply(message, reply);

Thanks!

You should really update, as that version has seen many updates since the most recent! If the problem still exists let us know.

Was this page helpful?
0 / 5 - 0 ratings