我正在为我的不和谐机器人制作一个关卡系统,所以我想要它,所以如果我达到第5级,它将给我一个角色。我希望这样,当您达到某个级别时,您将获得一个角色。
我粘贴了我正在使用的消息平衡系统的代码。
bot.on("message", async message => {
let xpAdd = Math.floor(Math.random() * 7) + 8;
if(!xp[message.author.id]) {
xp[message.author.id] = {
xp: 0,
level: 1
}
}
if(xp[message.author.id].level[5]) //idk if im doing this right, I want it so if level reaches a certain number, it will give out a role
{
console.log("skrt");
}
let curxp = xp[message.author.id].xp;
let curlvl = xp[message.author.id].level;
let nxtlvl = xp[message.author.id].level * 300;
let rankuplvl = xp[message.author.id].xp * 7;
xp[message.author.id].xp = curxp + xpAdd;
if(nxtlvl <= xp[message.author.id].xp) {
xp[message.author.id].level = curlvl + 1;
const lvlUp = new Discord.RichEmbed()
.setTitle("You leveled up!")
.setColor("#FFC0CB")
.addField("NEW LEVEL", curlvl + 1);
message.channel.send(lvlUp);
}
fs.writeFile("./xp.json", JSON.stringify(xp), (err) => {
if(err) console.log(error)
});
if(message.content.startsWith(prefix + "level")) {
message.channel.bulkDelete(1)
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 = curlvl * 300;
let difference = nxtlvl - curxp;
const lvlEmbed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#FFC0CB")
.addField("XP", curxp, true)
.addField("Level", curlvl, true)
.setFooter(`${difference} XP until next rank!`, message.author.displayAvatarURL);
message.channel.send(lvlEmbed)
}
});
答案 0 :(得分:1)
让我们分解一下达到您期望的行为所需做的事情。假设您正在使用稳定分支(很有可能)...
检查用户级别是否为5。
Equality operator
检索所需的角色。
Guild.roles
Map.get()
或将ID用作#3中的RoleResolvable。 Collection.find()
向用户添加角色。
GuildMember.addRole()
现在,让我们将它们放在一起并将其实现到您的代码中。
if(nxtlvl <= xp[message.author.id].xp) {
xp[message.author.id].level = curlvl + 1;
// Check if the user's level is 5.
if (xp[message.author.id].level === 5) {
// Retrieve the desired role. Uncomment whichever line you need, as described.
// const role = message.guild.roles.get('idHere');
// const role = message.guild.roles.find(role => role.name === 'Level 5');
message.member.addRole(role) // Or simply use the role ID directly.
.catch(console.error); // Make sure to catch any rejected promises.
}
// Continue with your level-up embed.
}