简体   繁体   English

discord.js:将存储为json文件的数组内容随机化

[英]discord.js: randomise contents of array stored as json file

I'm working on a small Discord bot to try and teach myself a bit of js, but I've run into something I can't really work out or find an answer to. 我正在开发一个小型Discord机器人,尝试自学一些js,但是遇到了我无法真正解决或找到答案的问题。

I have a command that has the bot post a quote at random from an array stored in a separate json file, and this works as intended. 我有一个命令,该命令使bot从存储在单独的json文件中的数组中随机发布一个引号,并且此命令按预期工作。

var config = require("./settings.json");
var quotes = config.quotes;

function randomQuote() {
   return quotes[Math.floor(Math.random() * quotes.length)];
};

if(message.content.toLowerCase() === prefix + "quote") {
   message.channel.send(randomQuote());
}

I'm now trying to do something similar, except rather than the array being a part of the json file, it is the json file. 现在我试图做同样的事情,除了不与阵列是JSON文件的一部分,它 JSON文件。 Essentially, the user can tell the bot to save a certain message, and it takes the message author & content and adds them as an entry to the array. 从本质上讲,用户可以告诉机器人保存特定的消息,然后接收消息的作者和内容并将其作为条目添加到数组中。 It should then do the same as the quote function, randomise the entries in the array and then print one into the chat. 然后,它应该执行与quote函数相同的操作,将数组中的条目随机化,然后将其打印到聊天室中。

var mess;

let saveMess = JSON.parse(fs.readFileSync("./saveMess.json", "utf8"));

function randomMess() {
    return saveMess[Math.floor(Math.random() * saveMess.length)];
};

if(message.content.toLowerCase().startsWith(prefix + "saveMess")) {
    mess = message.author + " told me the following:\n" + message.content;
    fs.readFile("./saveMess.json", function (err, data) {
        var json = JSON.parse(data)
        json.push(mess)
        fs.writeFile("./saveMess.json", JSON.stringify(json), (err) => {
            if (err) console.error(err)
        });
    })
}

if(message.content.toLowerCase() === prefix + "printMess") {
    message.channel.send(randomMess());
}

From my testing, I know it's being correctly stored in an array, and asking to print the entire file correctly (ie message.channel.send(saveMess) ) displays all the saved entries; 通过测试,我知道它已正确存储在数组中,并要求正确打印整个文件(即message.channel.send(saveMess) )会显示所有已保存的条目。 however, when attempting to do the same randomising function, I get the error "cannot send empty message". 但是,当尝试执行相同的随机化功能时,出现错误“无法发送空消息”。 Clearly, there's something I'm missing that sets an array contained within a json file apart from an array that is a json file; 显然,我缺少一些东西来设置包含在json文件中的数组,而不是设置为json文件的数组。 does anyone have any ideas on how I can get this working as intended? 有谁对我如何使它按预期工作有任何想法?

For clarification, here's how the two json files look, to demonstrate the difference I'm talking about: 为了澄清起见,这是两个json文件的外观,以说明我在说的区别:

//settings.json - the quote file

{ "token" : "BotTokenHere",
    "prefix" : "|",
    "quotes" : [
        "Quote 1",
        "Quote 2",
        "Quote 3"
    ]
}

//savemess.json - the saved messages file

[
    "<@user1> told me the following:\n|saveMess test",
    "<@user2> told me the following:\n|saveMess test2",
    "<@user3> told me the following:\n|saveMess test3"
]

According to the JSON guidelines at json.org , a json file must be an object, it cannot be an array. 根据json.org上的JSON准则,json文件必须是一个对象,不能是数组。 It is possible that JSON.parse is not throwing an error, however returning an empty array/object. JSON.parse可能没有引发错误,但是返回了一个空数组/对象。 You should stick with using the original settings.json . 您应该坚持使用原始settings.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM