【发布时间】:2019-07-23 23:33:09
【问题描述】:
我现在正在开发一个从 JSON 文件中读取响应的不和谐机器人。基本格式如下:
"matt":
{
"insults" : ["test 1",
"test 2",
"test 3",
"test 4"
]
},
我目前正在开发一个函数,该函数允许用户使用!addInsult 命令,后跟一个字符串,然后将其附加到现有数组中。
我想要的工作流程是这样的:
用户类型如下:!addInsult test 5。然后,这会将matt 下的insults 的JSON 对象修改为以下内容:
"matt":
{
"insults" : ["test 1",
"test 2",
"test 3",
"test 4",
"test 5"
]
},
这样做可以让我的朋友向我的机器人添加数据,而无需我在每次需要新内容时手动编辑 JSON。
最好的解决方法是什么?我研究过这个叫做 push 的东西,但我真的不明白它是如何工作的。
这是我目前所拥有的。我认为我的方向是正确的,但我不确定:
在脚本的开头建立以下内容:
// contains the insults
var insults = require('./insults.json');
// get the insults from the json file specific to user
var insultsString = JSON.stringify(insults);
var json = JSON.parse(insultsString);
这是将要进行附加的函数:
// command that allows users to add to the pool of insults
function addInsultCommand(args, receivedMessage)
{
// create an object that contains the information for the json file
json["bot"].push(["test"]);
receivedMessage.channel.send(json.matt.insults[0]);
}
【问题讨论】:
标签: javascript json discord