Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda