admin管理员组

文章数量:1289868

How to properly tag a user using message.channel.send in discord.js?

Here is my code:

function replaceAll(str, find, replacer) {
      return str.replace(new RegExp(find, 'g'), replacer);
    }
    Bot.on('message', (message) => {
      var mcontent = message.content;
      var mauth = message.author;
      var mtag = mauth.tag;
      if (mcontent.includes("@p")) {
        var newmsg = replaceAll(mcontent, "@p", "@" + mtag);
        message.delete();
        message.channel.send(newmsg);
    });

And, it prints this: (By the way, I am hieyou1#6009) [with the message.delete(); disabled]

No console logs are present when I execute.

How to properly tag a user using message.channel.send in discord.js?

Here is my code:

function replaceAll(str, find, replacer) {
      return str.replace(new RegExp(find, 'g'), replacer);
    }
    Bot.on('message', (message) => {
      var mcontent = message.content;
      var mauth = message.author;
      var mtag = mauth.tag;
      if (mcontent.includes("@p")) {
        var newmsg = replaceAll(mcontent, "@p", "@" + mtag);
        message.delete();
        message.channel.send(newmsg);
    });

And, it prints this: (By the way, I am hieyou1#6009) [with the message.delete(); disabled]

No console logs are present when I execute.

Share Improve this question edited Jun 21, 2018 at 13:01 panta82 2,7213 gold badges21 silver badges38 bronze badges asked Jun 20, 2018 at 23:38 MikeyMikey 3141 gold badge5 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Mentions from the bot is a little bit different. You need to use <@userid>.
But Discord.JS has a cleaner way to mention a user, instead of using message.author.tag, just use message.author. That will tag the user that sent the message.

message.channel.send(`Hey ${message.author} how's it going?`);

Or with the old way to concatenate the string:

message.channel.send("Hey " + message.author + " how's it going?");

本文标签: javascriptHow to properly tag a user using messagechannelsend in discordjsStack Overflow