Error fetching guild on client ready event

I have a function that will check for all guilds that have member count enabled However this specific group of code seems to always fail to fetch the guild, even tho when evaluating the same code it succeeds. This specific line const guild = await client.guilds.fetch(g.guild).catch(() => {}); always returns void or undefined. Even tho the provided guild ID is valid. This function is called from the ClientReady event.
import Client from '@/app';
import Guild from '@/models/Guild';
import { ChannelType } from 'discord.js';
import { schedule } from 'node-cron';

export default async (client: Client) => {
schedule('*/5 * * * *', async () => {
const guilds = await Guild.find({ 'memberCount.enabled': true });
for (const g of guilds) {
if (!g.memberCount.channel) return;
const guild = await client.guilds.fetch(g.guild).catch(() => {});
if (!guild) return;
const channel = await client.channels.fetch(g.memberCount.channel).catch(() => {});
if (!channel) return;
const members = await guild.members.fetch();
const filtered = members.filter((m) => !m.user.bot);
const ch = await channel.fetch();
if (ch.type !== ChannelType.GuildVoice) return;
await ch.edit({ name: g.memberCount.name ? g.memberCount.name.replace('{members}', filtered.size.toString()) : `Members: ${filtered.size}`, reason: 'Automated Action: Member Count Updated' });
}
});
};
import Client from '@/app';
import Guild from '@/models/Guild';
import { ChannelType } from 'discord.js';
import { schedule } from 'node-cron';

export default async (client: Client) => {
schedule('*/5 * * * *', async () => {
const guilds = await Guild.find({ 'memberCount.enabled': true });
for (const g of guilds) {
if (!g.memberCount.channel) return;
const guild = await client.guilds.fetch(g.guild).catch(() => {});
if (!guild) return;
const channel = await client.channels.fetch(g.memberCount.channel).catch(() => {});
if (!channel) return;
const members = await guild.members.fetch();
const filtered = members.filter((m) => !m.user.bot);
const ch = await channel.fetch();
if (ch.type !== ChannelType.GuildVoice) return;
await ch.edit({ name: g.memberCount.name ? g.memberCount.name.replace('{members}', filtered.size.toString()) : `Members: ${filtered.size}`, reason: 'Automated Action: Member Count Updated' });
}
});
};
12 Replies
d.js toolkit
d.js toolkit12mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Fallen
Fallen12mo ago
Discord.js: v14.11.0 Node.js: v18.13.0
treble/luna
treble/luna12mo ago
how many guilds do you have because you might get ratelimited by doing that
Fallen
Fallen12mo ago
My bot is in about 12 guilds right now, it is sharded if that causes an issue
treble/luna
treble/luna12mo ago
why is is sharded for 12 guilds
Fallen
Fallen12mo ago
Because im planning on advertising it but it's not done
treble/luna
treble/luna12mo ago
but seen the amount of fetch() calls you do, you're probably ratelimited listen to the client.rest.on('ratelimit') event
Fallen
Fallen12mo ago
Would it be better to pull from the cache? Instead of fetching everything
treble/luna
treble/luna12mo ago
if you have the guilds intent all guilds and channels should be cached and guilds have a .memberCount property
Fallen
Fallen12mo ago
I need to filter the members to remove bots
treble/luna
treble/luna12mo ago
if you're gonna fetch the members for every guild, that is gonna cause a ratelimit
Fallen
Fallen12mo ago
I just rewrote that function to be cache based
schedule('*/5 * * * *', async () => {
const guilds = await Guild.find({ 'memberCount.enabled': true });
for (const g of guilds) {
if (!g.memberCount.channel) return;
const guild = client.guilds.cache.get(g.guild);
if (!guild) return;
const channel = client.channels.cache.get(g.memberCount.channel);
if (!channel || channel.type !== ChannelType.GuildVoice) return;
await channel.edit({ name: g.memberCount.name ? g.memberCount.name.replace('{members}', guild.memberCount.toString()) : `Members: ${guild.memberCount}`, reason: 'Automated Action: Member Count Updated' });
}
});
schedule('*/5 * * * *', async () => {
const guilds = await Guild.find({ 'memberCount.enabled': true });
for (const g of guilds) {
if (!g.memberCount.channel) return;
const guild = client.guilds.cache.get(g.guild);
if (!guild) return;
const channel = client.channels.cache.get(g.memberCount.channel);
if (!channel || channel.type !== ChannelType.GuildVoice) return;
await channel.edit({ name: g.memberCount.name ? g.memberCount.name.replace('{members}', guild.memberCount.toString()) : `Members: ${guild.memberCount}`, reason: 'Automated Action: Member Count Updated' });
}
});
nevermind already found an error in that Whenever this function executes getting the guild from the cache is still returning undefined