【问题标题】:Discord.js Issue with .includes function.includes 函数的 Discord.js 问题
【发布时间】:2021-04-08 05:01:02
【问题描述】:

arg 不是变量中的colors 之一时,我正试图让我的机器人返回一条消息,但它一直在发送此消息:

有两个问题我无法解决

  1. 机器人发送两条“红色”消息
  2. 即使我在颜色变量中定义了“黑色”,机器人也会说“这不是有效的皮肤”
let skin = db.fetch(`${message.author.id}.skin`)
        if (skin === null) skin = "red"; //red
        let skin2 = client.emojis.cache.get(e[skin])
        var colors = ["red", "blue", "green", "pink", "orange", "yellow", "black", "white", "purple", "brown", "cyan", "cyan"]

        const attachment = new Discord.MessageAttachment(`./assets/colors/${skin}.png`, `${skin}.png`);
        const embed = new Discord.MessageEmbed()
        .attachFiles(attachment) // <- add attachment
        .setAuthor(`${message.author.tag}\'s skins (${message.author.id})`, message.author.avatarURL())
        .setThumbnail(`attachment://${skin}.png`)
        .addField(`Current skin`, `${skin2}`)
        .addField(`Default skins`, `red, blue, green, pink, orange, yellow, black, white, purple, brown, cyan, lime`)
        .addField(`Unlocked skins`, `none`)
        .addField(`Locked skins`, `none`)
        .setDescription(`To set a skin use ${prefix}skin <skin>`)
        .setTimestamp()
        .setColor('#00ffff')
        .setFooter(message.member.user.tag, message.author.avatarURL());
        if(!args[0]) return message.channel.send(embed)
        for (var i=0; i < colors.length; i++) {
            if (args[0].includes(colors[i])) {
              let skin3 = args[0]
              db.set(`${message.author.id}.skin`, args[0])
              message.channel.send(`Set your skin to ${args[0]}`)

        
            } else if(!args[0].includes(colors[i])) {
                return message.channel.send(`That is not a valid skin`)
            }
        }

如你所见

var colors = ["red", "blue", "green", "pink", "orange", "yellow", "black", "white", "purple", "brown", "cyan", "cyan"]

其中已经有“黑色”,但它执行 else if 函数。

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    当您遍历 colors 数组并检查 if (args[0].includes(colors[i])) 时,您会检查字符串是否包含 colors[i] 字符串。 String#includes 检查是否在另一个字符串 (args[0]) 中找到了一个字符串 (colors[i])。

    下面的例子展示了它是如何工作的。它会一一检查colors 中的每一项:

    const colors = ['red', 'green', 'blue']
    const args = ['red']
    
    for (let i = 0; i < colors.length; i++) {
      console.log(`Does the string "${args[0]}" include the string "${colors[i]}"?`)
      if (args[0].includes(colors[i])) {
        console.log('yes')
      } else {
        console.log('no')
      }
    }

    return 语句将退出循环

    问题是你返回if(!args[0].includes(colors[i]))。在 for 循环中,return 会停止执行并退出函数。

    在您的第一个示例中,colors 数组中的第一项是"red"。在 for 循环中,检查 args[0] ("black") 的值是否包含在 "red" 的字符串中。由于这是错误的,因此您返回 That is not a valid skin 并退出 for 循环。您甚至没有检查 colors 数组 ("blue") 中的第二项。

    在您的第二个示例中,您的消息是"au/skin red"。你开始 for 循环。 colors 数组中的第一项是 "red"。在 for 循环中,检查 args[0] ("red") 的值是否包含在 "red" 的字符串中。因为这是真的,你发送Set your skin to red 但没有return 所以这次你不要退出循环。您检查 colors 数组 ("blue") 中的第二项。由于这不是红色的,因此您返回 That is not a valid skin 并退出 for 循环。这就是为什么您在第二个示例中发送了两条消息。

    数组#includes

    您可以使用Array#includes。它检查数组 (colors) 是否在其条目中包含某个值 (args[0])。你甚至不需要 for 循环。

    const colors = [
      'red', 'blue', 'green', 'pink', 'orange', 'yellow', 'black', 'white', 'purple', 'brown', 'cyan', 'lime',
    ];
    const args = ['white']
    
    if (colors.includes(args[0])) {
      console.log(`Set your skin to ${args[0]}`);
    } else {
      console.log('That is not a valid skin');
    }

    工作代码

    // if fetch returns null, skin is 'red'
    const skin = db.fetch(`${message.author.id}.skin`) || 'red';
    const skin2 = client.emojis.cache.get(e[skin]);
    const colors = [
      'red', 'blue', 'green', 'pink', 'orange', 'yellow', 'black', 'white', 'purple', 'brown', 'cyan', 'lime',
    ];
    
    const attachment = new Discord.MessageAttachment(
      `./assets/colors/${skin}.png`,
      `${skin}.png`,
    );
    const embed = new Discord.MessageEmbed()
      .attachFiles(attachment) // <- add attachment
      .setAuthor(
        `${message.author.tag}\'s skins (${message.author.id})`,
        message.author.avatarURL(),
      )
      .setThumbnail(`attachment://${skin}.png`)
      .addField(`Current skin`, `${skin2}`)
      .addField(`Default skins`, colors.join(', '))
      .addField(`Unlocked skins`, `none`)
      .addField(`Locked skins`, `none`)
      .setDescription(`To set a skin use ${prefix}skin <skin>`)
      .setTimestamp()
      .setColor('#00ffff')
      .setFooter(message.member.user.tag, message.author.avatarURL());
    
    if (!args[0])
      return message.channel.send(embed);
    
    if (!colors.includes(args[0]))
      return message.channel.send(`That is not a valid skin`);
    
    db.set(`${message.author.id}.skin`, args[0]);
    message.channel.send(`Set your skin to ${args[0]}`);
    

    【讨论】:

      猜你喜欢
      • 2020-08-19
      • 1970-01-01
      • 2018-10-07
      • 2020-12-22
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 2021-05-29
      • 2021-04-18
      相关资源
      最近更新 更多