首页 文章

Javascript irc bot node.js连接文本文件错误的字符串

提问于
浏览
0

我正在使用node.js和IRC库(https://github.com/martynsmith/node-irc)创建一个irc机器人 . 当我尝试连接文本文件中的字符串时,irc bot会关闭并抛出错误 . 我怀疑有一些看不见的断线,但是如果是这样的话,我不知道如何测试或摆脱它 .

Explanation of the code: 当用户在irc通道中键入消息时,将调用myFunction . myFunction读取一个名为test.txt的文本文件,并将这些行保存为名为myArray的数组中的元素 . 然后我尝试使用bot.say命令让机器人打印出该数组的元素 . config.channels [0]是输入消息的通道,也是机器人应该响应的位置 .

机器人可以在不同的行上打印出myArray [0]和myArray [1]而没有任何问题,但之后无法连接任何内容 .

Error message: C:\ Program Files \ nodejs \ node_modules \ irc \ lib \ irc.js:849 throw err; ^错误[ERR_UNHANDLED_ERROR]:未处理的错误 . ([object Object])在Client的Client.emit(events.js:171:17) . (C:\ Program Files \ nodejs \ node_modules \ irc \ lib \ irc.js:643:26)在iterator上的Client.emit(events.js:182:13)处(C:\ Program Files \ nodejs \ node_modules \ irc) \ lib \ irc.js:846:26)位于Socket.mitleData(C:\ Program Files \ nodejs \ node_modules \ irc \ lib \ irc.js:841:15)的Array.forEach()处于Socket.emit(事件) .js:182:13)在addChunk(_stream_readable.js:283:12)处的readableAddChunk(_stream_readable.js:260:13)处于Socket.Readable.push(_stream_readable.js:219:10)

test.txt 包含不同行上的字母a b c d .

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;

}

1 回答

  • 0

    问题是文本文件中出现了一些不好的隐形字符(但仍然不知道是什么字符) . 在另一篇文章中找到了答案,我使用mystring.replace(/ \ W / g,'')从myArray中的所有元素中删除了所有非字母数字字符 .

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

相关问题