【问题标题】:How can i get mentioned user's username in discord.js我如何在 discord.js 中获得提及用户的用户名
【发布时间】:2020-04-02 18:16:02
【问题描述】:

阅读标题,如果有人可以帮助我,我会很高兴

例如messages.mentions.first()。像这样,提到的用户名有什么用吗?

【问题讨论】:

  • 欢迎来到 StackOverflow!你尝试了哪些努力?它看起来不太清楚,因为它没有提供示例。

标签: discord.js


【解决方案1】:

message.mentions.members.first().user.username;

这将为您提供第一次提及的用户名。

你也可以message.mentions.users.first().username;

【讨论】:

    【解决方案2】:

    最好的方法是使用message.mentions.users.first(),因为这实际上会给您消息中提到的第一个用户。 (message.mentions 不是collection)。如果没有提到用户,第一个将导致undefined,因此您可能需要检查它。拥有用户后,您将需要访问 username 属性。

    例子:

    // Assuming we just need a message event
    client.on('message', (message) => {
      const user = message.mentions.users.first();
      if (user === undefined) {
        return; // Do not proceed, there is no user.
      }
      const name = user.username;
      // Do stuff with the username
    })
    

    来源:
    message.mentions
    message.mentions.users

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 2020-09-06
      • 2019-10-19
      • 1970-01-01
      • 2021-12-03
      • 2021-07-04
      • 2021-05-23
      • 1970-01-01
      相关资源
      最近更新 更多