discord - Discord.js XP 系统,添加角色作为成员升级
问题描述
所以我找到了一些易于理解的 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)
});
解决方案
我认为这可能是由于您存储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;
推荐阅读
- linux - 如何让 git 更喜欢文件名的选项卡完成而不是分支名称?
- vba - 无法使用 selenium webdriver for vba 从下拉列表中选择项目
- android - 点击圆形图标按钮会在颤动中转换为方形
- python - jsonpath_ng.lexer.JsonPathLexerError:第 1 行错误,第 8 列:意外字符:/
- multiple-columns - 在 Power Query 中同时展开所有列
- android - 有人知道 google book api 畅销书排行榜吗?
- php - 使用服务帐户访问谷歌日历但答案变量为空
- php - 使用 AWS PHP SDK 获取 403 禁止执行 API Gateway getConnection 或 postToConnection(在 Postman 中工作)
- python - 如何使用 HTML 显示熊猫图表
- python - AWS 上的 jupyter 笔记本