gpt4 book ai didi

Node.js message.split 返回未定义

转载 作者:太空宇宙 更新时间:2023-11-04 01:56:05 25 4
gpt4 key购买 nike

所以我想创建一个机器人命令,当我输入时-say Something Here 它返回 someone said: Something here 但它所做的只是返回 someone said: 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文档。

关于Node.js message.split 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47952362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com