mirror of
https://github.com/Art-Portal/April.git
synced 2025-01-27 21:33:54 +01:00
Add error catching and logging
Logs to discord using webhook
This commit is contained in:
parent
2265141308
commit
47a1cfcce3
@ -11,5 +11,6 @@
|
|||||||
"sequelizeCredentials": {
|
"sequelizeCredentials": {
|
||||||
"username": "user",
|
"username": "user",
|
||||||
"password": "password"
|
"password": "password"
|
||||||
}
|
},
|
||||||
|
"errorWebhookURL": "https://discord.com/api/webhooks/123456789/aBcDeFgHiJkLmNoPqRsTuVwXyZ"
|
||||||
}
|
}
|
19
events/error.js
Normal file
19
events/error.js
Normal 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] });
|
||||||
|
},
|
||||||
|
};
|
98
functions.js
98
functions.js
@ -1,8 +1,10 @@
|
|||||||
const { REST } = require('@discordjs/rest');
|
const { REST } = require('@discordjs/rest');
|
||||||
const { Routes } = require('discord-api-types/v10');
|
const { Routes } = require('discord-api-types/v10');
|
||||||
const { token, clientId, guildId, sequelizeCredentials } = require('./config.json');
|
const { token, clientId, guildId, sequelizeCredentials, errorWebhookURL } = require('./config.json');
|
||||||
const { Collection } = require('discord.js');
|
const { Collection, WebhookClient, EmbedBuilder, Client } = require('discord.js');
|
||||||
|
const { inspect } = require("util");
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const rest = new REST({ version: '10' }).setToken(token);
|
const rest = new REST({ version: '10' }).setToken(token);
|
||||||
const Sequelize = require('sequelize');
|
const Sequelize = require('sequelize');
|
||||||
|
|
||||||
@ -96,4 +98,94 @@ function loadDatabase(client) {
|
|||||||
artists.sync();
|
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 }
|
||||||
|
5
index.js
5
index.js
@ -1,7 +1,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const { Client, GatewayIntentBits } = require("discord.js");
|
const { Client, GatewayIntentBits } = require("discord.js");
|
||||||
const { token } = require('./config.json');
|
const { token } = require('./config.json');
|
||||||
const { deploy_commands, loadDatabase } = require('./functions.js');
|
const { deploy_commands, loadDatabase, loadErrorCatcher } = require('./functions.js');
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [
|
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'));
|
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
|
||||||
for (const file of eventFiles) {
|
for (const file of eventFiles) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user