简体   繁体   中英

Is there is a way to DM someone that is mentioned in a message with discord.js?

Is there is a way to DM someone that is mentioned in a message with discord.js?

this is my code.

client.on("message", message => {
  if (message.author.bot) {return}
  let person = message.content.mentions
  person.send("My message")
})

it's not working for some reason

mentions is not a valid property of content . You're looking for Message.mentions which is aMessageMentions object.


client.on("message", message => {
    if (message.author.bot) return false;

    // Getting the first mentioned user in the message.
    const person = message.mentions.users.first();
    // Checking if any user was mentioned.
    if (!person) return message.reply("Please mention someone.");

    person.send("Hello!").then(() => {
        message.reply(`Message sent to ${person.tag}`);
    }).catch(error => {
        message.reply(`Couldn't send the message to ${person.tag}. | ${error}`);
    });
});

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