Search code examples
javascriptnode.jsdiscord.jssharding

UnhandledPromiseRejectionWarning while starting code while sharding


I am trying to run my index.js file which runs my bot.js file. When I did node index.js, I got this error. Index.js is sharding my bot.js file (discord.js)

(node:7284) UnhandledPromiseRejectionWarning: Error [SHARDING_READY_DIED]: Shard 0's process exited before its Client became ready.
    at Shard.onDeath (C:\Users\Sochum\Desktop\BobloxBot\node_modules\discord.js\src\sharding\Shard.js:158:16)
    at Object.onceWrapper (events.js:422:26)
    at Shard.emit (events.js:315:20)
    at Shard._handleExit (C:\Users\Sochum\Desktop\BobloxBot\node_modules\discord.js\src\sharding\Shard.js:384:10)
    at ChildProcess.emit (events.js:315:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
(node:7284) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 7)

It doesn't actually say what the error is. Here is my index.js code:

const { ShardingManager } = require('discord.js');
const manager = new ShardingManager('./bot.js', {
   totalShards:4,
   token: "#",
})

manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`));
manager.spawn();

Here is my bot.js file code https://pastebin.com/RW8abmKP

I am not sure why I am getting this error, but It might have something to do with the code starting in bot.js

new EconomyClient().start(require(`./config`).token, './commands', './index');

Solution

  • The spawnTimeout can be set to -1 or Infinity to prevent future errors: https://discord.js.org/#/docs/main/stable/class/ShardingManager?scrollTo=spawn. This can also happen if you call ShardingManager#spawn before attaching a listener to the shardCreate event which could create a race condition possibly preventing the shard 0 to perform a successful launch.