简体   繁体   中英

Discord.js grab mentioned user for text

Okay so I'm trying to grab a user that was mentioned for this bot I'm making (harry potter themed) and I want my bot to grab the user that was mentioned in the command and paste it's output to text so the bot will mention the target user I've looked over a couple of ways to do so and just can't find the right way probably leaving something out here chuck id undefined error.

async run(message, args) {
    var mention = message.mentions.users.first().id
    var expell = Math.floor(Math.random() * 100) + 1;
if (expell < 50)
message.reply('Target was hit by Expelliarmus and is disarmed.<@{mention}>')
else if (expell < 70)
message.reply('the spell was blocked.<@{mention}>')
else
message.reply('Missed target completely')

}

}

The .users property returns a Collection<Snowflake, User> . This extends upon Map . .id isn't a property or function of Map object's first() . You can actually just mention a user by putting their User object in the string.

Also if trying to access the user object by providing the ID is useless when you are already calling .first() . The function gives you the first User on the Collection without having to give it a key. The correct method of doing that was:

var mention = message.mentions.users.get(id)

Also, consider this:

.users

Any users that were mentioned Order as received from the API, not as they appear in the message content

Type: Collection<Snowflake, User>

It's not guaranteed that .first() is actually going to be the most recent mention if there's by chance multiple mentions in a message, so you'll have to figure out how you want to approach that.

What you wanted by the way was:

I realize you were just putting the id in the string but this is how I'd do it. It's neater to me.

async run(message, args) {
// store the first User object on the Map in variable mention
var mention = message.mentions.users.first()
var expell = Math.floor(Math.random() * 100) + 1;
    if (expell < 50)
     message.reply(`Target was hit by Expelliarmus and is disarmed.${mention}`)
    else if (expell < 70)
     message.reply(`The spell was blocked.${mention}`)
    else
     message.reply('Missed target completely')

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM