1

I am currently working on a members bot. I'm trying to make it so the bot creates an invite and then it gets saved on the JSON file called "servers.json" and the output should be similar to the following...

{
  "GUILD_ID":"INVITE TOKEN"
}

At the moment, it just creates the invite. Any suggestions?

The command is

  if (message.content == `${PREFIX}setup`) {
    if (!message.member.hasPermission("ADMINISTRATOR"))
      return message.reply("Nope, you can't since you don't have permissions.");
    message.channel
      .createInvite({ unique: true, maxAge: 0, maxUses: 0 })
      .then(invite => {
        let invvembed = new Discord.MessageEmbed()
          .setTitle("Setup command")
          .setDescription(
            `Setup has been completed. Invite link is https://discord.gg/${invite.code}. Make sure to keep this invite on forever. Now once you have bought the access for your code, your server should popup on **g+find**`
          )
          .setFooter("Setup");
        message.channel.send(invvembed);
        
  }) 
  }
dippas
  • 58,591
  • 15
  • 114
  • 126
ZinX
  • 35
  • 6
  • My suggestion would be to include a [mcve] so we can help you. – evolutionxbox Jul 19 '20 at 18:17
  • Um, I'm confused really. On my Discord bot, it creates an Invite Code for the current server that the command was executed on. Now the only thing I need is for it to get written inside of the JSON file. – ZinX Jul 19 '20 at 18:20
  • And that is good, but how can we help when we can’t even see the code you’re using? – evolutionxbox Jul 19 '20 at 18:31
  • ```if (message.content == `${PREFIX}setup`) { message.channel.createInvite({ unique: true, maxAge: 0, maxUses: 0 }) .then(invite => { let invvembed = new Discord.MessageEmbed() .setTitle("Setup command") .setDescription( `Setup has been completed. Invite link is https://discord.gg/${invite.code}. Make sure to keep this invite on forever. Now once you have bought the access for your code, your server should popup on **g+find**` ) .setFooter("Setup"); message.channel.send(invvembed); }); }``` – ZinX Jul 19 '20 at 18:35
  • Please remove that comment and put the code into the question. – evolutionxbox Jul 19 '20 at 18:37
  • There we go, @evolutionxbox. – ZinX Jul 19 '20 at 18:38
  • How about the code to save the file? – evolutionxbox Jul 19 '20 at 18:39
  • That's what I need help on. – ZinX Jul 19 '20 at 18:39
  • I have been using numerous code from SlackOverflow but seems to not work – ZinX Jul 19 '20 at 18:40
  • “seems not to work” is the kind of stuff you need to share. Otherwise we’re not helping, but instead just writing code for you – evolutionxbox Jul 19 '20 at 18:44
  • No I mean, I've used my other codes from my level up commands but I copied them, they dont work. Obviously I changed some stuff. – ZinX Jul 19 '20 at 18:46
  • I tried importing/requiring the FS package but i dont seem to know how. – ZinX Jul 19 '20 at 18:47
  • Usually someone would've just gave a way to give it based on the code given above. – ZinX Jul 19 '20 at 18:47
  • Does this answer your question? [Writing files in Node.js](https://stackoverflow.com/questions/2496710/writing-files-in-node-js) – evolutionxbox Jul 19 '20 at 18:47

1 Answers1

0

You can have a look at the edit-json-file npm package.

An example can be:

const editJsonFile = require("edit-json-file");
let config = editJsonFile(`./config.json`);

//Put your code here
  if (message.content == `${PREFIX}setup`) {
    message.channel
      .createInvite({ unique: true, maxAge: 0, maxUses: 0 })
      .then(invite => {
        config.set("GUILD_ID", "https://discord.gg/" + invite.code);
        config.save();
    }) 
  }

Edit values with config("path", "value");. And don't forget to save your file after editing it: config.save().

Dorian349
  • 1,521
  • 1
  • 9
  • 18