首页 > 解决方案 > DiscordJS 和 Linux 的屏幕

问题描述

我遇到了 discordjs 的问题,但我不明白。我在 linux 屏幕中运行“node main.js”,分离它,进入睡眠状态,当我回来时,它崩溃了。你能启发我吗?

    # screen -r BotApex
(node:6559) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
events.js:180
    throw err; // Unhandled 'error' event
    ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ([object Object])
    at Client.emit (events.js:178:17)
    at WebSocketConnection.onError (/root/ApexLegendsSnipeBot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:374:17)
    at WebSocket.onError (/root/ApexLegendsSnipeBot/node_modules/ws/lib/event-target.js:128:16)
    at WebSocket.emit (events.js:189:13)
    at _receiver.cleanup (/root/ApexLegendsSnipeBot/node_modules/ws/lib/websocket.js:211:14)
    at Receiver.cleanup (/root/ApexLegendsSnipeBot/node_modules/ws/lib/receiver.js:557:13)
    at WebSocket.finalize (/root/ApexLegendsSnipeBot/node_modules/ws/lib/websocket.js:206:20)
    at TLSSocket.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)

标签: node.jsdiscord.js

解决方案


看起来您的机器人error在您离开时遇到了事件。如果您不处理错误事件,它们通常会使您的机器人崩溃。为了防止机器人崩溃,只需添加一个错误监听器:

client.on("error", info => {
  console.log('Error event:\n' + JSON.stringify(info));
  // handle the error here
});

请记住,错误事件已因某种原因被调用,您应该在之后处理该错误。


推荐阅读