简体   繁体   中英

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

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);

});

我忘了这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM