【问题标题】:How to get user who ran interaction如何获取运行交互的用户
【发布时间】:2022-10-17 00:53:13
【问题描述】:

我已经为此搜索了一段时间,但似乎找不到答案。有谁知道如何获取运行交互昵称、用户名和用户 ID 的用户?谢谢。

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    您可以使用Interactionuser/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。您可能希望在答案中更改它以更好地回答实际问题。
    【解决方案2】:

    我建议获取用户,然后获取您想要的所有信息。

    const interactionUser = await interaction.guild.members.fetch(interaction.user.id)
    
    const nickName = interactionUser.nickname
    const userName = interactionUser.user.username
    const userId = interactionUser.id
    

    不明确地获取用户可能会导致信息丢失。

    【讨论】:

    • 这绝对是要走的路,因为一次获取​​所有信息更容易。
    【解决方案3】:

    没有必要为了获得运行交互的用户的昵称而进行另一个 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}!`)
    })
    

    【讨论】:

      猜你喜欢
      • 2015-04-03
      • 2021-09-04
      • 2011-10-24
      • 1970-01-01
      • 2010-11-21
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多