JSON wont update while running bot

When I run my bot and I run a particular command, when I restart the bot it doesn’t save the json for some reason.
My code:
const commando = require(‘discord.js-commando’);
const Discord = require(“discord.js”);
const oneLine = require(‘common-tags’).oneLine;
const shopitems = require(’/app/shopitems.json’);
const coins = require(’/app/accountbalance.json’);
const inven = require(’/app/userinventory.json’);
const prize = require(’/app/items.json’);
const fs = require(‘fs’);
var removeitem = ‘Mystery Weapon Crate’;
var data = fs.readFileSync(’/app/userinventory.json’);
var json = JSON.parse(data);

function inventor() {
let item = ‘Mystery Weapon Crate’;
return item = ‘Mystery Weapon Crate’
}
module.exports = class ShopCommand extends commando.Command {
constructor(client) {
super(client, {
name: ‘open’,
aliases: [‘open’],
group: ‘economy’,
ownerOnly: true,
memberName: ‘open’,
description: ‘Open a crate you have in your inventory.’,
details: oneLine Open a crate you have in your inventory.,
examples: [’+open’],
});
}

async run(msg){
if(msg.author.bot) return;
if(!inven[msg.author.id]) {
return msg.channel.send(‘Sorry, you don’t have an inventory(try buying some crates).’);
} else {
let crat = inven[msg.author.id].items.find(inventor);
if(!crat) {
return msg.channel.send(‘Sorry, you don’t have any crates in your inventory at the moment.’);
}
/* let id = msg.member.id;
var itemss = json.id;
var inv = {‘guild’: msg.guild.id, ‘items’:[‘Mystery Weapon Crate’]};
delete inv[‘items’];
*/
let pixe = prize[‘items’][Math.floor(Math.random() * prize[‘items’].length)];
let currit = inven[msg.author.id].items
inven[msg.author.id] = {
guild: msg.guild.id,
items: [currit, pixe.name]
};
fs.writeFile("/app/userinventory.json", JSON.stringify(json), function (err) {
if (err) throw err;
console.log(“Saved!!”)
});

How are you determining the JSON isn’t being updated? The editor doesn’t yet sync with programmatic changes, so it could be that the changes are being made you just can’t see them yet by looking at the file in the editor. Refresh the editor or run refresh from the console and you should then be able to see any updates made.

When ever the bot reloads when I run a command that checks the inventory the added item isn’t there

Hello @Franklin-The-Turtle

I suggest you take full usage of the fs API by using fs.writeFile and fs.readFile instead of using require to load JSON files. The cause for this is because require caches the data into require.cache

Thanks for the help, I will try to check out the fs api

1 Like

Franklin, also, fs corrupts JSON files!

Explain more please, how on earth would you write to files without using the filesystem?

Well, he/she could check out fs docs which are pretty understandable. Also, as said fs corrupts JSON files while writing or reading them.

So you’re simply saying that MongoDB, CouchDB, DocumentDB, MarkLogic, OrientDB, RethinkDB and many more databases suck because they use FS to write and read to/from a JSON file?