简体   繁体   中英

How to make a bot ping a user using discord.js

I'm kinda new to bot developing, but I've made a command called &pat and I want it to work like this: You do &pat @user and I want it to respond like this: @user-executing-the-command, you have successfully patted @user-mentioned-in-the-command. I've come up with this code so far and it works all well apart from pinging the patted person. I does ping them, but it shows @invalid-user, and idk how to fix that. My code:

var pattedone = message.mentions.members.first();

    if(!pattedone) return message.reply('please mention the one who should be patted');
    
    message.reply(`you have successfully patted ${pattedone}`);
    

Do you know how to fix that @invalid-user thing? Thanks. Here is screenshot of the bot's response

According to the Discord.js Documentation

Discord uses a special syntax to embed mentions in a message. For user mentions it is the user's ID with <@ at the start and > at the end, like this: <@86890631690977280>

With this in mind. Simply change

message.reply(`you have successfully patted ${pattedone}`);

to this

message.reply(`you have successfully patted <@${pattedone.id}>`);

EDIT

You'll also need to update

var pattedone = message.mentions.members.first();

to

var pattedone = message.mentions.users.first();

you can mention a user with the following "method":

let mention = `<@${userId}>`;

Discord mentions work like this: <@user-id> . Simply replace user-id with the ID of the "victim". You can also mention roles using <&channel-id> or text channels using <#channel-id> .

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