【问题标题】:else code not working properly discord.js否则代码无法正常工作 discord.js
【发布时间】:2021-04-09 06:47:14
【问题描述】:

我试图在 discord 上发出一个拥抱命令来拥抱提到的用户。我设置了一个命令来检查是否有提到的用户。如果我不提及用户,它会按预期工作,但是当提及用户时,它会输出相同的内容。这是我的代码:

    if (message.mentions.users.length > 1) {

        const hug = new Discord.MessageEmbed()
            .setAuthor(`${message.mentions.users.first().username} got a hug from ${message.author.username}!`, `${message.author.avatarURL({ dynamic:true })}`)
            .setImage(hugs[hugged])
            .setColor('#ffb9f4')
        message.channel.send(hug);

    } else {
        message.channel.send('Please mention a user to hug!');
    };

我认为不需要其他任何东西,但请告诉我

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    您的条件检查是否有多个用户被标记,但您可以检查是否有任何被标记的用户

    if (message.mentions.users == true)
    

    第二个问题是 users 是一个集合,所以你必须使用 .size 而不是 .length

    if (message.mentions.users.size > 0)
    

    两个都不错。

    【讨论】:

    • 谢谢!第一个代码 if(message.mentions.users == true 不起作用,但用大小改变长度效果很好
    • 我的错!集合(就像 js 中的其他对象一样)即使为空也会返回 true,因此它无法工作
    【解决方案2】:

    如果你是在公会中做的,我很确定所有提及都是针对 GuildMember,尝试将你的 if 语句设置为

    if(message.mentions.members.length > 1 || message.mentions.users.length > 1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-28
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      相关资源
      最近更新 更多