gpt4 book ai didi

javascript - Discord 在消息字符串中添加逗号

转载 作者:行者123 更新时间:2023-11-28 03:28:23 26 4
gpt4 key购买 nike

我有一个命令,它将我发送的文本减去/sendchat部分写入文本文件,当我执行/sendchat消息时,它会添加一个额外的逗号,因此它的输出是“,消息”这个逗号实际上弄乱了我的事情尝试用输出触发。

我尝试用逗号对其进行另一个拆分,我尝试将逗号放在主拆分中,因此“前缀+“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“full string” 输出应该是“full string”

最佳答案

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

"!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

您应该获得您正在寻找的输出。

关于javascript - Discord 在消息字符串中添加逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58422931/

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