slight logger rework

This commit is contained in:
Erik 2022-03-15 02:00:45 +02:00
parent 038b5b6403
commit 5922b51539
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589
5 changed files with 24 additions and 55 deletions

View File

@ -1,4 +1,5 @@
const { Collection } = require('@discordjs/collection');
const Logger = require('./Logger');
class Dispatcher {
@ -6,6 +7,7 @@ class Dispatcher {
this.client = client;
this.dispatched = new Collection();
this.logger = new Logger(this);
this.client.eventHooker.hook('componentUpdate', ({ component, type }) => {
if(component.type === 'observer') {
@ -29,7 +31,7 @@ class Dispatcher {
.filter((c) => c._type === 'observer' && !c.disabled)
.sort((a, b) => a.priority - b.priority);
this.client.logger.debug(`Starting dispatch on ${observers.size} observers.`);
this.logger.debug(`Starting dispatch on ${observers.size} observers.`);
for(const observer of observers.values()) {
for(const [hook, func] of observer.hooks) {
this.client.eventHooker.hook(hook, func);

View File

@ -1,4 +1,5 @@
const { EventEmitter } = require('events');
const Logger = require('./Logger');
const { InteractionWrapper, GuildWrapper } = require('./wrappers');
class EventHooker {
@ -8,10 +9,12 @@ class EventHooker {
this.target = target;
this.events = new Map();
this.logger = new Logger(this);
}
hook(eventName, func) {
this.logger.debug(`Hooking ${eventName} to ${func.name}`);
if(this.events.has(eventName)) {
const funcs = this.events.get(eventName);
this.events.set(eventName, [ ...funcs, func ]);
@ -33,7 +36,9 @@ class EventHooker {
}
async _handleEvent(eventName) {
this.logger.debug(`Setting up handler for ${eventName}`);
this.target.on(eventName, async (...args) => {
this.logger.debug(`Handler ${eventName}`);
// Should probably move this elsewhere, but testing this out
for (const arg of args) if (arg?.guild) arg.guildWrapper = new GuildWrapper(this.target, arg.guild);

View File

@ -4,6 +4,7 @@ const chalk = require('chalk');
const Util = require('../../Util.js');
const { Emojis } = require('../../constants/index.js');
const Logger = require('./Logger.js');
class LocaleLoader {
@ -13,6 +14,8 @@ class LocaleLoader {
this.languages = {};
this.logger = new Logger(this);
}
async loadLanguages() {
@ -40,7 +43,7 @@ class LocaleLoader {
try {
string = eval(string); //eslint-disable-line no-eval
} catch(error) {
this.client.logger.error(`Locale [${language}.${index}] failed to execute code.\n${error.stack || error}`);
this.logger.error(`Locale [${language}.${index}] failed to execute code.\n${error.stack || error}`);
}
}
@ -63,7 +66,7 @@ class LocaleLoader {
}
this.languages[language] = combined;
this.client.logger.info(`Language ${chalk.bold(language)} was ${chalk.bold("loaded")}.`);
this.logger.info(`Language ${chalk.bold(language)} was ${chalk.bold("loaded")}.`);
}

View File

@ -2,64 +2,21 @@
const chalk = require('chalk');
const { inspect } = require('util');
const Constants = {
ComponentTypes: {
LOAD: 'loaded',
UNLOAD: 'unloaded',
RELOAD: 'reloaded',
ENABLE: 'enabled',
DISABLE: 'disabled'
}
};
class Logger {
constructor(client) {
this.client = client;
this.client.eventHooker.hook('ready', () => {
const guilds = this.client.guilds.cache.size;
this.info(`Client connected to ${chalk.bold(this.client.user.tag)} with ${chalk.bold(`${guilds} guild${guilds === 1 ? '' : 's'}`)}.`);
});
this.client.eventHooker.hook('componentUpdate', ({ component, type }) => {
this.info(`Component ${chalk.bold(component.resolveable)} was ${chalk.bold(Constants.ComponentTypes[type])}.`); //eslint-disable-line no-use-before-define
});
this.client.eventHooker.hook('guildCreate', (guild) => {
this.debug(`${chalk.bold('[GUILD]')} Joined guild ${chalk.bold(guild.name)} (${guild.id}).`);
});
this.client.eventHooker.hook('guildDelete', (guild) => {
this.debug(`${chalk.bold('[GUILD]')} Left guild ${chalk.bold(guild.name)} (${guild.id}).`);
});
this.client.eventHooker.hook('shardDisconect', () => {
this.status('Shard disconnected.');
});
this.client.eventHooker.hook('shardError', (error) => {
this.status(`Shard errored:\n${error.stack ?? error}`);
});
this.client.eventHooker.hook('shardReady', (id, unavailableGuilds) => {
this.status(`Shard is ready${unavailableGuilds ? ` with ${chalk.bold(`${[...unavailableGuilds].length} unavailable guilds`)}` : ''}.`);
});
this.client.eventHooker.hook('shardReconnecting', () => {
this.status('Shard is reconnecting.');
});
this.client.eventHooker.hook('shardResume', () => {
this.status('Shard resumed.');
});
this.name = client.name || client.constructor.name;
}
async transport(message = 'N/A', opts = {}) {
if (typeof message !== 'string') message = inspect(message);
return this.client.intercom.send('logger', { message, ...opts });
message = `${chalk.bold(`[${this.name.substring(0, 6)}]`)} ${message}`;
process.send({ _logger: true, message, ...opts });
//return this.client.intercom.send('logger', { message, ...opts });
}
/* Quick & Dirty Functions */

View File

@ -3,6 +3,7 @@ const path = require('path');
const { Component, Module } = require('../interfaces/');
const Util = require('../../Util.js');
const Logger = require('./Logger');
class Registry {
@ -11,6 +12,7 @@ class Registry {
this.client = client;
this.components = new Collection();
this.logger = new Logger(this);
}
@ -32,14 +34,14 @@ class Registry {
for(const path of files) {
const func = require(path);
if(typeof func !== 'function') {
this.client.logger.warn("Attempted to index an invalid function as a component.");
this.logger.warn("Attempted to index an invalid function as a component.");
delete require.cache[path];
continue;
}
const component = new func(this.client); //Instantiates the component class.
if(classToHandle && !(component instanceof classToHandle)) {
this.client.logger.warn("Attempted to load an invalid class.");
this.logger.warn("Attempted to load an invalid class.");
delete require.cache[path];
continue;
}
@ -65,12 +67,12 @@ class Registry {
if(!(component instanceof Component)) {
delete require.cache[directory];
this.client.logger.warn("Attempted to load an invalid component.");
this.logger.warn("Attempted to load an invalid component.");
return null;
}
if(this.components.has(component.resolveable)) {
this.client.logger.warn("Attempted to reload an existing component.");
this.logger.warn("Attempted to reload an existing component.");
return null;
}