2

I'm building a bot to run a game from discord. Players log in and their data is pushed into an array which looks like this:

name: Emily, tag: something#4532, kills: 0, alive: true

Whenever the bot restarts however (which happens a few times a day), all that data is lost.
I know the best way to fix this is to write that information to a file and then recall that file whenever the bot restarts.
I know how to write to a file, but im not sure how to recall that information in way that I could push it back into the array.

svyat1s
  • 868
  • 9
  • 12
  • 21
Jasper
  • 21
  • 3
  • 1
    Duplicate of [Using Node.JS, how do I read a JSON file into (server) memory?](https://stackoverflow.com/questions/10011011/using-node-js-how-do-i-read-a-json-file-into-server-memory) – esqew Mar 10 '21 at 21:19

1 Answers1

1

If I am following your question correctly, it looks like you just need to read the contents of a file and store it in a variable to use it again. This can easily be done with JSON.parse(). Here is an example I found from the post that was linked in a comment to your post:

var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('file_path_here', 'utf8'));

This should work, assuming you entered the data into the file with JSON.stringify()

Brandons404
  • 109
  • 1
  • 9