discord.js: Timeout - Shards With Unavailable Guilds Not Starting

Please describe the problem you are having in as much detail as possible: When I start my bot sharded bot (in 4240 servers), one of the shards times out and stops starting. This issue is persistent across all versions of djs, including 12. The timeout happens around 90% of the time I start the shards. Here are the errors and messages I receive in the console for that shard.

Error: Shard 1's Client took too long to become ready. Error: Something took too long to do. Left a guild called undefined.

The last message is me logging the guild leave event, which also seems to be firing from that specific shard.

Include a reproducible code sample here, if possible:

My Sharding File (Code for version 11):

const { ShardingManager } = require('discord.js');
const manager = new ShardingManager('./bot.js', { token: process.env.TOKEN });

manager.spawn(20); // Start 4 Shards
manager.on('launch', shard => console.log("Sharding Manager ➜".bold.brightYellow, `Launched shard ${shard.id}`.bold.brightCyan));

Code for Ready event:

client.login(process.env.TOKEN).catch(console.error)

client.on('ready', (i, callback) => {
    functions.log(`Online in ${client.guilds.size} servers!`)

    blapi.handle(client, apiKeys, 10);

    setTimeout(() => {
        /* Bot Presence (Game and Status) */
        client.shard.fetchClientValues('guilds.size').then(results => {

            let servercount = results.reduce((prev, guildCount) => prev + guildCount, 0)

            client.user.setPresence({
                game: {
                type: 0,
                name: game + " | " + servercount
                //name: 'Discord API Error ➜ https://status.discordapp.com'
                },
                status: status
            });

            if (client.shard.id === 3) {
                Glenn.updateStats(servercount, client.shard.count);
                functions.postStats(client)
                functions.log(`Posted ${servercount} servers!`)
            }
        })
    },60000);
});

The error is persistent across different IP addresses and computers, and I get no email from discord.

Further details:

  • discord.js version: 11.4.2, 11.5.1, 11.6.1, 12, 12.0.2
  • Node.js version: v12.15.0
  • Operating system: Ubuntu & Mac
  • Priority this issue should have – please be realistic and elaborate if possible: Not sure…
  • I have also tested the issue on latest master, commit hash:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 77 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I fixed this problem for my shard.js 😇🥳

const settings = require('./settings.json');
const { ShardingManager } = require('discord.js')

const manager = new ShardingManager('./bot.js', {
    totalShards: "auto", // or number e.g ( totalShards: 10 )
    token: settings.token, // or token e.g ( token: "YOUR BOT TOKEN" )
    spawnTimeout: -1,
    respawn: true
});
manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`));

manager.spawn(manager.totalShards, 5500, -1).catch(e => console.log(e))

The workaround for this issue is to manually run triggerReady(). You can do it on a timer like this for example (for v11):

setTimeout(() => {
    client.ws.connection.triggerReady()
},30000)

This will force it to become ready after 30 seconds no matter what. Not sure if this is the best way to do it, but it seemed to work for many people.

where should i put this

anywhere in your bot’s main file, ie:

let client = new Discord.Client();

setTimeout(() => {
    client.ws.connection.triggerReady()
},30000)

client.login();

+1

I was suggested this potential solution in the discord.js server. Consider attempting client.login() as early as possible (before your database init., etc.)

For me, this resolved the issue.

Haven’t seen any of these issues in DJS v12