From f1f660f7f9e23bebeec8c521910ac2e3a2a0263c Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Tue, 15 Mar 2022 02:01:29 +0200 Subject: [PATCH] logger tweaks --- src/structure/DiscordClient.js | 63 ++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/src/structure/DiscordClient.js b/src/structure/DiscordClient.js index 0746a31..cb9be26 100644 --- a/src/structure/DiscordClient.js +++ b/src/structure/DiscordClient.js @@ -1,4 +1,5 @@ const { Client } = require('discord.js'); +const chalk = require('chalk'); const { Logger, Intercom, EventHooker, LocaleLoader, Registry, Dispatcher, Resolver } = require('./client/'); const { Observer, Command, Setting, Inhibitor } = require('./interfaces/'); @@ -9,6 +10,16 @@ const { DefaultGuild } = require('../constants/'); const options = require('../../options.json'); const RateLimiter = require('./client/RateLimiter'); +const Constants = { + ComponentTypes: { + LOAD: 'loaded', + UNLOAD: 'unloaded', + RELOAD: 'reloaded', + ENABLE: 'enabled', + DISABLE: 'disabled' + } +}; + class DiscordClient extends Client { constructor(options) { @@ -18,10 +29,10 @@ class DiscordClient extends Client { ...options.discord.clientOptions }); - this.eventHooker = new EventHooker(this); - this.dispatcher = new Dispatcher(this); - this.intercom = new Intercom(this); this.logger = new Logger(this); + this.eventHooker = new EventHooker(this); + this.intercom = new Intercom(this); + this.dispatcher = new Dispatcher(this); this.localeLoader = new LocaleLoader(this); this.storageManager = new StorageManager(this, options.storage); this.registry = new Registry(this); @@ -45,6 +56,8 @@ class DiscordClient extends Client { // }, 1800000); // I think this is 30 minutes. I could be wrong. // }); + this._loadEevents(); + } async build() { @@ -120,6 +133,50 @@ class DiscordClient extends Client { return this.shard.count === 1; } + /** + * @private + */ + _loadEevents() { + + this.eventHooker.hook('ready', () => { + const guilds = this.guilds.cache.size; + this.logger.info(`Client connected to ${chalk.bold(this.user.tag)} with ${chalk.bold(`${guilds} guild${guilds === 1 ? '' : 's'}`)}.`); + }); + + this.eventHooker.hook('componentUpdate', ({ component, type }) => { + this.logger.info(`Component ${chalk.bold(component.resolveable)} was ${chalk.bold(Constants.ComponentTypes[type])}.`); //eslint-disable-line no-use-before-define + }); + + this.eventHooker.hook('guildCreate', (guild) => { + this.logger.debug(`${chalk.bold('[GUILD]')} Joined guild ${chalk.bold(guild.name)} (${guild.id}).`); + }); + + this.eventHooker.hook('guildDelete', (guild) => { + this.logger.debug(`${chalk.bold('[GUILD]')} Left guild ${chalk.bold(guild.name)} (${guild.id}).`); + }); + + this.eventHooker.hook('shardDisconect', () => { + this.logger.status('Shard disconnected.'); + }); + + this.eventHooker.hook('shardError', (error) => { + this.logger.status(`Shard errored:\n${error.stack ?? error}`); + }); + + this.eventHooker.hook('shardReady', (id, unavailableGuilds) => { + this.logger.status(`Shard is ready${unavailableGuilds ? ` with ${chalk.bold(`${[...unavailableGuilds].length} unavailable guilds`)}` : ''}.`); + }); + + this.eventHooker.hook('shardReconnecting', () => { + this.logger.status('Shard is reconnecting.'); + }); + + this.eventHooker.hook('shardResume', () => { + this.logger.status('Shard resumed.'); + }); + + } + } module.exports = DiscordClient;