因此,我发现了一些易于理解的XP系统,我只想对其进行编辑并添加功能,例如在升级时添加角色。目前,没有任何反应。我尝试过四处移动一些东西,这些东西最终要么是在垃圾邮件中嵌入了升级的内容,要么是一无所获。我没有错误。
谢谢!
以下代码:
// Further up are the level roles defined, etc but that's not relevant.
if (message.author.bot) return;
let xpAdd = Math.floor(Math.random() * 7) + 8;
if (!xp[message.author.id]) {
xp[message.author.id] = {
xp: 0,
level: 1
};
}
let curxp = xp[message.author.id].xp;
let curlvl = xp[message.author.id].level;
let nxtLvl = xp[message.author.id].level * 500;
xp[message.author.id].xp = curxp + xpAdd;
let lvlupRoles = new Discord.RichEmbed()
.setAuthor("Level Up!", message.author.displayAvatarURL)
.setThumbnail("https://cdn.discordapp.com/attachments/682717976771821646/705125856455950496/Levelup.png")
.setColor("RANDOM")
.setDescription(`Level: ${curlvl + 1}\nXP: ${curxp}`);
if (nxtLvl <= xp[message.author.id].xp) {
xp[message.author.id].level = curlvl + 1;
let lvlup = new Discord.RichEmbed()
.setAuthor("Level Up!", message.author.displayAvatarURL) .setThumbnail("https://cdn.discordapp.com/attachments/682717976771821646/705125856455950496/Levelup.png")
.setColor("#00703C")
.setDescription(`Level: ${curlvl + 1}\nXP: ${curxp}`);
message.channel.send(lvlup).then(message => {message.delete(10000)});
if (curlvl === "10") {
lvlupRoles.addField("Roles gained", `${Level10Role.toString()}`, true)
let addrankup = message.member;
addrankup.addRole(Level10Role.id).catch(console.error);
message.channel.send(lvlupRoles).catch(e => console.log(e))
}
if (curlvl === "20") {
lvlupRoles.addField("Roles gained", `${Level20Role.toString()}`, true)
let addrankup = message.member;
addrankup.addRole(Level20Role.id).catch(console.error);
message.channel.send(lvlupRoles).catch(e => console.log(e))
}
// ETC . . .
}
fs.writeFile("./storage/xp.json", JSON.stringify(xp), (err) => {
if (err) console.log(err)
});
答案 0 :(得分:1)
我认为这可能是由于您将xp[message.author.id].level
存储在curlvl
中,修改了xp[message.author.id].level
并仍然使用curlvl
。在JS中,仅对象({}
,数组,函数等)通过引用传递,因此当curlvl
是数字时xp[message.author.id].level
不会被更新。
在xp[message.author.id].level = curlvl + 1;
下添加此内容:
curlvl = xp[message.author.id].level;