Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 views
in Technique[技术] by (71.8m points)

else code not working properly discord.js

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...