Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
how can i store data in json file Continuous for discord js

I want to take the message information from the user and save it in a JSON file and this data is constantly added, but with the following code, this data is constantly replaced. and I don't replace data I want to add data this is my code :

const fs = require("fs");
const { Client, Intents } = require("discord.js");

const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
const now = new Date();
const obj = {
  table: [],
};
let confirm = false;
const { badWords } = require("../badWordList.json");
client.on("message", async (message) => {
  if (message.author.bot) return;
  for (let i = 0; i < badWords.length; i++) {
    if (message.content.toLowerCase().includes(badWords[i].toLowerCase()))
      confirm = true;
  }
  if (confirm) {
    obj.table.push({
      name: message.author.username,
      date: `${now.getFullYear()}/${now.getMonth()}/${now.getDate()}`,
      message: message.channel.messages.channel.lastMessage.cleanContent,
    });
    fs.writeFile("myjsonfile.json", JSON.stringify(obj), function (err) {
      if (err) throw err;
      console.log("complete");
    });
  }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When using the fs.writeFile() it replaces the content of the file, as written in the docs.

At a first glance, you might want to use fs.write(), see the docs for usage.

But the NodeJS docs says :

It is unsafe to use fs.write() multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream() is recommended.

Since you are in asynchronous mode, you shoud probably define a write stream to the file and then write to it, it gives you something like that :

// -snip-
const whateverJsonFileStream = fs.createWriteStream("myjsonfile.json");

client.on("message", async (message) => {
    //-snip-

    if (confirm) {
        obj.table.push({
            name: message.author.username,
            date: `${now.getFullYear()}/${now.getMonth()}/${now.getDate()}`,
            message: message.channel.messages.channel.lastMessage.cleanContent,
        });

        whateverJsonFileStream.write(JSON.stringify(obj), function (err) {
            if (err) throw err;
            console.log("complete");
        });
    }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!