简体   繁体   中英

DiscortJS bot send a message back on the channel mentioning a specific user

I have build this message on js. And i want to add a condition if you tag an user the bot to add the message + tag that person otherwise just to send a normal message.

The issue that I have is what is the right variable for user_mention. I found different ways, but couldn't make it to work.

DiscordClient.on('message', message => {
  const msg = message.content.toLowerCase();
  const mention = message.mentions.users;

  if (msg === "yubnub") {
    if (mention == null){
      message.channel.send('YUB NUB!! YUB NUB!! Grrrrr!!');
    } else {
      message.channel.send('YUB NUB!! YUB NUB!! Grrrrr!! ' + ${@user_mention})
    }
  }

});

Thank you @boris and @Adrian. The final code looks like this:

if (msg.startsWith("yubjub")) {
const mention = message.mentions.members;

if (mention.size === 0){

  message.channel.send('YUB NUB!! YUB NUB!! Grrrrr!!);

} else {
    const mentionUser = mention.first().user;

    message.channel.send('YUB NUB!! YUB NUB!! Stab Stab Stab <@' + mentionUser.id + '> !!');

}

}

I think mention is an array of users . So you can do:

for (const user of mention) {
    message.channel.send('YUB NUB!! YUB NUB!! Grrrrr!! @' + user.username)
}

Try:

const mention = message.mentions.users.first();

Source: https://anidiots.guide/first-bot/command-with-arguments#grabbing-mentions

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