Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda