【问题标题】:Discord.js how to get user id just from the usernameDiscord.js 如何仅从用户名中获取用户 ID
【发布时间】:2020-06-12 03:55:42
【问题描述】:

这不起作用: client.users.cache.get(`${args[0]}`).id;

我应该怎么做才能修复它?我在 v12 上

【问题讨论】:

标签: javascript node.js discord discord.js


【解决方案1】:

试试

matching_client = client.users.cache.filter(user => user.username === args[0]);

matching_client 是与该用户名匹配的用户集合

相关文档链接:

https://discord.js.org/#/docs/collection/master/class/Collection?scrollTo=filter

【讨论】:

  • 这行得通,但我该如何让它只使用 id?我试过votefor = message.client.users.cache.filter(user => user.username === args[0]); id = votefor.id; 但返回未定义
  • 你必须从集合中获取第一个元素,试试id = votefor.array()[0].id
  • 这有点没必要,为什么不只是用户<Collection>.find()?这样您就不必将其转换为数组,也不必按 0 对其进行索引。
猜你喜欢
  • 2023-04-11
  • 2021-08-11
  • 2020-07-05
  • 2021-06-03
  • 1970-01-01
  • 2020-07-31
  • 2018-08-21
  • 2021-05-07
  • 2014-12-18
相关资源
最近更新 更多