An interesting one. I have a Discord Bot with a Levels/XP System. It works absolutely fine but at some point. My XP Json File gets cleared for no reason and it gives me the Unexpected End of Json Input.
Code:
let xpAdd = Math.floor(Math.random() * 8) + 2;
console.log(xpAdd)
if (!xp[message.author.id]) {
xp[message.author.id] = {
xp: 0,
level: 1
};
if (charactis_talked_users.has(message.author.id)) return;
}
if(message.author.bot) return
let curxp = xp[message.author.id].xp;
let curlvl = xp[message.author.id].level;
let nxtLvl = xp[message.author.id].level * 680;
xp[message.author.id].xp = curxp + xpAdd;
if (nxtLvl <= xp[message.author.id].xp) {
xp[message.author.id].level = curlvl + 1;
let lvlup = new Discord.RichEmbed()
.setAuthor('Level 🆙', 'https://i.imgur.com/s6OpKNC.jpg')
.setDescription(`**${message.author.username}** has Levelled up!`)
.setThumbnail(message.author.displayAvatarURL)
.setColor("#ddff00")
.addField("**New Level**", `${curlvl + 1}`)
.setFooter(`$120 Earned! | Run n!level or n!profile to check your progress!`)
message.channel.send(lvlup).then(msg => {
db.add(`userBalance_${message.author.id}`, 120)
});
}
fs.writeFile("./database/xp.json", JSON.stringify(xp, null, 2), (err) => {
if (err) console.log(err)
I think its something to do with the fs.writeFile. If anyone can give me a solution to this. That would be great.
Project: nate-bot