JSON wont update while running bot


#1

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!!”)
});


#2

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.


#3

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


#4

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


#5

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


#6

Franklin, also, fs corrupts JSON files!


#7

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


#8

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


#9

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?