galactic-bot/src/structure/client/Logger.js

87 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-04-12 14:34:26 +02:00
const chalk = require('chalk');
const Constants = {
ComponentTypes: {
LOAD: 'loaded',
UNLOAD: 'unloaded',
RELOAD: 'reloaded',
ENABLE: 'enabled',
DISABLE: 'disabled'
}
};
2020-04-12 14:34:26 +02:00
class Logger {
constructor(client) {
this.client = client;
2020-04-13 22:38:10 +02:00
this.client.eventHooker.hook('ready', () => {
2020-04-14 05:25:17 +02:00
const guilds = this.client.guilds.cache.size;
2021-08-16 23:01:08 +02:00
this.info(`Client connected to ${chalk.bold(this.client.user.tag)} with ${chalk.bold(`${guilds} guild${guilds === 1 ? '' : 's'}`)}.`);
2020-04-12 14:34:26 +02:00
});
2020-04-13 22:38:10 +02:00
this.client.eventHooker.hook('componentUpdate', ({ component, type }) => {
2021-08-16 23:01:08 +02:00
this.info(`Component ${chalk.bold(component.resolveable)} was ${chalk.bold(Constants.ComponentTypes[type])}.`); //eslint-disable-line no-use-before-define
2020-04-12 14:34:26 +02:00
});
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}).`);
});
2021-08-16 23:01:08 +02:00
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.');
});
2020-04-12 14:34:26 +02:00
}
async transport(message = 'N/A', opts = {}) {
return this.client.intercom.send('logger', { message, ...opts });
2020-04-12 14:34:26 +02:00
}
/* Quick & Dirty Functions */
2021-08-16 23:01:08 +02:00
warn(message, opts = {}) {
this.transport(message, { ...opts, type: 'warn' });
2020-04-12 14:34:26 +02:00
}
2021-08-16 23:01:08 +02:00
error(message, opts = {}) {
this.transport(message, { ...opts, type: 'error' });
2020-04-12 14:34:26 +02:00
}
2021-08-16 23:01:08 +02:00
debug(message, opts = {}) {
this.transport(message, { ...opts, type: 'debug' });
2020-04-12 14:34:26 +02:00
}
2020-04-13 22:38:10 +02:00
info(message, opts = {}) {
this.transport(message, { ...opts, type: 'info' });
}
2021-08-16 23:01:08 +02:00
status(message, opts = {}) {
this.transport(message, { ...opts, type: 'status' });
2020-04-12 14:34:26 +02:00
}
}
module.exports = Logger;