最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Node.js message.split返回undefined

运维笔记admin13浏览0评论

Node.js message.split返回undefined

Node.js message.split返回undefined

所以我想创建一个机器人命令,当我键入-say Something Here它返回someone says: Something here但它所做的只是返回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);
});
回答如下:

我不太确定,但我认为你的意思是别的。告诉我,如果我弄错了。但正如我已经理解你的问题,这是正确的方法。

考虑到这个消息=“-say 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文档。

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论