bot.addListener('message', function (from, channel, message) {
IRClog.write("[" + (new Date()).toJSON() + "] [" + channel + "] <" + from + ">" + message + "\n");
// ============= PLAYER COMMANDS ============= //
if (logMessages){
util.log("[" + channel + "] <" + from + ">" + message);
}
console.log(message)// the message is logged
bot.whois(from,function(WHOIS){
if(typeof WHOIS.account == 'undefined'){
var isAuthed = false;
} else {
var isAuthed = true;
}
if (message.indexOf("!") === 0){//now the message is undefined
...
如代码中所述,var消息是一个字符串,然后,我不知道为什么,它变成一个未定义的变量。为什么会这样?我没有将它分配给另一个值。
答案 0 :(得分:2)
根据执行上下文,function
执行的bot.whois
可能没有在范围中定义message
。您可以通过传递message
。
(function (msg) {
console.log(msg)// the message is logged
bot.whois(from, function(WHOIS){
var isAuthed = typeof WHOIS.account !== 'undefined';
if (msg.indexOf("!") === 0) {
...
}
})(message);
答案 1 :(得分:0)
您的代码显然是不完整的,并且实际的错误可能位于您的截止点以下某处,因为您提出了问题:
bot.addListener('message', function (from, channel, message) {
IRClog.write("[" + (new Date()).toJSON() + "] [" + channel + "] <" + from + ">" + message + "\n");
// ============= PLAYER COMMANDS ============= //
if (logMessages){
util.log("[" + channel + "] <" + from + ">" + message);
}
console.log(message)// the message is logged
bot.whois(from,function(WHOIS){
if(typeof WHOIS.account == 'undefined'){
var isAuthed = false;
} else {
var isAuthed = true;
}
if (message.indexOf("!") === 0){//now the message is undefined
...
}); // end of the whois block, which is asynchronous
/* somewhere down here, message probably gets set to undefined
like this:
message = undefined; // this would run before bot.whois(from, cb); finishes
*/
}); // end of addListener
您需要确保没有弄乱whois
电话下面的消息,或者您需要创建一个不会弄乱的副本,或者您需要关注@ Romoku的建议并将whois
调用包装在格式正确的闭包中(如下所示),并将消息传递到严格的本地范围:
bot.addListener('message', function (from, channel, message) {
IRClog.write("[" + (new Date()).toJSON() + "] [" + channel + "] <" + from + ">" + message + "\n");
// ============= PLAYER COMMANDS ============= //
if (logMessages){
util.log("[" + channel + "] <" + from + ">" + message);
}
console.log(message)// the message is logged
(function (msg) {
bot.whois(from,function(WHOIS){
if(typeof WHOIS.account == 'undefined'){
var isAuthed = false;
} else {
var isAuthed = true;
}
if (msg.indexOf("!") === 0){//now the message is undefined
...
}); // end of the whois block, which is asynchronous
})(message);
/* now you can do whatever you want to the message variable and bot.whois will
be able to operate on its independent, unmolested copy
*/
}); // end of addListener
请注意我的示例和@ Romoku的message
已经明确重命名(分别为msg
和m
),以明确您正在使用在不同的范围内使用不同的数据副本。