Discord.js: Guild.member don't work sometimes

Created on 31 Oct 2020  路  10Comments  路  Source: discordjs/discord.js

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:

  • discord.js version: ^12.1.1
  • Node.js version: v15.0.1
  • Operating system: Ubuntu Server 18.04

What happened:
image

Console log in member:
image

  • [ ] I have also tested the issue on latest master, commit hash:
invalid

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

All 10 comments

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

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

4890 says hello

I knew i discussed that idea before with someone... should not late-night github.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smchase picture smchase  路  3Comments

Blumlaut picture Blumlaut  路  3Comments

PassTheMayo picture PassTheMayo  路  3Comments

BrandonCookeDev picture BrandonCookeDev  路  3Comments

LLamaFTL picture LLamaFTL  路  3Comments