【发布时间】:2022-10-17 00:53:13
【问题描述】:
我已经为此搜索了一段时间,但似乎找不到答案。有谁知道如何获取运行交互昵称、用户名和用户 ID 的用户?谢谢。
【问题讨论】:
标签: discord discord.js
我已经为此搜索了一段时间,但似乎找不到答案。有谁知道如何获取运行交互昵称、用户名和用户 ID 的用户?谢谢。
【问题讨论】:
标签: discord discord.js
您可以使用Interaction 的user/member 属性。
client.on('interactionCreate', async interaction => {
// Making sure the interaction is a command
if (!interaction.isCommand()) return false;
await interaction.reply(`Hello, ${interaction.user.tag}!`)
})
【讨论】:
interaction.user.tag 既不是昵称、用户名也不是 id。它是user.tag。您可能希望在答案中更改它以更好地回答实际问题。
我建议获取用户,然后获取您想要的所有信息。
const interactionUser = await interaction.guild.members.fetch(interaction.user.id)
const nickName = interactionUser.nickname
const userName = interactionUser.user.username
const userId = interactionUser.id
不明确地获取用户可能会导致信息丢失。
【讨论】:
没有必要为了获得运行交互的用户的昵称而进行另一个 fetch-request。交互发送一个GuildMember 实例,其中包含发送交互的用户的所有数据,可以通过interaction.member 访问。因此,在您的代码示例中,您可以这样做:
client.on('interactionCreate', async interaction => {
// Making sure the interaction is a command
if (!interaction.isCommand()) return false;
await interaction.reply(`Hello, ${interaction.member.displayName}!`)
})
【讨论】: