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

128
Visualizações
Discord.js write data to file

Heyo, I need help here. I want to structure a JSON file. Down here I can explain it a little bit better:

I have a channelId and a serverName and I want to safe these in a config file with fs. Here is how it would look now:

    {
  "server": "MyServer"
    }

But I want it to look like this:

[
  {
    "channelId": "MyChannel",
    "info": [
      {
        "server": "MyServer"
      }
    ]
  }
]

Here is my code:

const server = "MyServer"
const channel = "MyChannel" //I want this to be at the top

  const template = {           //this must be edited i think
    server: "Cool Server",     
  }
   template.server = server;
  fs.writeFileSync("channels.json", JSON.stringify(template, null, 2));

With "server" I mean the server where the channel exists. Thanks for helping me ;-)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are lots of ways you can do this. Try this as an easy fix.

const server = "MyServer";
const channel = "MyChannel"; //You want this to be at the top

const template = [
  {
    channelId: "Your Channel",
    info: [
      {
        server: "Your Server",
      },
    ],
  },
];
template[0].channelId = channel;
template[0].info[0].server = server;
fs.writeFileSync("channels.json", JSON.stringify(template, null, 2));

Later on you can use a for loop or map function on the template array to add channels and servers dynamically.

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