How do I add variable `XP` to `userData.XP` for my Discord.js bot?

1.6k views Asked by At

So I have looked around but almost none of it is for Discord and included HTML which I can't use for Discord Bots. Here is what I am trying to do: Make an XP bot so that I can do an interesting Discord Game. Here I want XP to end up getting +10 when I say !killSlime but the output ends up as 0. If anyone could help with this I would Greatly Appreciate it. Also I do realize that only one person can use this bot. That is on purpose so that I can test and edit without my friends asking what is going on here.

const Discord = require("discord.js");
const bot = new Discord.Client();
const fs = require('fs');
let XP = JSON.parse(fs.readFileSync('./XP.json', 'utf8'));

bot.on("message", msg => {
let prefix = "!";

console.log(0)
if(!msg.content.startsWith(prefix)) return;

console.log(0.1)
if(msg.author.id != "235144525763444736") return;

console.log("help command")
if (msg.content.startsWith(prefix + "help")) {
    msg.reply("Here are my commands: !help,")
}

let userData = XP[msg.author.id];
if (!userData) userData = {XP: 0, level: 0};

let curLevel = Math.floor(0.1 * Math.sqrt(userXP));
if (curLevel > userData.level) {
    userData.level = curLevel;
    msg.reply('You have lvled ^ to lvl **${curLevel}**!');
}

console.log("level")
if (msg.content.startsWith(prefix + "level")) {
    msg.reply(`You are lvl ${userData.level}, with ${userData.XP} XP Right Now.`);
}

if (!XP[msg.author.id]) XP[msg.author.id] = {XP: 0, level: 0}



console.log("Slime")
if (msg.content.startsWith(prefix + "killSlime")) {
    userData.XP += 10
    msg.channel.sendMessage(`${msg.author} has killed a Slime!`)
}

console.log(XP)
fs.writeFile('./XP.json', JSON.stringify(XP), console.error);

});
1

There are 1 answers

1
Jhonforge_Gaming On BEST ANSWER

I Forgot this:

let userData = XP[msg.author.id]; if (!userData) userData = {XP: 0, level: 0};