Reputation: 13
client.on("message", async message =>{
message.guild.members.fetch({query: "username here", limit: 1})
.then( members => console.log(members.user.id)) //results in undefined
.catch(console.error);
}
I would like to be able to get a users ID through their username. In this case I use the "fetch()" method and a query within it. However when i try to log (members.user.id) I get "undefined". The thing is whenever i fetch by a user ID(see code below) I am able to access the users ID,username, etc.. How would I able to fetch by their username and then be able to get a users ID. I was using this as a reference point https://discord.js.org/#/docs/main/master/class/GuildMemberManager?scrollTo=fetch
If theirs an easier way to get a users ID by their username please do tell.
client.on("message", async message =>{
message.guild.members.fetch("userID here")
.then( members => console.log(members.user.username))// a username is logged
.catch(console.error);
}
Upvotes: 1
Views: 994
Reputation:
Looks like even if the limit is 1 it will still give back a collection, so you will need to get the first member of that collection
message.guild.members.fetch({query: "username here", limit: 1})
.then(members => {
const member = members.first();
//no need to convert it into a user
console.log(member.id);
});
Upvotes: 2