【问题标题】:UnhandledPromiseRejectionWarning while starting code while sharding分片时启动代码时出现 UnhandledPromiseRejectionWarning
【发布时间】:2021-07-21 18:24:52
【问题描述】:

我正在尝试运行我的 index.js 文件,该文件运行我的 bot.js 文件。当我做 node index.js 时,我得到了这个错误。 Index.js 正在分片我的 bot.js 文件 (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)

它实际上并没有说明错误是什么。这是我的 index.js 代码:

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();

这是我的 bot.js 文件代码 https://pastebin.com/RW8abmKP

我不确定为什么会收到此错误,但它可能与以 bot.js 开头的代码有关

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

【问题讨论】:

    标签: javascript node.js discord.js sharding


    【解决方案1】:

    spawnTimeout 可以设置为-1Infinity 以防止将来出现错误: https://discord.js.org/#/docs/main/stable/class/ShardingManager?scrollTo=spawn。如果您在将侦听器附加到 shardCreate 事件之前调用 ShardingManager#spawn 也会发生这种情况,这可能会产生竞争条件,可能会阻止分片 0 执行成功启动。

    【讨论】:

    • 你如何生成超时?
    • 我的意思是说*你使用
    • const manager = new ShardingManager('./bot.js', { totalShards:4, token: "#", spawnTimeout: -1 })
    猜你喜欢
    • 1970-01-01
    • 2022-08-07
    • 2021-08-05
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2017-09-16
    • 2018-03-13
    相关资源
    最近更新 更多