When using guild.member(id) in my users it's finding the user, but while using with my friend id (he is both online and in the same guild) the bot can't find his user and returns null.
The documentation says: "Returns the GuildMember form of a User object, if the user is present in the guild."
execute: async function (msg: Message, args: string[]) {
const id = args.pop();
const member = msg.guild.member(id);
console.log(member);
if (member) {
return msg.reply(' membro encontrado');
}
return msg.reply(' n茫o encontrei esse membro');
}
Further details:
What happened:

Console log in member:

This is not a bug, but expected behavior as Guild#member relies on cache.
It's really just a shortcut which resolves from the cache. If the member is not cached it can't be resolved. If you want to ensure you get the member if it is still in the guild <Guild>.members.fetch(<UserResolvable>) is the way to go. the returned promise will resolve with a GuildMember instance or reject if the member is not part of the guild.
Empty member cache can be an issue coming from #4961 - however you should investigate that topic and judge if you need privileged intents or not, should this be a possible cause.
If you need more help with djs usage for your use case please visit our discord server https://discord.gg/bRCvFy9
I recommend having this be specified in the documentation, this can keep happening with people that don't know about how discord.js works in the background (like me xD).
That aspect absolutely makes sense, am currently drafting a PR to address this!
Thanks :smile:
I probably should just add it to #4944
I don't quite see what that has to do with intents, wouldn't fit the scope of that merge decision
Having members not cached is much more likely without privileged intents, and that pr includes edits to #cache property descriptions saying they may not include all members (especially without the member and presence intents) and that it's better to fetch for getting members by id
go for it then, "Resolves a GuildMember from provided user based on the guilds member cache" or something along those lines to make very clear that it's cache dependent - we might want to consider removing Guild#member altogether in v13.
we might want to consider removing Guild#member altogether in v13.
4890 says hello
I knew i discussed that idea before with someone... should not late-night github.
Most helpful comment
This is not a bug, but expected behavior as Guild#member relies on cache.
https://github.com/discordjs/discord.js/blob/7ec0bd93b0d7788e8ccf5576add37f7198be3f72/src/structures/Guild.js#L646-L648
It's really just a shortcut which resolves from the cache. If the member is not cached it can't be resolved. If you want to ensure you get the member if it is still in the guild
<Guild>.members.fetch(<UserResolvable>)is the way to go. the returned promise will resolve with a GuildMember instance or reject if the member is not part of the guild.Empty member cache can be an issue coming from #4961 - however you should investigate that topic and judge if you need privileged intents or not, should this be a possible cause.
If you need more help with djs usage for your use case please visit our discord server https://discord.gg/bRCvFy9