timotejroiko / discord.js-light

All the power of discord.js, zero caching. This library modifies discord.js's internal classes and functions in order to give you full control over its caching behaviour.
Apache License 2.0
292 stars 29 forks source link

Question #32

Closed dylanjamesdev closed 3 years ago

dylanjamesdev commented 3 years ago

Hi there! I really love this wrapper.

Could you possibly explain why I'm unable to read usernames from this code? When console logging, it shows the user info but when putting it into code it does not.

Test:

const test = await req.app.get("client").users.fetch("359498825150365699");
console.log(test);

Code: (EJS)

<%= await discord_data.users.fetch(i.userID).username ? discord_data.users.fetch(i.userID).username : "Not In Guild" %> 

Notes: discord_data is the discord.js light client. i.userID is a user ID string from my database.

MaximKing1 commented 3 years ago

Hey There, Could I please see your client options?

dylanjamesdev commented 3 years ago
const client = new discord.Client({
  cacheGuilds: true,
  cacheChannels: true,
  cacheOverwrites: false,
  cacheRoles: true,
  cacheEmojis: true,
  cachePresences: false,
  fetchAllMembers: true
});
timotejroiko commented 3 years ago

you cannot access a value from a promise like that, the awaiting part needs to be isolated:

(await discord_data.users.fetch(i.userID)).username

also, you dont have any error handling there, you're not awaiting the second fetch and a second fetch is not needed. just do it like this:

await discord_data.users.fetch(i.userID).then(u => u.username).catch(() => "Not in Guild")

dylanjamesdev commented 3 years ago

Thank you so much :)

MaximKing1 commented 3 years ago

So on the package is has this

Besides the bot user, all other Users and Members are never automatically cached. Having an incomplete user cache is not very useful most of the time, so we prefer an all-or-nothing approach. The fetchAllMembers client option can be used to cache all Users and Members, otherwise they must be manually fetched if required. Events that include some User and/or Member data usually do not require fetching as the event itself already contains enough information to provide a complete User and/or Member object.

Try fetching the user manually and tell me how that goes..

dylanjamesdev commented 3 years ago

Heard thank you both, I will get back to you in a few moments