aboutsummaryrefslogtreecommitdiff
path: root/src/lib/utils/Logger.ts
blob: f38365ebac76d2553a5c242e2102de0e2fd19c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { TextChannel } from 'discord.js';
import { BotClient } from '../extensions/BotClient';
import chalk from 'chalk';

export class Logger {
	private client: BotClient;
	public constructor(client: BotClient) {
		this.client = client;
	}
	private stripColor(text: string): string {
		return text.replace(
			// eslint-disable-next-line no-control-regex
			/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
			''
		);
	}
	public getChannel(channel: 'log' | 'error' | 'dm'): Promise<TextChannel> {
		return this.client.channels.fetch(
			this.client.config.channels[channel]
		) as Promise<TextChannel>;
	}
	public async log(message: string, sendChannel = false): Promise<void> {
		console.log(chalk`{bgCyan LOG} ` + message);
		if (sendChannel) {
			const channel = await this.getChannel('log');
			await channel.send('[LOG] ' + this.stripColor(message));
		}
	}

	public async verbose(message: string, sendChannel = false): Promise<void> {
		if (!this.client.config.verbose) return;
		console.log(chalk`{bgMagenta VERBOSE} ` + message);
		if (sendChannel) {
			const channel = await this.getChannel('log');
			await channel.send('[VERBOSE] ' + this.stripColor(message));
		}
	}
	public async error(message: string, sendChannel = false): Promise<void> {
		console.log(chalk`{bgRed ERROR} ` + message);
		if (sendChannel) {
			const channel = await this.getChannel('error');
			await channel.send('[ERROR] ' + this.stripColor(message));
		}
	}
}