Javascript irc bot node.js 连接来自文本文件的字符串错误
Javascript irc bot node.js concatenating strings from text file error
我正在使用 node.js 和 IRC 库 (https://github.com/martynsmith/node-irc) 创建一个 irc 机器人。当我尝试连接文本文件中的字符串时,irc bot 关闭并抛出错误。我怀疑有一些看不见的换行符会把事情搞砸,但我不知道如何测试它或在这种情况下如何摆脱它。
代码解释:
当用户在 irc 频道中键入消息时,将调用 myFunction。
myFunction 读取名为 test.txt 的文本文件并将行作为元素保存在名为 myArray 的数组中。然后,我尝试使用 bot.say 命令让机器人打印出该数组的一个元素。 config.channels[0] 是输入消息的通道,也是机器人应该响应的通道。
机器人可以毫无问题地在不同的行上打印出 myArray[0] 和 myArray[1],但之后无法连接任何内容。
错误信息:
C:\程序Files\nodejs\node_modules\irc\lib\irc.js:849
抛出错误;
^
错误 [ERR_UNHANDLED_ERROR]:未处理的错误。 ([对象对象])
在 Client.emit (events.js:171:17)
在客户端。 (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:643:26)
在 Client.emit (events.js:182:13)
在迭代器 (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:846:26)
在 Array.forEach ()
在 Socket.handleData (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:841:15)
在 Socket.emit (events.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;
}
问题是文本文件中出现了一些不可见的错误字符(虽然仍然不知道是什么字符)。在另一个 post 中找到了答案,我使用 mystring.replace(/\W/g, '') 从 myArray 中的所有元素中删除了所有非字母数字字符。
Remove not alphanumeric characters from string. Having trouble with the [\] character
我正在使用 node.js 和 IRC 库 (https://github.com/martynsmith/node-irc) 创建一个 irc 机器人。当我尝试连接文本文件中的字符串时,irc bot 关闭并抛出错误。我怀疑有一些看不见的换行符会把事情搞砸,但我不知道如何测试它或在这种情况下如何摆脱它。
代码解释: 当用户在 irc 频道中键入消息时,将调用 myFunction。 myFunction 读取名为 test.txt 的文本文件并将行作为元素保存在名为 myArray 的数组中。然后,我尝试使用 bot.say 命令让机器人打印出该数组的一个元素。 config.channels[0] 是输入消息的通道,也是机器人应该响应的通道。
机器人可以毫无问题地在不同的行上打印出 myArray[0] 和 myArray[1],但之后无法连接任何内容。
错误信息: C:\程序Files\nodejs\node_modules\irc\lib\irc.js:849 抛出错误; ^ 错误 [ERR_UNHANDLED_ERROR]:未处理的错误。 ([对象对象]) 在 Client.emit (events.js:171:17) 在客户端。 (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:643:26) 在 Client.emit (events.js:182:13) 在迭代器 (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:846:26) 在 Array.forEach () 在 Socket.handleData (C:\Program Files\nodejs\node_modules\irc\lib\irc.js:841:15) 在 Socket.emit (events.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;
}
问题是文本文件中出现了一些不可见的错误字符(虽然仍然不知道是什么字符)。在另一个 post 中找到了答案,我使用 mystring.replace(/\W/g, '') 从 myArray 中的所有元素中删除了所有非字母数字字符。
Remove not alphanumeric characters from string. Having trouble with the [\] character