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

210
Visualizações
How Do I edit a .json value through discord commands?

Im new to coding and I've coded myself a discord bot coded in Node.js which can change the whole config.json, start the bot, stop the bot, create instances etc... I'm stuck at a part where I want to change a specific part of the config.json and not the whole config.

{
   "cookie": "",
   "proxy": "",
   "webhook": "https://discord.com/api/webhooks/...",

   "sending": [
     1647598632,
     1647676420
   ],
   "receiving": [
     124127383
   ]
 }

I want to change the "Sending" (currently 1647598632, 1647676420) and "Receiving" (currently 124127383) stuff from a simple discord command such as,

 $mass_send 124127383 [1647598632,1647676420].

I want it so I can change the ID's. If you would like to cross reference with the code I made for changing the whole config not specific variables please let me know. Thank you to whoever helps.

I do have a Config updater command, but it updates the whole config rather than a specific part of the config

    }
else if (command === '$setconfig') {
    if (message.attachments.size < 1)
        return message.reply({
            embeds: [
                new MessageEmbed()
                .setColor('RED')
                .setDescription('Please attach a new config file.')
            ]
        })
   
    const attachment = message.attachments.first();
    console.log(attachment);
   
    if (!attachment.contentType || attachment.contentType.indexOf('text/plain') < 0)
        return message.reply({
            embeds: [
                new MessageEmbed()
                .setColor('RED')
                .setDescription('Config must be a .txt/.ini file.')
            ]
        })
   
    superagent('GET', attachment.url)
    .then(resp => {
        const configText = resp.text;
       
        try {
            ini.parse(configText);
        } catch {
            return message.reply({
                embeds: [
                    new MessageEmbed()
                    .setColor('RED')
                    .setDescription('Failed to parse ini file.')
                ]
            })
        }
       
        fs.writeFileSync(instanceDirectory + '/settings.ini', configText);
        return message.reply({
            embeds: [
                new MessageEmbed()
                .setColor('RED')
                .setDescription('Config file successfully updated.\nstop the bot with `$stop`')
            ]
        })
    })

I want to edit a specific part rather than the Whole entire config. Please let me know if this is possible Thanks! Also Please let me know if it is unclear, i can provide more information.

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

0

I accomplished this before when I was trying to use a .json file as a temporary database in one of my older bots. The code for it is long gone, but I think you might be able to get something out of this.

What I noticed is that when you require() a json file, the contents get cached into the variable, therefore editing the values in the variable change only the contents of the cached json within the variable, not the json file itself. This lets you run javascript operations on it without affecting the overall aspect of the json file (such as adding onto it, etc).

Then when you were done and wanted to update the json file to persist the data, you would use JSON.Stringify() on the variable holding all that data, and use fs to replace config.json's value with the stringified JSON.

To give a programatic view of what I'm saying, I'll try my best to show it in code

var config = require('config.json'); // get config.json's value into the variable

console.log(config) // this shows the value of the variable, which should print out a json object to the command line

var newstuff = {
        username: "DiscordUser",
        id: "1345678910"
};

config.push(newstuff) // add new data to the JSON, however this only affects the variable. In your case, this is where you'll want to manipulate the JSON to adjust the specific values of your json keys to the desired values

console.log(config) // should show config.json + the newly added content

var stringifiedconfig = JSON.Stringify(config); // convert the JSON content to a string so that we can put the JSON into the file
fs.writeFile('config.json', stringifiedconfig); // replace the value of config.json with the stringified config.

var newconfig = require('config.json'); // fetch new config.json so that you don't have to refresh the bot

This post might help you, I recommend reading it.
I hope this answers your question and that I'm not just misunderstanding.

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