2

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);
0

3 Answers 3

11

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;
5

Old post (I know) but commenting for those using v12,

const user = client.users.cache.find(user => user.username == "").id;

1

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
2
  • let id =client.users.get('name', 'DODDO').id; ^ TypeError: Cannot read property 'id' of undefined DODDO is my name.
    – 최재원
    Jul 8, 2018 at 6:05
  • client.user should be client.users and the get() method can only be used with an id. Jul 8, 2018 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.