Currently, I am developing a discord bot that extracts responses from a JSON file. The structure of the JSON file is basic and looks like this:
"matt":
{
"insults" : ["test 1",
"test 2",
"test 3",
"test 4"
]
},
My current focus is on creating a function that allows users to use the !addInsult
command, followed by a string, which will then be added to the existing array.
The ideal process would go something like this:
User enters: !addInsult test 5
. This action should modify the JSON object of insults
under matt
to look like this:
"matt":
{
"insults" : ["test 1",
"test 2",
"test 3",
"test 4",
"test 5"
]
},
By implementing this feature, it will enable my friends to contribute data to my bot without requiring manual intervention to update the JSON every time we want to add something new.
What would be the most effective approach to achieving this? I have heard of something called push, but I am unclear on how it operates.
This is what I have accomplished so far. I believe I am headed in the right direction, though not entirely confident:
The code snippet below is declared at the beginning of the script:
// contains the insults
var insults = require('./insults.json');
// retrieving insults from the user-specific JSON file
var insultsString = JSON.stringify(insults);
var json = JSON.parse(insultsString);
Here is the function responsible for appending the insults:
// command allowing users to add to the insult pool
function addInsultCommand(args, receivedMessage)
{
// create an object containing information for the JSON file
json["bot"].push(["test"]);
receivedMessage.channel.send(json.matt.insults[0]);
}