Discord.js XP系统,将角色添加为成员级别

Discord.js XP系统,将角色添加为成员级别,discord,discord.js,Discord,Discord.js,所以我找到了一些简单易懂的XP系统,我只想编辑它并添加一个功能,比如在升级时添加角色。目前,什么都没有发生。我尝试过在这里和那里移动一些东西,结果要么用升级嵌入的方式滥发一个频道,要么什么都没有。我没有错误 提前谢谢 代码如下: // Further up are the level roles defined, etc but that's not relevant. if (message.author.bot) return; let xpAdd = Math.floor(Mat

所以我找到了一些简单易懂的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)
  });
//进一步定义了级别角色等,但这与此无关。
if(message.author.bot)返回;
设xpAdd=Math.floor(Math.random()*7)+8;
如果(!xp[message.author.id]){
xp[message.author.id]={
xp:0,
级别:1
};
}
让curxp=xp[message.author.id].xp;
让curlvl=xp[message.author.id].level;
设nxtLvl=xp[message.author.id].level*500;
xp[message.author.id].xp=curxp+xpAdd;
让lvlupRoles=newdiscord.RichEmbed()
.setAuthor(“升级!”,message.author.displayAvatarURL)
.set缩略图(“https://cdn.discordapp.com/attachments/682717976771821646/705125856455950496/Levelup.png")
.setColor(“随机”)
.setDescription(`Level:${curlvl+1}\nXP:${curxp}`);
if(nxtLvl{message.delete(10000)});
如果(卷发高度==“10”){
lvlupRoles.addField(“已获得角色”,“${Level10Role.toString()}”,true)
让addrankup=message.member;
addrankup.addRole(Level10Role.id).catch(console.error);
message.channel.send(lvlupRoles.catch(e=>console.log(e))
}
如果(卷发高度==“20”){
lvlupRoles.addField(“已获得角色”,“${Level20Role.toString()}”,true)
让addrankup=message.member;
addrankup.addRole(Level20Role.id).catch(console.error);
message.channel.send(lvlupRoles.catch(e=>console.log(e))
}
//等等。
}
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;