如何为我的Discord.js机器人添加变量`XP`到`userData.XP`?

时间:2016-12-17 01:08:42

标签: javascript node.js bots discord

所以我环顾四周,但几乎没有一个是Discord,包括HTML,我不能用于Discord Bots。这是我想要做的:制作一个XP机器人,这样我就可以做一个有趣的Discord游戏。在这里我希望XP最终得到+10当我说!killSlime但输出最终为0.如果有人可以帮助这个我会非常欣赏它。我也意识到只有一个人可以使用这个机器人。这是故意的,这样我就可以在没有朋友询问这里发生了什么的情况下进行测试和编辑。

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 个答案:

答案 0 :(得分:0)

我忘了这个:

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