From 65d2745cdc6bbfeb9a04690b6b4ceda27a2d41bd Mon Sep 17 00:00:00 2001 From: benji7425 Date: Thu, 7 Dec 2017 00:19:16 +0000 Subject: [PATCH] Revert removal of restart on unhandled exception Turns out maybe it is actually somewhat useful after all --- discord-bot-core/Client.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/discord-bot-core/Client.js b/discord-bot-core/Client.js index 42663ff..4f70020 100644 --- a/discord-bot-core/Client.js +++ b/discord-bot-core/Client.js @@ -32,6 +32,7 @@ module.exports = class Client extends Discord.Client { this.on("debug", this._onDebug); this.on("guildCreate", this._onGuildCreate); this.on("guildDelete", this._onGuildDelete); + process.on("uncaughtException", err => this._onUnhandledException(this, err)); } bootstrap() { @@ -70,6 +71,15 @@ module.exports = class Client extends Discord.Client { CoreUtil.dateLog(`Removed from guild ${guild.name}, removing data for this guild`); } + + _onUnhandledException(client, err) { + CoreUtil.dateError("Unhandled exception!\n", err); + CoreUtil.dateLog("Destroying existing client..."); + client.destroy().then(() => { + CoreUtil.dateLog("Client destroyed, recreating..."); + setTimeout(() => client.login(client._token), InternalConfig.reconnectTimeout); + }); + } }; function compactCollections() {