3
client.on('message', message => {
     if (message.content === `L!hug`) {
        if (!message.mentions.users.size) {
            return message.reply('you need to tag a user in order to hug them!!');

            const taggeduser = message.mentions.users.first();
        }
        // message goes below!
         message.channel.send(userID + ` you just got a hug  https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
    }
}); 

I have tried a few ideas and I am very new to this library of language (discord.js)

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
FBILOLIGIRL
  • 69
  • 2
  • 3
  • 12
  • Can you elaborate on what you want to accomplish? Since you're not asking a question, it's very hard to figure out what you need help with – ClydeFrog Jan 29 '20 at 10:55

6 Answers6

4

You can use message.mentions.members.first()

client.on('message', message => {
     if (message.content.startsWith('L!hug')) { 
    let targetMember = message.mentions.members.first();
    if(!targetMember) return message.reply('you need to tag a user in order to hug them!!');
        // message goes below!
         message.channel.send(`<@${targetMember.user.id}> you just got a hug  https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
    }
}); 
Cipher
  • 2,702
  • 1
  • 6
  • 17
  • 1
    this did not work unfortunately is there another way to do this as i did ` }); client.on('message', message => { if (message.content === `L!hug`) { let targetMember = message.mentions.members.first(); if (!targetMember) return message.reply('you need to tag a user in order to hug them!!'); // message goes below! message.channel.send(`<@${targetMember.user.id}> you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`); } }); ` – FBILOLIGIRL Jan 24 '20 at 19:20
  • I didn’t get an error it’s just not doing the mentioning – FBILOLIGIRL Jan 27 '20 at 07:41
  • What`s message bot reply when you use it? Oh i found an error, you use if message.content === somethink. But with mentions message content will never === somethink..... – Cipher Jan 27 '20 at 07:57
  • Nothing but when I don’t mention people it say @person you need to mention someone – FBILOLIGIRL Jan 27 '20 at 07:57
  • 1
    Change `if (message.content === 'L!hug') {` to `if (message.content.startsWith('L!hug')) {` – Cipher Jan 27 '20 at 07:58
  • ok so it works!!!!! but ` message.channel.send(`${user} + "you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);` with the arrow pointing at the { i got this error after i used ` ` those but when i use ' ' it just goes ${user} has gotten a hug – FBILOLIGIRL Jan 28 '20 at 07:56
  • Sure, because user is not define in my code :) . You can replace ${user} to ${targetMember.user} – Cipher Jan 28 '20 at 08:32
  • `message.channel.send(`${targetmember.user} + you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);` this is what i got when i did the cmd in discord ;-; ^ – FBILOLIGIRL Jan 28 '20 at 09:25
  • Just use my code in answer and its will be work. The stackoverflowMardown interferes with the normal formatting of quotation marks in comments – Cipher Jan 28 '20 at 09:29
2

Discord.js uses many custom toString() and an User return his mention. So if you want to mention an user in a message you can do

This :

`<@${user.id}>`

But a faster way is this :

`${user}`

And you can simply put user without any String it works also if the function runs a .toString() on your string.

like this :

message.channel.send(user + " has made something");

Will mention the user.

Note : It won't work anymore in v13.

Ayfri
  • 570
  • 1
  • 4
  • 24
  • hi @Ayfri could you please explain how to use this a bit better (srry im quite new to this) ``` }); client.on('message', message => { if (message.content === `L!hug`) { let targetMember = message.mentions.members.first(); if (!targetMember) return message.reply('you need to tag a user in order to hug them!!'); // message goes below! message.channel.send(` ${ user.id } you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`); } });``` do you mean to replace the **targetMember** in the message and the cmd or just the message? – FBILOLIGIRL Jan 25 '20 at 06:42
  • breh use **targetMember** instead of user, the **user** was just for example. –  Jun 02 '21 at 07:21
1

If you use a command handler like this one this is how you would make it work

module.exports = {
    name: "hug",
    description: "@ someone to hug them through the bot command.",
    nsfw: false,
    execute(message, args){
        const targetmember = message.mentions.members.first()
        if (!targetmember) return message.reply("you need to tag a user in order to hug them.");
        var huggifs = [`${targetmember} Recieved a hug https://imgur.com/r9aU2xv`, `${targetmember} Recieved a hug https://tenor.com/LUqw.gif`, `${targetmember}  Recieved a hug https://media.giphy.com/media/3ZnBrkqoaI2hq/giphy.gif`, `${targetmember} Recieved a hug https://tenor.com/1jRF.gif`, `${targetmember} Recieved a hug https://media.giphy.com/media/lrr9rHuoJOE0w/giphy.gif`]
        var hugrandomform = huggifs[Math.floor(Math.random()*huggifs.length)];
        message.channel.send(hugrandomform).then().catch(console.error);  
    }
}
0

Here, you can use message.mentions.users.first():

let user = message.mentions.users.first(); // As a shortcut to it

// Check if there is actually a mention
if(!user) {
  return message.reply("You need to tag a user in order to hug them!");
}

// If there is a user, do this code
message.channel.send(`${user.toString()}, you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
// user.toString() will convert the user object to a mention

Hope this helped!

BotNC
  • 31
  • 6
0

Here is a simple version of the code. userID isn't defined, meaning you'd need to use the targetmember. Here, this is how it should be.

client.on('message', message => {
  if (message.content === "L!hug") {
    const targetmember = message.mentions.members.first()
    if (!targetmember) return message.reply("you need to tag a user in order to hug them!!");
    message.channel.send(`${targetmember} you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
  }
})
SirLemons
  • 11
  • 3
0

You can use this to create a mention (note that it will not work in pre, as no mention works in it)

'<@&' + user_id + '>'

Note the @ and &.

Nabeel Khan
  • 3,715
  • 2
  • 24
  • 37