Ad

Javascript Irc Bot Node.js Concatenating Strings From Text File Error

- 1 answer

I am creating an irc bot using node.js and the IRC library (https://github.com/martynsmith/node-irc). When I try to concatenate strings from a text file the irc bot shuts down and throws an error. I suspect there are some invisible line breaks that messes things up, but I don't know how to test it or to get rid of it if that is the case.

Explanation of the code: When a user types in a message in the irc channel, myFunction is called. myFunction reads a text file called test.txt and saves the rows as elements in an array called myArray. I then try to make the bot to print out an element of that array, using the bot.say command. config.channels[0] is the channel the message was typed in, and also where the bot should respond.

The bot can print out myArray[0] and myArray[1] on different lines without problems, but it cannot concatenate anything afterwards.

Error message:C:\Program Files\nodejs\node_modules\irc\lib\irc.js:849 throw err; ^ Error [ERR_UNHANDLED_ERROR]: Unhandled error. ([object Object]) at Client.emit (events.js:171:17) at Client. (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:643:26) at Client.emit (events.js:182:13) at iterator (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:846:26) at Array.forEach () at Socket.handleData (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:841:15) at Socket.emit (events.js:182:13) at addChunk (_stream_readable.js:283:12) at readableAddChunk (_stream_readable.js:260:13) at Socket.Readable.push (_stream_readable.js:219:10)

test.txt contains the letters a b c d on different lines.

var config = {
    channels: ["#channelname"],
    server: "se.quakenet.org",
    botName: "testbot"
};

// Get the lib
var irc = require("irc");

// Create the bot name
var bot = new irc.Client(config.server, config.botName, {
    channels: config.channels
});


// Listen for any message
bot.addListener("message", function(from, to, text, message) {

    myFunction(); //call myFunction when someone types something in the channel

});

function myFunction() {

var myArray = readTextFile('test.txt'); 

bot.say(config.channels[0],myArray[0]); //Print out the first element in myArray (works, this prints out 'a')
bot.say(config.channels[0],myArray[1]); //Print out the second element in myArray(works, this prints out 'b')
bot.say(config.channels[0],'test' + myArray[0]); //Works, prints out 'testa'
bot.say(config.channels[0],myArray[0] + 'test'); //concatenate a string afterwards (prints out 'a' and then throws an error and makes the bot disconnect from server)
bot.say(config.channels[0],myArray[0] + myArray[1]); //prints out 'a' and then throws an error and makes the bot disconnect from server


}

//function to read a text file:   
function readTextFile(file) {
    var fs = require("fs");
    var textFile = fs.readFileSync("./" + file, {"encoding": "utf-8"});
    textFileArray = textFile.split('\n');
return textFileArray;

}
Ad

Answer

The problem was some bad invisible characters that came along from the text file (still no idea what characters though). Found the answer in another post, I removed all non-alphanumeric characters from all elements in myArray by using mystring.replace(/\W/g, '').

Remove not alphanumeric characters from string. Having trouble with the [\] character

Ad
source: stackoverflow.com
Ad