您如何将用户ID从名称列表更改为不一致的用户名和歧视?

时间:2020-05-19 22:46:40

标签: javascript discord.js

我想创建一个如下所示的命令:!command @user @user机器人以@ user,@ user响应。我目前有这段代码来获取参数列表和嵌入:

let people = message.content.slice(6).trim().split(/ +/g);
const embed = new MessageEmbed()
            .setColor("RANDOM")
            .addField(`**${people}**`)
            .setThumbnail(message.author.avatarURL)
        message.channel.send(embed);

和我创建的嵌入显示如下:<@!number>,<@ number>。 如何使它显示实际的用户名和歧视,而无需再次对消息中的用户执行ping操作?

1 个答案:

答案 0 :(得分:0)

您可以使用Discord的MessageMentions.USERS_PATTERN(即/<@!?(\d{17,19})>/g)并使用匹配的ID(第一个捕获组)来获取用户的tag

`**${people.replace(MessageMentions.USERS_PATTERN, (_, $1) => message.client.users.cache.get($1).tag)}**`

const MessageMentions = {USERS_PATTERN: /<@!?(\d{17,19})>/g}
const message = {client: {users: {cache: new Map([
  ['712454579618447360', {tag: 'user1#1234'}],
  ['712454581979840513', {tag: 'user2#5678'}]
])}}}
const people = '<@712454579618447360> <@!712454581979840513>'

console.log(`**${people.replace(MessageMentions.USERS_PATTERN, (_, $1) => message.client.users.cache.get($1).tag)}**`)