The question is published on by Tutorial Guruji team.
I’m new here and I want to assign username for e.g on execute command !register ‘username’ save it to json file with assigned discord tag/user id and next make command that uses argument ‘username’ from before saved file it is possile on Node.js? I don’t wanna to use sql because it’s blackmagic for me and I don’t have time for it right now.
Answer
If you want to use JSON (I highly recommend AGAINST that, I’ve done it myself and I very often had to deal with my JSON’s being completely wiped). use the fs
package that comes with Node.
set the file as a blank array and this code will work.
var username = message.content.split(" ")[1]; var oldJSON = JSON.parse(fs.readFileSync("./usersfile.json"); var newJSON = oldJSON.push(`{"username": ${username}, "userID": ${message.member.id}, "name": ${message.member.username}, "discriminator": ${message.member.discriminator}}`) fs.writeFileSync(JSON.stringify(newJSON), "./usersfile.json");
Again, I HIGHLY recommend AGAINST using this system as it’s incredibly bug prone. Just learn MySQL or MongoDB. But if you’re willing to take (the immense) risk, that should work.