似乎无法追加到.json数组

时间:2019-12-02 10:03:30

标签: javascript node.js json bots discord

当前,我正在尝试为不和谐的bot添加到黑名单数组。读取数组可以正常工作,但是每当我尝试附加到bot节点时,代码1就会退出。

我尝试了几种方法,但似乎没有一种有效的方法。

const data = require('./data.json');
data.serverData.blackListedColors.append(randomRoleColor);

返回:

                        data.serverData.blackListedColors.append(randomRoleColor);
                                                          ^

TypeError: data.serverData.blackListedColors.append is not a function
    at Client.<anonymous> (C:\Users\mtutt\OneDrive\Documenten\DiscordKeyBot\bot.js:363:59)
    at Client.emit (events.js:209:13)
    at MessageCreateHandler.handle (C:\Users\mtutt\OneDrive\Documenten\DiscordKeyBot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\mtutt\OneDrive\Documenten\DiscordKeyBot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (C:\Users\mtutt\OneDrive\Documenten\DiscordKeyBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\mtutt\OneDrive\Documenten\DiscordKeyBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\mtutt\OneDrive\Documenten\DiscordKeyBot\node_modules\discord.js\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:209:13)
    at Receiver.receiverOnMessage (C:\Users\mtutt\OneDrive\Documenten\DiscordKeyBot\node_modules\discord.js\node_modules\ws\lib\websocket.js:789:20)
    at Receiver.emit (events.js:209:13)

Process finished with exit code 1

我尝试使用.parse.add,但它们似乎也不起作用。

使用.push似乎不会使其崩溃,但也不会将该字符串添加到.json文件中。

修改: 这是.json文件供参考

{
  "serverData": {
    "muffinID": "[disocrdID]",
    "token": "[token]",
    "blackListedColors": []
}

1 个答案:

答案 0 :(得分:0)

您不能使用create temporary function isstopword as 'org.dennis.udf.IsStopWord'; 来处理由于节点需要缓存而在bot进程的生存期内更改的数据。此外,仅修改require()不会自动将其持久保存到文件中;您必须将其写回到文件中。

写自己不是火箭手术,但您可能想研究一个为您服务的图书馆–我没有尝试过,但是看起来很有希望:https://www.npmjs.com/package/json-persistent-object