#[RESOLVED] Client Cache Undefined
1 messages · Page 1 of 1 (latest)
hello
Also I'm trying to get every member, including offline members, so I don't think cache is the right way to do it
it looks like i need to use fetch
Can you send us your code, sensitive content removed preferably
cache is only for a few
not all member is cached
This is from the guilds.members documentation
// Fetch all members from a guild
guild.members.fetch()
.then(console.log)
.catch(console.error);
that ends up returning nothing
no log or error
need .forEach
Oh i eventually did get "members didnt arrive in time"
@peak ginkgo that code above is all my code
you haven't parsed anything into the log method in console.log as well
console.log(client.guilds.cache.get(interaction.guild.id));
Again, this is running before the bot has initiated. Try putting that inside of the ready event
client.once('ready', () => {
// Your code to check the cache
}
Do this after constructing client
... are you trying to access the cache outside of an event
such as ready or messageCreate or interactionCreate
no it's in the interactionCreate event
Hard to tell because you defined client and then immediately tried to access its cache before it had initialised
i know
the console.log is in the slashcommandbuilder
I have the correct framework set up as shown by the discord.js tutorial, all my other commands work properly, i'm in the interactioncreate event, but fetch nor cache work properly
client.guilds.fetch().members?
try this inside an async function
// Fetch all members from a guild
const guildMembers = await guild.members.fetch()
console.log(guildMembers ?? 'Error');
just did that, I get, or well i did the sync version, I get
GuildMemberManager.js:435
reject(new Error('GUILD_MEMBERS_TIMEOUT'));
^
Error [GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time.
at Timeout._onTimeout (C:\Users\lambo\Desktop\DiscordBots\PokerBot\node_modules\discord.js\src\managers\GuildMemberManager.js:435:16)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
[Symbol(code)]: 'GUILD_MEMBERS_TIMEOUT'
}```
the returned value for fetch() is undefined, so that wont do anything
yes, so do async
it's an asynchronous function because fetch() returns a promise that must be resolved
HTTPError [DiscordjsError]: Request to use token, but token was unavailable to the client.
your token is invalid

what token
how would my bot be ablet o log in if the token is invalid?
might i be missing a permission or something on my bot?
I've used fetch to get messages
because you're probably trying to access the cache before the bot is ready 
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
client.commands = new Collection();
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(token);
index.js straight off discord.js tutorial
const { SlashCommandBuilder } = require('@discordjs/builders');
const { Client, Intents, MessageEmbed, MessageActionRow, MessageButton, MessageAttachment } = require('discord.js');
const chalk = require('chalk');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_MEMBERS], partials: ['CHANNEL'] });
module.exports = {
data: new SlashCommandBuilder()
.setName('leaderboard')
.setDescription('Display leaderboards of how many chips everyone in the server has.'),
async execute(interaction) {
console.log(chalk.yellowBright("[COMMAND]") + chalk.magentaBright("[" + interaction.user.username + "]") + " Used /leaderboard");
const guildMembers = await interaction.guild.members.fetch();
console.log(guildMembers ?? 'Error');
},
};
there's all the relevant code
Does this log anything at all
Error [GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
[Symbol(code)]: 'GUILD_MEMBERS_TIMEOUT'
}
Do you have the GUILD_MESSAGES intent turned on in the Developer Portal
Do you have the GUILD_MEMBERS intent in your client intents, or on the Developer Portal
not that
I think we found the issue
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_MEMBERS], partials: ['CHANNEL'] });
these are my client intents
I'm assuming i need members.read
ignore it, i just have that image on standby for these issues
do i need to reinvite my bot to get that new intent?
no
ok let me test it now
I just realised... you don't need to create a new client everytime your code runs
const client = require('./index.js')
or whatever your main file is called where you first made the client
even this: await client.guilds.fetch(); gives me the error Request to use token, but token was unavailable to the client.
also did you update your clients intents in index.js?
log the client
I need the log
and did you require the client in the leaderboard command
yeah do this too
so when i did the require index.js my client was {}
but i just changed it back and i get
yeah when you change it back you made a new client
so you've imported it incorrectly. this isnt a djs error
you cant keep making new clients. you need to construct one in your main file at the very top, and then parse that into every other command
Do this in index.js
try {
await command.execute(interaction, client);
}
And do this in the leaderboard file
async execute(interaction, client) {
do i even need to use the client object
this will parse your existing client class into your new command, which actually did wait until the bot initialised
can i not just do interaction.guild.members.fetch() or something
Yep you 100% can, but you also can't keep making new clients
i dont use my client object in any of my commands anyway so i will fix that
but I'm jsut trying to get the members list at the moment
and interaction.guild.members.fetch() doesn't work
"fix it" by adding it as a parameter to command.execute() and import it when necessary, because you will eventually need it, and making new client will never work
await
this is the code im trying
const guildMembers = await interaction.guild.members.fetch()
console.log(guildMembers ?? 'Error');
ok that looks like it works now

it's getting all my members now, even the offline ones
You really need to watch some videos on JavaScript fundamentals or take an online course thou. This is DJS help, not JS help
^^^^^^^^^^^^
this sorta seemed like i just didnt have the members intent on tho
didnt even have to do with the code
but yeah im doing pretty scuffed djs i suppose
You're doing pretty scuffed JavaScript
yes that too
time to archive the thread
thank u guys

np. good luck in future