How to find a user by username in the client.users collection

社会主义新天地 提交于 2021-01-06 04:55:07

问题


I want to give that user role by name. So I want to get id by username.

let role = message.guild.roles.find('name', 'TESTrole');
let id = client.users.get("name", "TESTname"); 
let member = message.member.guild.members.get(id);
member.addRole(role);

回答1:


You can use the find() method.

let user = client.users.find("username", "TESTname");
//OR
let user = client.users.find(user => user.username == "TESTname");
//once you've found the user you can get the id (or you can write .id after the find method)
let id = user.id;



回答2:


This

let id = client.user.get('name', 'name here').id;

should get you the results you're looking for. See the documentation for clarification of the properties you can search by and view, including id:

.id
  The ID of the user

  Type: Snowflake


来源:https://stackoverflow.com/questions/51229193/how-to-find-a-user-by-username-in-the-client-users-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!