From efc347fea8c1a7dc376924b9128bb2d1b5b151ec Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 14 Apr 2019 23:40:52 -0400 Subject: [PATCH] Should fix crash. --- core/bootstrap.js | 4 ++++ core/client.js | 28 ++++++++++++++++++++++++++-- core/utils.js | 1 + plugins/ngl/ngl.js | 22 +++++++++++++--------- 4 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 core/utils.js diff --git a/core/bootstrap.js b/core/bootstrap.js index fc7b32e..2659a1c 100644 --- a/core/bootstrap.js +++ b/core/bootstrap.js @@ -4,6 +4,10 @@ const logger = require('./logger'); core.hook('logger', logger); +const utils = require('./utils'); + +core.hook('utils', utils); + const client = require('./client'); core.hook('client', client); diff --git a/core/client.js b/core/client.js index c13b1ed..ac50109 100644 --- a/core/client.js +++ b/core/client.js @@ -1,16 +1,40 @@ const Discord = require('discord.js'); const { log } = require('@bot').logger; +const { sleep } = require('@bot').utils; const client = new Discord.Client(); const key = process.env.DISCORD_KEY; client.on('ready', () => { - client.user.setActivity('cowsay'); + client.user.setActivity('Web Development'); log('notify', 'Bot Started'); }); -client.login(key); +const tryLogin = async () => { + try { + const t = await client.login(key); + if (t !== key) { + log('Notify', 'Trying To Recover'); + await sleep(500); + await tryLogin(); + } + } catch (err) { + if (err.code === 'ENOTFOUND') { + log('Notify', 'Trying To Recover'); + await sleep(500); + await tryLogin(); + } + } +}; + +client.on('error', (error) => { + if (error.message === 'read ECONNRESET' || error.message === 'getaddrinfo ENOTFOUND') { + tryLogin(); + } +}); + +tryLogin(); module.exports.discord = Discord; module.exports.client = client; diff --git a/core/utils.js b/core/utils.js new file mode 100644 index 0000000..4649cb5 --- /dev/null +++ b/core/utils.js @@ -0,0 +1 @@ +exports.sleep = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds)); diff --git a/plugins/ngl/ngl.js b/plugins/ngl/ngl.js index c713aff..8468686 100644 --- a/plugins/ngl/ngl.js +++ b/plugins/ngl/ngl.js @@ -13,15 +13,19 @@ client.on('message', async (msg) => { }); commands.register(this.command, '', '', '', async (msg) => { - generator({ - filename: path.join(__dirname, 'ngl.txt'), - model: { - maxLength: 100, - minLength: 50, - }, - }, (err, sentence) => { - msg.channel.send(sentence); - }); + try { + generator({ + filename: path.join(__dirname, 'ngl.txt'), + model: { + maxLength: 100, + minLength: 50, + }, + }, (err, sentence) => { + msg.channel.send(sentence); + }); + } catch (error) { + console.log(error); + } }); exports.name = 'Naturl Language Generator';