【问题标题】:How do i include a array?我如何包含一个数组?
【发布时间】:2020-05-24 11:51:19
【问题描述】:

所以我正在为一个不和谐的机器人编写一个 xp 系统,我需要一些帮助。我希望机器人只在写一个单词时给 xp。例如:当有人说在字符串中或其他什么时,我想给 xp。但我也希望它为其他词添加 xp。所以我把我所有的话都放在一个数组中然后我就卡住了。

这是我的 xp 代码:

if (message.content === "test"){
    let xpAdd = Math.floor(1);
    console.log(xpAdd);

    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 * 300;
    xp[message.author.id].xp = curxp + xpAdd;
    if (nxtlvl <= curxp) {
      xp[message.author.id].level = curlvl + 1;
    }

    fs.writeFile("./database.json", JSON.stringify(xp), err => {
      if (err) console.log(err);
    });
  }

第一行只是一个测试,看看机器人是否工作,我需要用我的数组替换它。

感谢您的帮助

【问题讨论】:

    标签: javascript node.js bots discord discord.js


    【解决方案1】:

    我会使用Array#some 方法。

    // the array of words to check for
    const words = ['touge', 'bean', 'wine'];
    
    // example message event
    client.on('message', (message) => {
      if (words.some(word => message.content.includes(word))) {
        // do your XP stuff in here
      }
    });
    

    【讨论】:

    • 感谢它的帮助。我还有一个问题。升级后如何重置xp?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2020-05-15
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    相关资源
    最近更新 更多