Node-IRC Bot Issues

407 Views Asked by At

I am encountering issues with the node-irc module. I have tried this, but it doesn't seem to have the information I am looking for. Here is my issue: when I call:

new irc.Client("irc.freenode.net", "BotName", {channels: ["#bots"]});

I am met with:

/home/915Ninja/node_modules/irc/lib/irc.js:864
var channelName =  channel.split(' ')[0];
                           ^
TypeError: Cannot call method 'split' of undefined
    at Client.join (/home/915Ninja/node_modules/irc/lib/irc.js:864:32)
    at Object.<anonymous> (/home/915Ninja/irc/bot.js:8:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3`

So, my question is, what am I doing wrong?

My code:

var irc = require('irc');
var eval = require('eval');
var crypto = require('crypto');

var config = {server:"irc.freenode.net", nick:"Gh0stBot", channels:['#bots'], password:"", username:"Gh0stBot", realname:"Gh0stBot JS"};

var bot = new irc.Client(config.server, config.nick, {channels: ['#bots'], password:config.password, userName:config.username,  realName:config.realname});
bot.join(config.channel);
bot.addListener('message', function(nickname, to, text, msg){ //skipped for brevity}

Note: I named it BotName earlier to be generic.

Update: I figured it out.

I called bot.join(config.channels), which I failed to realize join() was receiving an array instead of a string.

1

There are 1 best solutions below

0
On

irc.Client.join(s) expects a string object. When I called bot.join(config.channels), I passed it an array of strings instead of just one string. Thus, it tried to split an array, where split() accepts a string, not array. So, I could either fix it with:

/* Passing channels into constructor... */
var bot = new irc.Client(config.server, config.nick, {
  channels: config.channels //The whole array
});

or

/* Recursive joining... */
var bot = new irc.Client(config.server, config.nick);
for (var i = 0; i < config.channels.length; i++){
  bot.join(config.channels[i]); //Passing in a string in the array one at a time
}