mirror of
https://github.com/TheShadowEevee/Konpeki-Discord-Bot.git
synced 2025-01-11 22:38:50 -06:00
Make logs more readable
This commit is contained in:
parent
0b8abbaf42
commit
dd882eb048
3 changed files with 75 additions and 5 deletions
13
index.js
13
index.js
|
@ -8,7 +8,10 @@ const fs = require('node:fs');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
|
|
||||||
// Add pm2 metrics - this should NEVER track ANYTHING identifiable. This is purely for basic metrics and bot performance tracking
|
// Add pm2 metrics - this should NEVER track ANYTHING identifiable. This is purely for basic metrics and bot performance tracking
|
||||||
const metrics = require('./pm2-metrics.js');
|
const metrics = require('./utils/pm2-metrics.js');
|
||||||
|
|
||||||
|
// Use a custom logging script
|
||||||
|
const logger = require('./utils/logging.js');
|
||||||
|
|
||||||
// Require the necessary discord.js classes
|
// Require the necessary discord.js classes
|
||||||
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
|
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
|
||||||
|
@ -31,14 +34,14 @@ for (const file of commandFiles) {
|
||||||
client.commands.set(command.data.name, command);
|
client.commands.set(command.data.name, command);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
logger.log(logger.logLevels.WARN, `The command at ${filePath} is missing a required "data" or "execute" property.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the client is ready, run this code (only once)
|
// When the client is ready, run this code (only once)
|
||||||
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
|
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
|
||||||
client.once(Events.ClientReady, c => {
|
client.once(Events.ClientReady, c => {
|
||||||
console.log(`Ready! Logged in as ${c.user.tag}`);
|
logger.log(logger.logLevels.INFO, `${logger.colorText('Ready!', logger.textColor.Green)} Logged in as ${logger.colorText(c.user.tag, logger.textColor.Blue)}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Client "on" Events
|
// Client "on" Events
|
||||||
|
@ -48,7 +51,7 @@ client.on(Events.InteractionCreate, async interaction => {
|
||||||
const command = interaction.client.commands.get(interaction.commandName);
|
const command = interaction.client.commands.get(interaction.commandName);
|
||||||
|
|
||||||
if (!command) {
|
if (!command) {
|
||||||
console.error(`No command matching ${interaction.commandName} was found.`);
|
logger.log(logger.logLevels.ERROR, `No command matching ${interaction.commandName} was found.`);
|
||||||
await interaction.reply({ content: `This command no longer exists! Please contact ${botOwner} to report that this is happening!`, ephemeral: true });
|
await interaction.reply({ content: `This command no longer exists! Please contact ${botOwner} to report that this is happening!`, ephemeral: true });
|
||||||
|
|
||||||
// Report error to PM2 dashboard
|
// Report error to PM2 dashboard
|
||||||
|
@ -66,7 +69,7 @@ client.on(Events.InteractionCreate, async interaction => {
|
||||||
await command.execute(interaction);
|
await command.execute(interaction);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
logger.log(logger.logLevels.ERROR, error);
|
||||||
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
||||||
|
|
||||||
// Report error to PM2 dashboard
|
// Report error to PM2 dashboard
|
||||||
|
|
62
utils/logging.js
Normal file
62
utils/logging.js
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
* Konpeki Shiho - Utility Definition File
|
||||||
|
* logging.js - A custom logging script to apply information to output
|
||||||
|
*
|
||||||
|
* Though console.error() and console.warn() exist, they don't exactly fit what is wanted here.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Enum list of severity levels
|
||||||
|
const logLevels = {
|
||||||
|
DEBUG: 0,
|
||||||
|
INFO: 1,
|
||||||
|
WARN: 2,
|
||||||
|
ERROR: 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Enum list of text colors
|
||||||
|
const textColor = {
|
||||||
|
White: '\x1b[97m',
|
||||||
|
Gray: '\x1b[37m',
|
||||||
|
Yellow: '\x1b[33m',
|
||||||
|
Red: '\x1b[91m',
|
||||||
|
Blue: '\x1b[96m',
|
||||||
|
Green: '\x1b[92m',
|
||||||
|
Reset: '\x1b[0m',
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get the current Date and Time
|
||||||
|
const date_time = new Date(new Date().toUTCString());
|
||||||
|
const datetime = '[' + ('0' + (date_time.getMonth() + 1)).slice(-2) + '/' + ('0' + date_time.getDate()).slice(-2) + '/' + date_time.getFullYear() + ' ' + ('0' + (date_time.getHours() + 1)).slice(-2) + ':' + ('0' + (date_time.getMinutes() + 1)).slice(-2) + ':' + ('0' + (date_time.getSeconds() + 1)).slice(-2) + ' UTC]';
|
||||||
|
|
||||||
|
// Add color to the text passed through - If a text manip util is made, move this there
|
||||||
|
const colorText = function(message, color) {
|
||||||
|
return color + message + textColor.Reset;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Print log message to console based on severity level
|
||||||
|
const log = function(logLevel, message) {
|
||||||
|
switch (logLevel) {
|
||||||
|
case 0:
|
||||||
|
console.log(datetime + ' ' + colorText('[DEBUG]', textColor.Gray) + ' ' + message);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
console.log(datetime + ' ' + colorText('[INFO]', textColor.White) + ' ' + message);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
console.log(datetime + ' ' + colorText('[WARN]', textColor.Yellow) + ' ' + message);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
console.log(datetime + ' ' + colorText('[ERROR]', textColor.Red) + ' ' + message);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log(datetime + ' ' + colorText('[INFO]', textColor.White) + ' ' + message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
logLevels,
|
||||||
|
textColor,
|
||||||
|
colorText,
|
||||||
|
log,
|
||||||
|
};
|
|
@ -1,3 +1,8 @@
|
||||||
|
/*
|
||||||
|
* Konpeki Shiho - Utility Definition File
|
||||||
|
* pm2-metrics.js - A place to keep all pm2 metrics and error definitions
|
||||||
|
*/
|
||||||
|
|
||||||
const io = require('@pm2/io');
|
const io = require('@pm2/io');
|
||||||
|
|
||||||
const interactionErrors = io.counter({
|
const interactionErrors = io.counter({
|
Loading…
Reference in a new issue