I am using node.js, and i want to add the arguments from a command and the server id as a key:value pair into a json file like this:
{
"guildid": "args",
"guildid2": "args2",
}
And the current code that i have is far from what i want, where this code:
const command = args.shift().toLowerCase();
const args = message.content.slice(config.prefix.length).trim().split(' ');
if (command === 'setup') {
const guildid = message.guild.id
const data = {
[guildid]: `${args}`
}
const groupname = JSON.stringify(data, null, 2);
fs.writeFile('./groups.json', groupname,{flags: "a"}, finished);
function finished(err) {
message.channel.send(`Success! Your group, **${args}**, has been registered to **${message.guild.name}**`
)}
Outputs what i want to the json file, but if i run the command again it just appends to the end:
{
"guildid": "args"
}{
"guildid2": "args2"
}
I understand now that using the a flag just appends to the end no matter what and const data is what is giving it the brackets, but i want to know how to be able to format it in the way i showed at the beginning. Apologies for any glaring errors i have made, this is one of my first times using javascript and node.js.