Caching Problems

Hey, i have some problems with caching, on discord.js they sad All Guilds are cached if you have the intent, and the client is ready. While using intents like GuildMembers & Guilds. i dont know if it fits into this forum, i tough because sapphire initates the listeners.... sometimes when i do this here it doesnt show up some guild... and when i restart the application it doesnt show up.
this.container.client.guilds.cache.filter(x => !x.members.cache.get(message.author.id)).forEach(guild => {
selectMenu.addOptions(new StringSelectMenuOptionBuilder()
.setLabel(guild.name)
.setEmoji({ id: getEmojiIdByName(this.container.client, DefaultEmojis.HELIX_ARROW) })
.setValue(guild.id.toString())
);
});
this.container.client.guilds.cache.filter(x => !x.members.cache.get(message.author.id)).forEach(guild => {
selectMenu.addOptions(new StringSelectMenuOptionBuilder()
.setLabel(guild.name)
.setEmoji({ id: getEmojiIdByName(this.container.client, DefaultEmojis.HELIX_ARROW) })
.setValue(guild.id.toString())
);
});
with this one i have no problems
const guilds = await Promise.all((await this.container.client.guilds.fetch()).map((guild) => guild.fetch()));

guilds.forEach(async (guild) => {
selectMenu.addOptions(
new StringSelectMenuOptionBuilder()
.setLabel(guild.name)
.setEmoji({ id: getEmojiIdByName(this.container.client, DefaultEmojis.HELIX_ARROW) })
.setValue(guild.id.toString())
);
});
const guilds = await Promise.all((await this.container.client.guilds.fetch()).map((guild) => guild.fetch()));

guilds.forEach(async (guild) => {
selectMenu.addOptions(
new StringSelectMenuOptionBuilder()
.setLabel(guild.name)
.setEmoji({ id: getEmojiIdByName(this.container.client, DefaultEmojis.HELIX_ARROW) })
.setValue(guild.id.toString())
);
});
7 Replies
f1fty
f1fty3mo ago
or is maybe tsc-watch the cause?
secre
secre2mo ago
I think the problem is that you trying to filter by id in members.cache. User can be on the guild but didn't do anything so he isn't in cache. I think you need to loop trough all guilds and manually fetch member for each guild and then if member was found - add that guild as an option for your select menu Second code works as needed because you just fetch all guilds and that's it. You don't rely on member cached in members property of guild
f1fty
f1fty2mo ago
mhm but i do much on server before adding option to selectmenu ill try to test smth
secre
secre2mo ago
I mean it's not like your bot need to do something, it's more about member need to be in guild's cache. You have 3 ways to do so: 1. Fetch all guild members 2. Fetch user by id 3. Let user do something that will put him in that cache. This is impossible for me because for 100% accuracy you will need to have user active on all guilds that he share with your bot :D
f1fty
f1fty2mo ago
Ah yes second code doesnt filter forgot that mean as a user
secre
secre2mo ago
Wait, why do you have ! when filtering guilds? .filter(x => !x.members.cache.get(message.author.id)) or is it intended? Maybe that was a case
f1fty
f1fty2mo ago
filter undefined