Add error catching and logging

Logs to discord using webhook
This commit is contained in:
Ninjdai1 2023-09-04 10:51:57 +02:00
parent 2265141308
commit 47a1cfcce3
4 changed files with 119 additions and 6 deletions

View File

@ -11,5 +11,6 @@
"sequelizeCredentials": {
"username": "user",
"password": "password"
}
},
"errorWebhookURL": "https://discord.com/api/webhooks/123456789/aBcDeFgHiJkLmNoPqRsTuVwXyZ"
}

19
events/error.js Normal file
View File

@ -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] });
},
};

View File

@ -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 }

View File

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