0

Ok, so I am making a Discord bot with node.js, and in my bot, I am trying to add 1 to the ticket numbers so a user will have a number when they submit their ticket, and I am storing this number in a .json file. When I make any changes to my code, or restart the web server, the number resets. I have also noticed the .json file isn't changing, only the number. I know that .json isn't the way to go for this problem, but I know it works, and I don't need much out of it, if you could look at my code and see my issue, help me out please.

CODE:

const Discord = require("discord.js");
const fs = require("fs");
const num = require("./numbers.json");
const fileName = "./numbers.json";
const file = require(fileName);

exports.run = (client, message, args, member) => {
  if (args[0] === undefined) {
    var embed = new Discord.RichEmbed()
      embed.setColor(0xFF0000);
      embed.addField("ERROR", "You did not specify a reason!", false);
    message.channel.send(embed);
    message.react("❎");
  }
  else {
    var arg = args.join(" ")
    file.ticket = num.ticket + 1;
    fs.writeFile(fileName, JSON.stringify(file), function (err) {
      if (err) return console.log(err);
      console.log(JSON.stringify(file));
      console.log('writing to ' + fileName);
    });
    var embed = new Discord.RichEmbed()
      embed.setColor(0x00AE86);
      embed.setTitle(arg + " *(#" + num.ticket + ")*");
      embed.setFooter(`Submission by @${message.author.tag}`, message.author.displyAvatarURL)
    client.channels.get("566996295768735745").send(embed)
    message.react("✅");
    message.channel.send("Your ticket has been submitted! *(#0)*")
  }
}```

1 Answers1

0

Your code is nearly correct. Your issue is with the un-needed import statement 'num' at the top. The following code should work.

const Discord = require("discord.js");
const fs = require("fs");

// remove the 'num' import and simplify to 'fileName' and 'file'
const fileName = "./numbers.json";
const file = require(fileName);

exports.run = (client, message, args, member) => {
    if (args[0] === undefined) {
        var embed = new Discord.RichEmbed();
        embed.setColor(0xFF0000);
        embed.addField("ERROR", "You did not specify a reason!", false);

        message.channel.send(embed);
        message.react("❎");
    } else {
        var arg = args.join(" ");

        // change 'num' to 'file'
        file.ticket = file.ticket + 1;

        fs.writeFile(fileName, JSON.stringify(file), function (err) {
            if (err) return console.log(err);
            console.log(JSON.stringify(file));
            console.log('writing to ' + fileName);
        });
        var embed = new Discord.RichEmbed();
        embed.setColor(0x00AE86);
        embed.setTitle(arg + " *(#" + num.ticket + ")*");
        embed.setFooter(`Submission by @${message.author.tag}`, message.author.displyAvatarURL);
        client.channels.get("566996295768735745").send(embed);
        message.react("✅");
        message.channel.send("Your ticket has been submitted! *(#0)*")
    }
};

Here is the code I used to test the solution. It was made with direct ref to this solution on SO

const fs = require('fs');

var fileName = './numbers.json';
var file = require(fileName);

file.tickets = file.tickets + 1;

fs.writeFile(fileName, JSON.stringify(file), function (err) {
    if (err) return console.log(err);
    console.log(JSON.stringify(file));
    console.log('writing to ' + fileName);
});
FLYINN
  • 48
  • 5
  • This code does not work for me, it still will not edit the .json, so when I change up the files, it resets back at 0. https://i.imgur.com/TUtOd5P.png – Permentert Gamez Apr 16 '19 at 20:30
  • Do you have anything else editing the file? Is the only structure in the file `{ ticket }` – FLYINN Apr 16 '19 at 20:50