Node.js message.split returns 未定义
Node.js message.split returns undefined
所以我想创建一个机器人命令,当我输入
-say Something Here
它 returns someone says: Something here
但它所做的只是 returns someone says: undefined
顺便说一句,我正在使用 tmi.js
bot.on("chat", function (channel, user, message, self) {
if(message === "-say")
var code = message.split(' ')[1];
bot.action("stankotomic", "someone says: " + code);
});
如果你采取以下行:
var code = message.split(' ')[1];
...并将其更改为以下内容,它应该有所帮助:
var code = message.split(' ')[1];
你有两个空格作为 split()
的分隔符参数,而你应该只有一个。
的文档,请参阅此处
我不是很确定,但我想你的意思是别的。如果我误会了,请告诉我。但据我了解你的问题,这是正确的方法。
正在考虑该消息 =“-在这里说点什么”;
你的结果应该是:"Someone says: Something Here"
让我们逐行查看您的代码:
if(message === "-say") // I am 100% sure "-say" and
//"-say something here" are different. correct me if i am wrong.
//so we need to check for the first word, or first element in our array of words.
//lets first create array than check: if(message.split(" ")[0] == "-say")
var code = message.split(' ')[1]; //now, we don't have 2 spaces together
//anywhere in our message, so array == nothing.
//I guess it should be more like: message.split(" ").shift().join(" ");
// this will return you: "Something Here".
bot.action("stankotomic", "someone says: " + code);
您的最终代码:
bot.on("chat", function (channel, user, message, self) {
if(message.split(" ")[0] == "-say")
var code = message.split(" ").shift().join(" ");
bot.action("stankotomic", "someone says: " + code);
});
PS:
Split 文档。
Join 文档。
Shift 文档。
所以我想创建一个机器人命令,当我输入
-say Something Here
它 returns someone says: Something here
但它所做的只是 returns someone says: undefined
顺便说一句,我正在使用 tmi.js
bot.on("chat", function (channel, user, message, self) {
if(message === "-say")
var code = message.split(' ')[1];
bot.action("stankotomic", "someone says: " + code);
});
如果你采取以下行:
var code = message.split(' ')[1];
...并将其更改为以下内容,它应该有所帮助:
var code = message.split(' ')[1];
你有两个空格作为 split()
的分隔符参数,而你应该只有一个。
我不是很确定,但我想你的意思是别的。如果我误会了,请告诉我。但据我了解你的问题,这是正确的方法。
正在考虑该消息 =“-在这里说点什么”; 你的结果应该是:"Someone says: Something Here"
让我们逐行查看您的代码:
if(message === "-say") // I am 100% sure "-say" and
//"-say something here" are different. correct me if i am wrong.
//so we need to check for the first word, or first element in our array of words.
//lets first create array than check: if(message.split(" ")[0] == "-say")
var code = message.split(' ')[1]; //now, we don't have 2 spaces together
//anywhere in our message, so array == nothing.
//I guess it should be more like: message.split(" ").shift().join(" ");
// this will return you: "Something Here".
bot.action("stankotomic", "someone says: " + code);
您的最终代码:
bot.on("chat", function (channel, user, message, self) {
if(message.split(" ")[0] == "-say")
var code = message.split(" ").shift().join(" ");
bot.action("stankotomic", "someone says: " + code);
});
PS:
Split 文档。
Join 文档。
Shift 文档。