问题
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