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