简体   繁体   English

如何使用 Discord.JS 标记用户?

[英]How to tag users using Discord.JS?

I am looking to create a command in order to tag certain users automatically using their username eg.我正在寻找创建一个命令,以便使用他们的用户名自动标记某些用户,例如。 "@RYAN#9602" whenever a switch statement case is executed.每当执行 switch 语句 case 时,“@RYAN#9602”。 Currently the problem that I'm experiencing in that whenever I do try to tag users, it just writes "@RYAN#9602" into the text channel and doesn't actually tag them.目前我遇到的问题是,每当我尝试标记用户时,它只会将“@RYAN#9602”写入文本频道,而实际上并未标记他们。

This is what I have tried:这是我尝试过的:

var players = [
"@RYAN#9602"
]



switch(args[0].toLowerCase()){

 case "play":
            message.channel.send(players.join('\n'));
            break;
}

So in summary, using Discord.JS, how do I make the bot actually tag the user so that they will get 'pinged' instead of just sending a message of their name into the text channel?总而言之,使用 Discord.JS,我如何让机器人实际标记用户,以便他们得到“ping”,而不仅仅是将他们的姓名消息发送到文本通道?

Thanks very much in advance!首先十分感谢!

You have two options.你有两个选择。

You can either use the toString method on the User object, or form the mention yourself using the user's ID.您可以在 User 对象上使用toString方法,也可以使用用户 ID 自己形成提及。

Here's an example using toString : 这是使用 toString 的示例

client.on("message", => {
    const channel = message.channel;
    channel.send(message.author.toString());
});

And here's an example using the ID这是使用 ID 的示例

client.on("message", => {
    const channel = message.channel;
    channel.send("<@" + message.author.id + ">");
});

Try using the toString method.尝试使用toString方法。

client.on("message", => {
    const channel = message.channel;
    channel.send(message.author.toString());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM