From 47a1cfcce363648e005e16eb4b7198ba4ae0bee9 Mon Sep 17 00:00:00 2001 From: Ninjdai1 Date: Mon, 4 Sep 2023 10:51:57 +0200 Subject: [PATCH] Add error catching and logging Logs to discord using webhook --- config.example.json | 3 +- events/error.js | 19 +++++++++ functions.js | 98 +++++++++++++++++++++++++++++++++++++++++++-- index.js | 5 ++- 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 events/error.js diff --git a/config.example.json b/config.example.json index 947be66..d632040 100644 --- a/config.example.json +++ b/config.example.json @@ -11,5 +11,6 @@ "sequelizeCredentials": { "username": "user", "password": "password" - } + }, + "errorWebhookURL": "https://discord.com/api/webhooks/123456789/aBcDeFgHiJkLmNoPqRsTuVwXyZ" } \ No newline at end of file diff --git a/events/error.js b/events/error.js new file mode 100644 index 0000000..87b98bf --- /dev/null +++ b/events/error.js @@ -0,0 +1,19 @@ +const { EmbedBuilder } = require("discord.js"); +const { inspect } = require("util"); + +const errorEmbed = new EmbedBuilder().setColor("Red"); + +module.exports = { + name: 'error', + async execute() { + errorEmbed + .setTitle("Discord API Error") + .setURL("https://discordjs.guide/popular-topics/errors.html#api-errors") + .setDescription( + `\`\`\`${inspect(err, { depth: 0 }).slice(0, 1000)}\`\`\`` + ) + .setTimestamp(); + + return client.errorCatcherWebhook.send({ embeds: [errorEmbed] }); + }, +}; \ No newline at end of file diff --git a/functions.js b/functions.js index 14053b4..300641c 100644 --- a/functions.js +++ b/functions.js @@ -1,8 +1,10 @@ const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v10'); -const { token, clientId, guildId, sequelizeCredentials } = require('./config.json'); -const { Collection } = require('discord.js'); +const { token, clientId, guildId, sequelizeCredentials, errorWebhookURL } = require('./config.json'); +const { Collection, WebhookClient, EmbedBuilder, Client } = require('discord.js'); +const { inspect } = require("util"); const fs = require('fs'); + const rest = new REST({ version: '10' }).setToken(token); const Sequelize = require('sequelize'); @@ -96,4 +98,94 @@ function loadDatabase(client) { artists.sync(); } -module.exports = { deploy_commands, loadDatabase } +function loadErrorCatcher(client) { + client.errorCatcherWebhook = new WebhookClient({ + url: errorWebhookURL, + }); + + process.on("unhandledRejection", (reason, promise) => { + console.log(reason, "\n", promise); + + const errorEmbed = new EmbedBuilder() + .setColor("Red") + .setTitle("Unhandled Rejection/Catch") + .setURL("https://nodejs.org/api/process.html#event-unhandledrejection") + .addFields( + { + name: "Reason", + value: `\`\`\`${inspect(reason, { depth: 0 }).slice(0, 1000)}\`\`\``, + }, + { + name: "Promise", + value: `\`\`\`${inspect(promise, { depth: 0 }).slice(0, 1000)}\`\`\``, + } + ) + .setTimestamp(); + + return client.errorCatcherWebhook.send({ embeds: [errorEmbed] }); + }); + + process.on("uncaughtException", (err, origin) => { + console.log(err, "\n", origin); + + const errorEmbed = new EmbedBuilder() + .setColor("Red") + .setTitle("Uncaught Exception/Catch") + .setURL("https://nodejs.org/api/process.html#event-uncaughtexception") + .addFields( + { + name: "Error", + value: `\`\`\`${inspect(err, { depth: 0 }).slice(0, 1000)}\`\`\``, + }, + { + name: "Origin", + value: `\`\`\`${inspect(origin, { depth: 0 }).slice(0, 1000)}\`\`\``, + } + ) + .setTimestamp(); + + return client.errorCatcherWebhook.send({ embeds: [errorEmbed] }); + }); + + process.on("uncaughtExceptionMonitor", (err, origin) => { + console.log(err, "\n", origin); + + const errorEmbed = new EmbedBuilder() + .setColor("Red") + .setTitle("Uncaught Exception Monitor") + .setURL( + "https://nodejs.org/api/process.html#event-uncaughtexceptionmonitor" + ) + .addFields( + { + name: "Error", + value: `\`\`\`${inspect(err, { depth: 0 }).slice(0, 1000)}\`\`\``, + }, + { + name: "Origin", + value: `\`\`\`${inspect(origin, { depth: 0 }).slice(0, 1000)}\`\`\``, + } + ) + .setTimestamp(); + + return client.errorCatcherWebhook.send({ embeds: [errorEmbed] }); + }); + + process.on("warning", (warn) => { + console.log(warn); + + const errorEmbed = new EmbedBuilder() + .setColor("Red") + .setTitle("Uncaught Exception Monitor Warning") + .setURL("https://nodejs.org/api/process.html#event-warning") + .addFields({ + name: "Warning", + value: `\`\`\`${inspect(warn, { depth: 0 }).slice(0, 1000)}\`\`\``, + }) + .setTimestamp(); + + return client.errorCatcherWebhook.send({ embeds: [errorEmbed] }); + }); +} + +module.exports = { deploy_commands, loadDatabase, loadErrorCatcher } diff --git a/index.js b/index.js index 7b45c87..c5d1566 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const fs = require('fs'); const { Client, GatewayIntentBits } = require("discord.js"); const { token } = require('./config.json'); -const { deploy_commands, loadDatabase } = require('./functions.js'); +const { deploy_commands, loadDatabase, loadErrorCatcher } = require('./functions.js'); const client = new Client({ intents: [ @@ -12,7 +12,8 @@ const client = new Client({ ] }); -loadDatabase(client) +loadDatabase(client); +loadErrorCatcher(client); const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js')); for (const file of eventFiles) {