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

1 answer

  • answered 2021-03-03 04:02 Jakye

    mentions is not a valid property of content. You're looking for Message.mentions which is a MessageMentions 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}`);
        });
    });