#[RESOLVED] Client Cache Undefined

1 messages · Page 1 of 1 (latest)

peak ginkgo
#

@pastel glen

pastel glen
#

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

peak ginkgo
#

Can you send us your code, sensitive content removed preferably

slow stump
#

not all member is cached

pastel glen
#

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

slow stump
#

need .forEach

pastel glen
#

Oh i eventually did get "members didnt arrive in time"

#

@peak ginkgo that code above is all my code

peak ginkgo
pastel glen
#

console.log(client.guilds.cache.get(interaction.guild.id));
peak ginkgo
pastel glen
#

the bot has iniated

#

initiated, it's a command im running

#

with the bot logged in

peak ginkgo
#

... are you trying to access the cache outside of an event

#

such as ready or messageCreate or interactionCreate

pastel glen
#

no it's in the interactionCreate event

peak ginkgo
pastel glen
#

that's not literally all my code

#

I have commands set up properly

peak ginkgo
#

i know

pastel glen
#

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

slow stump
#

client.guilds.fetch().members?

peak ginkgo
pastel glen
#

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'
}```
peak ginkgo
peak ginkgo
#

it's an asynchronous function because fetch() returns a promise that must be resolved

pastel glen
peak ginkgo
pastel glen
#

what token

peak ginkgo
#

your bots token

#

you know, the thing from the developer portal you use to login

pastel glen
#

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

peak ginkgo
#

because you're probably trying to access the cache before the bot is ready darkSweat

pastel glen
#

I'm not i promise

peak ginkgo
#

can you just send the lot in a sourcebin with sensitive content removed then

pastel glen
#
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

pastel glen
#
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'
}
peak ginkgo
#

Do you have the GUILD_MEMBERS intent in your client intents, or on the Developer Portal

pastel glen
#

is that this

slow stump
#

not that

peak ginkgo
#

I think we found the issue

pastel glen
#

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

slow stump
peak ginkgo
pastel glen
#

ah

#

i figured it was something in the portal

#

what is prescense intent

peak ginkgo
#

ignore it, i just have that image on standby for these issues

pastel glen
#

do i need to reinvite my bot to get that new intent?

peak ginkgo
#

no

pastel glen
#

ok let me test it now

peak ginkgo
#

const client = require('./index.js')

pastel glen
#

and then i move my client to index.js?

#

also i still get the members timeout

peak ginkgo
#

or whatever your main file is called where you first made the client

pastel glen
#

even this: await client.guilds.fetch(); gives me the error Request to use token, but token was unavailable to the client.

peak ginkgo
pastel glen
#

god dammit

#

that's what it is

slow stump
peak ginkgo
#

and did you require the client in the leaderboard command

peak ginkgo
pastel glen
#

so when i did the require index.js my client was {}

#

but i just changed it back and i get

peak ginkgo
#

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) {
pastel glen
#

do i even need to use the client object

peak ginkgo
#

this will parse your existing client class into your new command, which actually did wait until the bot initialised

pastel glen
#

can i not just do interaction.guild.members.fetch() or something

peak ginkgo
pastel glen
#

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

peak ginkgo
#

"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

pastel glen
#

this is the code im trying

const guildMembers = await interaction.guild.members.fetch()
        console.log(guildMembers ?? 'Error');
peak ginkgo
#

can you send your code again

#

all of it again plz

pastel glen
#

ok that looks like it works now

peak ginkgo
pastel glen
#

it's getting all my members now, even the offline ones

peak ginkgo
#

You really need to watch some videos on JavaScript fundamentals or take an online course thou. This is DJS help, not JS help

slow stump
#

and

peak ginkgo
#

^^^^^^^^^^^^

pastel glen
#

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

peak ginkgo
pastel glen
#

yes that too

slow stump
#

time to archive the thread

pastel glen
#

thank u guys

peak ginkgo
peak ginkgo