-2

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.

Arachnid
  • 11
  • 1
  • 1
    Where’s the code you’ve written so far as a [mre]? Please familiarize yourself with our standards on [ask] – esqew Mar 04 '22 at 16:14
  • @esqew please check my new edit, I have added part of the code. The code that I have added represents "Receiving" – Arachnid Mar 04 '22 at 16:27

1 Answers1

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.

  • Hi, Thank you for responding but how would I set the commands etc? My script works but I need to change the whole config with the discord command $setconfig which I need to attach a file into it. But I want it so a specific part would change such as $changeconfigsending where then I would write what I need to change such as $changeconfigsending 4447234 and it would automatically apply it. Sorry if my english is a bit confusing Im new to programming/scripting and English is not my first language. If so could you give an example of what I want it to be? The very first snipit. @smallketchup82 – Arachnid Mar 08 '22 at 20:30