不和谐向消息字符串添加逗号



我有一个命令,它将我发送的文本减去/sendchat 部分写入文本文件,当我执行/sendchat 消息时,它会添加一个额外的逗号,因此它的输出是", message" 这个逗号实际上搞砸了我试图用输出触发的东西。

我尝试用逗号为它做另一个拆分,我尝试将逗号放在主拆分中,所以"前缀 + "sendchat," ",这似乎只包含带有/sendchat 的整个命令。

if (message.content.split(" ")[0] == prefix + "sendchat") {
//sendchat command
message.delete();
var commandSplit = message.content.split(" ");
var commandSplitMSG = message.content.split(prefix + "sendchat ");
var sendChatCommand = commandSplit[0];
var sendChatMsg = commandSplitMSG;
if (message.member.roles.find("id", adminRole)) {
if (sendChatMsg == undefined) {
message.reply("Please specify a command to send!");
} else {
message.reply("Sending the command: " + sendChatMsg);
fs.appendFile("sendchat.txt", sendChatMsg, err => {
if (err) throw err;
});
}
}

它应该只发送命令而没有任何这些,我需要能够将一个字符串作为第二个参数放入,所以/sendchat "完整字符串"输出应该是"完整字符串">

看看你的代码,你似乎正在使用message.content.split(prefix + "sendchat ");.这很好,但是当您使用它时,您将直接访问它,但.split()返回一个array。举个例子,

"!sendchat hello world".split("!" + "sendchat ")
//["", "hello world"]

看看这个,似乎你正在将整个数组传递给文件,当你做["", "hello world"].toString()时,你会得到",hello world"作为输出。这解释了逗号,因为它在将数组附加到文件时将数组传递给字符串。

如果您更改

var sendChatMsg = commandSplitMSG;
//Given the example before, ["", "hello world"]

var sendChatMsg = commandSplitMSG[1];
//accessing [1] gets hello world

您应该获得所需的输出。

最新更新