Nó bot de Javascript irc.js a concatenar os textos a partir do erro do ficheiro de texto

estou a criar um bot de irc usando o nó.js e a biblioteca IRC ( https://github.com/martynsmith/node-irc quando tento concatenar os textos de um ficheiro de texto, o bot do irc desliga-se e erra. Suspeito que haja uma linha invisível que estraga as coisas, mas não sei como testá-la ou livrar-me dela, se for esse o caso.

explicação do Código: Quando um usuário escreve uma mensagem no canal de irc, minha função é chamada. a minha função lê um ficheiro de texto chamado teste.txt e grava as linhas como elementos de um array chamado myArray. Eu então tento fazer o bot para imprimir um elemento desse array, usando o bot.diga comando. configuracao.canais [0] é o canal em que a mensagem foi escrita, e também onde o bot deve responder.

o bot pode imprimir myArray[0] e myArray [1] em linhas diferentes sem problemas, mas não pode concatenar nada depois.

mensagem de erro: C:\Program Ficheiros\nodejs\node_ modules\irc\lib\irc.js: 849 lançar err; ^ Erro [ERR_ UNHANDLEDED_ error]: Erro não resolvido. ([objecto]) no cliente.emit (events.js: 171: 17) no cliente. (C:\Program ficheiros\nodejs\node_ modules\irc\lib\irc.js: 643: 26) no cliente.emit (events.js: 182: 13) at iterator (C:\Program ficheiros\nodejs\node_ modules\irc\lib\irc.js: 846: 26) na estação.forEach () No Socket.handleData (C:\Program Ficheiros\nodejs\node_ modules\irc\lib\irc.js: 841: 15) No Socket.emit (events.js: 182: 13) no addChunk (_stream_ readable.js: 283: 12) no readableAddChunk (_stream_ readable.js: 260: 13) No Socket.Legivel.push (_stream_ readable.js:219:10)

Teste.o txt contém as letras A b c d em linhas diferentes.

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;

}
Author: Perry, 2018-12-09

1 answers

O problema eram alguns maus personagens invisíveis que vieram do arquivo de texto (ainda não faço ideia de quais personagens). Encontrei a resposta em outro post, eu removi todos os caracteres não-alfanuméricos de todos os elementos em myArray usando mystring.substituir (/\w / g,").

Remover caracteres não alfanuméricos do texto. Ter problemas com o carácter [\]

 0
Author: Perry, 2018-12-09 15:36:13