diff --git a/src/config.js b/src/config.js index 0ac83cb..91af8ba 100644 --- a/src/config.js +++ b/src/config.js @@ -7,7 +7,8 @@ module.exports = { "MONGODB_URI": testmode ? "mongodb://localhost/k4t" : process.env.MONGODB_URI, "webhooks": { - "welcome": process.env.WEBHOOK_WELCOME.split("/"), + "welcome": process.env.WEBHOOK_WELCOME.split("/"), + "error": process.env.WEBHOOK_ERROR.split("/") }, "opID": "281134216115257344", diff --git a/src/main.js b/src/main.js index 302a3f8..6e05986 100755 --- a/src/main.js +++ b/src/main.js @@ -6,6 +6,23 @@ global.Discord = require('discord.js'); global.fs = require('fs'); global.dClient = new Discord.Client({ disableEveryone: true }); +// error handling +{ + let webhook = new Discord.WebhookClient(config.webhooks.error[0], config.webhooks.error[1]); + global.onError = function logError(error) { + let msg = error.stack || error.message || error; + console.error(msg); + try { + webhook.send(`\`\`\`\n${msg}\n\`\`\``).catch(()=>{}); + } catch(e) {} + } + process.on('unhandledRejection', onError); + process.on('uncaughtException', onError); + dClient.on('error', onError); + dClient.on('warn', onError); +} + + global.dbClient = new (require('pg').Client)({ connectionString: process.env.DATABASE_URL, ssl: !testmode, @@ -30,6 +47,4 @@ dClient.once('ready', () => { require('./screenshotter'); require('./misc'); -}); -dClient.on('error', console.error); -dClient.on('warn', console.warn); \ No newline at end of file +}); \ No newline at end of file diff --git a/src/util.js b/src/util.js index 4c41fc1..a59d538 100644 --- a/src/util.js +++ b/src/util.js @@ -1,10 +1,3 @@ -process.on('unhandledRejection', error => { - console.error(error.stack); -}); -process.on('uncaughtException', error => { - console.error(error.stack); -}); - Array.prototype.random = function () { return this[Math.floor(Math.random() * this.length)] } \ No newline at end of file