For a Discord bot, I currently have a command that displays info for our DnD sessions:
The data is stored in a dndinfo.json file thats looks like this:
{"time":"**18:00 UK time**",
"date":"**14/05/20**",
"dm":"**Mannion**",
"prime":"Playing",
"smurphy":"Playing",
"calle":"Playing",
"smardon":"Playing",
"james":"Playing",
"mannion":"DMing",
"dex":"Playing",
"module":"Hoard of the Dragon Queen"}
I want users to be able to do a command, such as '!te time 17:00', which will update the time accordingly.
I currently am working with this code:
const Discord = module.require('discord.js');
const fs = require('fs');
const dndinfo = require ('../../dndinfo.json');
module.exports = {
name: 'test',
aliases: ['te'],
category: 'dnd',
description: 'Updates DnD info',
usage: '!te',
run: async (client, message, args) => {
const time = dndinfo.time;
let editMessage = message.content.slice(9);
if (message.content.toLowerCase().includes('time')) {
fs.readFile('dndinfo.json', function(err, data) {
console.log(time);
fs.writeFile('dndinfo.json', JSON.stringify(editMessage, null, 2), (err) => {
if (err) console.error;
message.channel.send ('message written');
});
});
}
},
};
When I run the command '!te time 17:00', the entire dndinfo.json file is replaced with:
"17:00"
I understand that it's because i'm using fs.writeFile but i'm not sure of how to specify only 'time' and have that updated?