I am struggling to update my JSON file with new data and retrieve that updated data when needed. Currently, whenever I try to access any information from the JSON file, it shows the same data as the last time I saved it manually. I have made some changes to my code for better clarity on the issue. Your help is much appreciated! Below is the code snippet without any error logs:
const Discord = require('discord.js');
const botconfig = require("./botconfig.json");
const fs = require("fs");
const bot = new Discord.Client();
bot.on("message", async message => {
let prefix = botconfig.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
console.log(message.member.id)
var playerFile = require(`./playerData/${message.member.id}.json`);
if (message.author.bot) return;
if (message.channel.type === "dm") return;
if (cmd.charAt(0) === prefix) {
if(cmd === `${prefix}fc`){
fs.exists(`./playerData/${message.member.id}.json`, function(exists) {
if(exists){
let ar = args[0];
let ninConsole = args[1];
let code = args[2];
if(ar === "add" || ar === "remove"){
if(code){
if(ar === "add"){
console.log("Add");
if(ninConsole === "switch"){
console.log("Switch " + code);
let fileContent = `{"switch": "${code}","threeDS": "${playerFile.threeDS}"}`
fs.writeFile(`./playerData/${message.member.id}.json`, fileContent, (err) => {
if (err) {
console.error(err);
return;
};
});
}
if(ninConsole === "3ds"){
let fileContent = `{"switch": "${playerFile.switch}","threeDS": "${code}"}`
fs.writeFile(`./playerData/${message.member.id}.json`, fileContent, (err) => {
if (err) {
console.error(err);
return;
};
});
}
}
if(ar === "remove"){
if(ninConsole === "switch"){
let fileContent = `{"switch": "None","threeDS": "${playerFile.threeDS}"}`
fs.writeFile(`./playerData/${message.member.id}.json`, fileContent, (err) => {
if (err) {
console.error(err);
return;
};
});
}
if(ninConsole === "3ds"){
let fileContent = `{"switch": "${playerFile.switch}","threeDS": "None"}`
fs.writeFile(`./playerData/${message.member.id}.json`, fileContent, (err) => {
if (err) {
console.error(err);
return;
};
});
}
}
}
}
}else{
return;
}
});
}