Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have tried to make a hug command on discord to hug the mentioned user. I set up a command to check if there has been a user mentioned. If I do not mention a user, it works as intended, but when mentioning a user, it outputs the same thing. Here is my code:

    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!');
    };

I don't think anything else should be needed but let me know

question from:https://stackoverflow.com/questions/65546358/else-code-not-working-properly-discord-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
950 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your condition checks if there is more than one user tagged, but you can check if there is any tagged users with

if (message.mentions.users == true)

Second problem is that users is a collection so you have to use .size instead of .length

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

Both are good.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...