diff options
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/BushCache.ts | 5 | ||||
-rw-r--r-- | src/lib/utils/BushLogger.ts | 191 | ||||
-rw-r--r-- | src/lib/utils/Console.ts | 115 | ||||
-rw-r--r-- | src/lib/utils/Logger.ts | 43 |
4 files changed, 196 insertions, 158 deletions
diff --git a/src/lib/utils/BushCache.ts b/src/lib/utils/BushCache.ts new file mode 100644 index 0000000..915fcb1 --- /dev/null +++ b/src/lib/utils/BushCache.ts @@ -0,0 +1,5 @@ +import { Snowflake } from 'discord.js'; + +export class BushCache { + public static superUsers = new Array<Snowflake>(); +} diff --git a/src/lib/utils/BushLogger.ts b/src/lib/utils/BushLogger.ts new file mode 100644 index 0000000..d48ec07 --- /dev/null +++ b/src/lib/utils/BushLogger.ts @@ -0,0 +1,191 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +import chalk from 'chalk'; +import { MessageEmbed } from 'discord.js'; +import { inspect } from 'util'; +import { BushClient, BushMessageType } from '../extensions/BushClient'; + +export class BushLogger { + private client: BushClient; + public constructor(client: BushClient) { + this.client = client; + } + + private parseFormatting( + content: any, + color: 'blueBright' | 'blackBright' | 'redBright' | 'yellowBright' | 'greenBright' | '', + discordFormat = false + ): string | typeof content { + if (typeof content !== 'string') return content; + const newContent: Array<string> = content.split(/<<|>>/); + const tempParsedArray: Array<string> = []; + newContent.forEach((value, index) => { + if (index % 2 !== 0) { + tempParsedArray.push(discordFormat ? `**${value}**` : chalk[color](value)); + } else { + tempParsedArray.push(value); + } + }); + return tempParsedArray.join(''); + } + + private inspectContent(content: any, depth = 2): string { + if (typeof content !== 'string') { + return inspect(content, { depth }); + } + return content; + } + + 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, + '' + ); + } + + private getTimeStamp(): string { + const now = new Date(); + const hours = now.getHours(); + const minute = now.getMinutes(); + let hour = hours; + let amOrPm: 'AM' | 'PM' = 'AM'; + if (hour > 12) { + amOrPm = 'PM'; + hour = hour - 12; + } + return `${hour >= 10 ? hour : `0${hour}`}:${minute >= 10 ? minute : `0${minute}`} ${amOrPm}`; + } + + /** + * Logs information. Highlight information by surrounding it in `<<>>`. + * @param header - The header displayed before the content, displayed in cyan. + * @param content - The content to log, highlights displayed in bright blue. + * @param sendChannel - Should this also be logged to discord? Defaults to true. + */ + public get log() { + return this.info; + } + + /** Sends a message to the log channel */ + public async channelLog(message: BushMessageType): Promise<void> { + const channel = await this.client.util.getConfigChannel('log'); + await channel.send(message).catch(() => {}); + } + + /** Sends a message to the error channel */ + public async channelError(message: BushMessageType): Promise<void> { + const channel = await this.client.util.getConfigChannel('error'); + await channel.send(message).catch(() => {}); + } + + /** + * Logs debug information. Only works in dev is enabled in the config. + * @param content - The content to log. + */ + public debug(...content: any): void { + if (!this.client.config.dev) return; + console.log(`${chalk.bgGrey(this.getTimeStamp())} ${chalk.grey('[Debug]')}`, ...content); + } + + /** + * Logs verbose information. Highlight information by surrounding it in `<<>>`. + * @param header - The header printed before the content, displayed in grey. + * @param content - The content to log, highlights displayed in bright black. + * @param sendChannel = false - Should this also be logged to discord? Defaults to false. + */ + public async verbose(header: string, content: any, sendChannel = false): Promise<void> { + if (!this.client.config.logging.verbose) return; + const newContent = this.inspectContent(content); + console.info( + `${chalk.bgGrey(this.getTimeStamp())} ${chalk.grey(`[${header}]`)} ` + this.parseFormatting(newContent, 'blackBright') + ); + if (!sendChannel) return; + const embed = new MessageEmbed() + .setDescription(`**[${header}]** ${this.stripColor(newContent)}`) + .setColor(this.client.util.colors.gray) + .setTimestamp(); + this.channelLog({ embeds: [embed] }); + } + + /** + * Logs information. Highlight information by surrounding it in `<<>>`. + * @param header - The header displayed before the content, displayed in cyan. + * @param content - The content to log, highlights displayed in bright blue. + * @param sendChannel - Should this also be logged to discord? Defaults to true. + */ + public async info(header: string, content: any, sendChannel = true): Promise<void> { + if (!this.client.config.logging.info) return; + const newContent = this.inspectContent(content); + console.info( + `${chalk.bgCyan(this.getTimeStamp())} ${chalk.cyan(`[${header}]`)} ` + this.parseFormatting(newContent, 'blueBright') + ); + if (!sendChannel) return; + const embed = new MessageEmbed() + .setDescription(`**[${header}]** ${this.parseFormatting(this.stripColor(newContent), '', true)}`) + .setColor(this.client.util.colors.info) + .setTimestamp(); + this.channelLog({ embeds: [embed] }); + } + + /** + * Logs warnings. Highlight information by surrounding it in `<<>>`. + * @param header - The header displayed before the content, displayed in yellow. + * @param content - The content to log, highlights displayed in bright yellow. + * @param sendChannel - Should this also be logged to discord? Defaults to true. + */ + public async warn(header: string, content: any, sendChannel = true): Promise<void> { + const newContent = this.inspectContent(content); + console.warn( + `${chalk.bgYellow(this.getTimeStamp())} ${chalk.yellow(`[${header}]`)} ` + + this.parseFormatting(newContent, 'yellowBright') + ); + + if (!sendChannel) return; + const embed = new MessageEmbed() + .setDescription(`**[${header}]** ${this.parseFormatting(this.stripColor(newContent), '', true)}`) + .setColor(this.client.util.colors.warn) + .setTimestamp(); + this.channelLog({ embeds: [embed] }); + } + + /** + * Logs errors. Highlight information by surrounding it in `<<>>`. + * @param header - The header displayed before the content, displayed in bright red. + * @param content - The content to log, highlights displayed in bright red. + * @param sendChannel - Should this also be logged to discord? Defaults to true. + */ + public async error(header: string, content: any, sendChannel = true): Promise<void> { + const newContent = this.inspectContent(content); + console.error( + `${chalk.bgRedBright(this.getTimeStamp())} ${chalk.redBright(`[${header}]`)} ` + + this.parseFormatting(newContent, 'redBright') + ); + if (!sendChannel) return; + const embed = new MessageEmbed() + .setDescription(`**[${header}]** ${this.stripColor(newContent)}`) + .setColor(this.client.util.colors.error) + .setTimestamp(); + this.channelError({ embeds: [embed] }); + } + + /** + * Logs successes. Highlight information by surrounding it in `<<>>`. + * @param header - The header displayed before the content, displayed in green. + * @param content - The content to log, highlights displayed in bright green. + * @param sendChannel - Should this also be logged to discord? Defaults to true. + */ + public async success(header: string, content: any, sendChannel = true): Promise<void> { + const newContent = this.inspectContent(content); + console.log( + `${chalk.bgGreen(this.getTimeStamp())} ${chalk.greenBright(`[${header}]`)} ` + + this.parseFormatting(newContent, 'greenBright') + ); + if (!sendChannel) return; + const embed = new MessageEmbed() + .setDescription(`**[${header}]** ${this.stripColor(newContent)}`) + .setColor(this.client.util.colors.success) + .setTimestamp(); + await this.channelLog({ embeds: [embed] }).catch(() => {}); + } +} diff --git a/src/lib/utils/Console.ts b/src/lib/utils/Console.ts deleted file mode 100644 index a3bf326..0000000 --- a/src/lib/utils/Console.ts +++ /dev/null @@ -1,115 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -import chalk from 'chalk'; -import { BushClient } from '../extensions/BushClient'; - -export class Log { - client: BushClient; - - public constructor(client: BushClient) { - this.client = client; - } - - private parseColors( - content: any, - color: 'blueBright' | 'blackBright' | 'redBright' | 'yellowBright' | 'greenBright' - ): string | any { - if (typeof content === 'string') { - const newContent: Array<string> = content.split(/<<|>>/); - const tempParsedArray: Array<string> = []; - newContent.forEach((value, index) => { - if (index % 2 !== 0) { - tempParsedArray.push(chalk[color](value)); - } else { - tempParsedArray.push(value); - } - }); - return tempParsedArray.join(''); - } else { - return content; - } - } - - private timeStamp(): string { - const now = new Date(); - const hours = now.getHours(); - const minute = now.getMinutes(); - let hour = hours; - let amOrPm: 'AM' | 'PM' = 'AM'; - if (hour > 12) { - amOrPm = 'PM'; - hour = hour - 12; - } - - return `${hour >= 10 ? hour : `0${hour}`}:${minute >= 10 ? minute : `0${minute}`} ${amOrPm}`; - } - - /** - * Logs debug information. Only works in dev is enabled in the config. - * @param content - The content to log. - */ - public debug(...content: any): void { - if (this.client.config.dev) { - console.log(`${chalk.bgGrey(this.timeStamp())} ${chalk.grey('[Debug]')}`, ...content); - } - } - - /** - * Logs verbose information. Highlight information by surrounding it in `<<>>`. - * @param header - The header displayed before the content, displayed in grey. - * @param content - The content to log, highlights displayed in bright black. - */ - public verbose(header: string, content: any): void { - if (this.client.config.logging.verbose) { - return console.info( - `${chalk.bgGrey(this.timeStamp())} ${chalk.grey(`[${header}]`)} ` + this.parseColors(content, 'blackBright') - ); - } - } - - /** - * Logs information. Highlight information by surrounding it in `<<>>`. - * @param header - The header displayed before the content, displayed in cyan. - * @param content - The content to log, highlights displayed in bright blue. - */ - public info(header: string, content: any): void { - if (this.client.config.logging.info) { - return console.info( - `${chalk.bgCyan(this.timeStamp())} ${chalk.cyan(`[${header}]`)} ` + this.parseColors(content, 'blueBright') - ); - } - } - - /** - * Logs warnings. Highlight information by surrounding it in `<<>>`. - * @param header - The header displayed before the content, displayed in yellow. - * @param content - The content to log, highlights displayed in bright yellow. - */ - public warn(header: string, content: any): void { - return console.warn( - `${chalk.bgYellow(this.timeStamp())} ${chalk.yellow(`[${header}]`)} ` + this.parseColors(content, 'yellowBright') - ); - } - - /** - * Logs errors. Highlight information by surrounding it in `<<>>`. - * @param header - The header displayed before the content, displayed in bright red. - * @param content - The content to log, highlights displayed in bright red. - */ - public error(header: string, content: any): void { - return console.error( - `${chalk.bgRedBright(this.timeStamp())} ${chalk.redBright(`[${header}]`)} ` + this.parseColors(content, 'redBright') - ); - } - - /** - * Logs successes. Highlight information by surrounding it in `<<>>`. - * @param header - The header displayed before the content, displayed in green. - * @param content - The content to log, highlights displayed in bright green. - */ - public success(header: string, content: any): void { - return console.log( - `${chalk.bgGreen(this.timeStamp())} ${chalk.greenBright(`[${header}]`)} ` + this.parseColors(content, 'greenBright') - ); - } -} diff --git a/src/lib/utils/Logger.ts b/src/lib/utils/Logger.ts deleted file mode 100644 index 0675e3d..0000000 --- a/src/lib/utils/Logger.ts +++ /dev/null @@ -1,43 +0,0 @@ -import chalk from 'chalk'; -import { TextChannel } from 'discord.js'; -import { BushClient } from '../extensions/BushClient'; - -export class BushLogger { - private client: BushClient; - public constructor(client: BushClient) { - 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.logging.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)); - } - } -} |