diff options
Diffstat (limited to 'src/lib/utils/BushLogger.ts')
-rw-r--r-- | src/lib/utils/BushLogger.ts | 256 |
1 files changed, 122 insertions, 134 deletions
diff --git a/src/lib/utils/BushLogger.ts b/src/lib/utils/BushLogger.ts index 073b8e2..7d42574 100644 --- a/src/lib/utils/BushLogger.ts +++ b/src/lib/utils/BushLogger.ts @@ -1,10 +1,11 @@ import chalk from 'chalk'; // eslint-disable-next-line @typescript-eslint/no-unused-vars -import { EmbedBuilder, Util, type Message, type PartialTextBasedChannelFields } from 'discord.js'; +import { EmbedBuilder, escapeMarkdown, PartialTextBasedChannelFields, type Message } from 'discord.js'; import repl, { REPLServer, REPL_MODE_STRICT } from 'repl'; import { WriteStream } from 'tty'; -import { inspect } from 'util'; import { type SendMessageType } from '../extensions/discord-akairo/BushClient.js'; +import { colors } from './BushConstants.js'; +import { getConfigChannel, inspect } from './BushUtils.js'; let REPL: REPLServer; let replGone = false; @@ -59,78 +60,78 @@ export function init() { } /** - * Custom logging utility for the bot. + * Parses the content surrounding by `<<>>` and emphasizes it with the given color or by making it bold. + * @param content The content to parse. + * @param color The color to emphasize the content with. + * @param discordFormat Whether or not to format the content for discord. + * @returns The formatted content. */ -export class BushLogger { - /** - * Parses the content surrounding by `<<>>` and emphasizes it with the given color or by making it bold. - * @param content The content to parse. - * @param color The color to emphasize the content with. - * @param discordFormat Whether or not to format the content for discord. - * @returns The formatted content. - */ - static #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 ? `**${Util.escapeMarkdown(value)}**` : color ? chalk[color](value) : value); - } else { - tempParsedArray.push(discordFormat ? Util.escapeMarkdown(value) : value); - } - }); - return tempParsedArray.join(''); - } - - /** - * Inspects the content and returns a string. - * @param content The content to inspect. - * @param depth The depth the content will inspected. Defaults to `2`. - * @param colors Whether or not to use colors in the output. Defaults to `true`. - * @returns The inspected content. - */ - static #inspectContent(content: any, depth = 2, colors = true): string { - if (typeof content !== 'string') { - return inspect(content, { depth, colors }); +function 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 ? `**${escapeMarkdown(value)}**` : color ? chalk[color](value) : value); + } else { + tempParsedArray.push(discordFormat ? escapeMarkdown(value) : value); } - return content; - } + }); + return tempParsedArray.join(''); +} - /** - * Strips ANSI color codes from a string. - * @param text The string to strip color codes from. - * @returns A string without ANSI color codes. - */ - static #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, - '' - ); +/** + * Inspects the content and returns a string. + * @param content The content to inspect. + * @param depth The depth the content will inspected. Defaults to `2`. + * @param colors Whether or not to use colors in the output. Defaults to `true`. + * @returns The inspected content. + */ +function inspectContent(content: any, depth = 2, colors = true): string { + if (typeof content !== 'string') { + return inspect(content, { depth, colors }); } + return content; +} - /** - * Generates a formatted timestamp for logging. - * @returns The formatted timestamp. - */ - static #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}`; +/** + * Strips ANSI color codes from a string. + * @param text The string to strip color codes from. + * @returns A string without ANSI color codes. + */ +function 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, + '' + ); +} + +/** + * Generates a formatted timestamp for logging. + * @returns The formatted timestamp. + */ +function 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}`; +} +/** + * Custom logging utility for the bot. + */ +export default { /** * Logs information. Highlight information by surrounding it in `<<>>`. * @param header The header displayed before the content, displayed in cyan. @@ -138,31 +139,31 @@ export class BushLogger { * @param sendChannel Should this also be logged to discord? Defaults to false. * @param depth The depth the content will inspected. Defaults to 0. */ - public static get log() { - return BushLogger.info; - } + get log() { + return this.info; + }, /** * Sends a message to the log channel. * @param message The parameter to pass to {@link PartialTextBasedChannelFields.send}. * @returns The message sent. */ - public static async channelLog(message: SendMessageType): Promise<Message | null> { - const channel = await util.getConfigChannel('log'); + async channelLog(message: SendMessageType): Promise<Message | null> { + const channel = await getConfigChannel('log'); return await channel.send(message).catch(() => null); - } + }, /** * Sends a message to the error channel. * @param message The parameter to pass to {@link PartialTextBasedChannelFields.send}. * @returns The message sent. */ - public static async channelError(message: SendMessageType): Promise<Message | null> { - const channel = await util.getConfigChannel('error'); + async channelError(message: SendMessageType): Promise<Message | null> { + const channel = await getConfigChannel('error'); if (!channel) { void this.error( 'BushLogger', - `Could not find error channel, was originally going to send: \n${util.inspect(message, { + `Could not find error channel, was originally going to send: \n${inspect(message, { colors: true })}\n${new Error().stack?.substring(8)}`, false @@ -170,27 +171,27 @@ export class BushLogger { return null; } return await channel.send(message); - } + }, /** * Logs debug information. Only works in dev is enabled in the config. * @param content The content to log. * @param depth The depth the content will inspected. Defaults to `0`. */ - public static debug(content: any, depth = 0): void { + debug(content: any, depth = 0): void { if (!client.config.isDevelopment) return; - const newContent = this.#inspectContent(content, depth, true); - console.log(`${chalk.bgMagenta(this.#getTimeStamp())} ${chalk.magenta('[Debug]')} ${newContent}`); - } + const newContent = inspectContent(content, depth, true); + console.log(`${chalk.bgMagenta(getTimeStamp())} ${chalk.magenta('[Debug]')} ${newContent}`); + }, /** * Logs raw debug information. Only works in dev is enabled in the config. * @param content The content to log. */ - public static debugRaw(...content: any): void { + debugRaw(...content: any): void { if (!client.config.isDevelopment) return; - console.log(`${chalk.bgMagenta(this.#getTimeStamp())} ${chalk.magenta('[Debug]')}`, ...content); - } + console.log(`${chalk.bgMagenta(getTimeStamp())} ${chalk.magenta('[Debug]')}`, ...content); + }, /** * Logs verbose information. Highlight information by surrounding it in `<<>>`. @@ -199,19 +200,17 @@ export class BushLogger { * @param sendChannel Should this also be logged to discord? Defaults to `false`. * @param depth The depth the content will inspected. Defaults to `0`. */ - public static async verbose(header: string, content: any, sendChannel = false, depth = 0): Promise<void> { + async verbose(header: string, content: any, sendChannel = false, depth = 0): Promise<void> { if (!client.config.logging.verbose) return; - const newContent = this.#inspectContent(content, depth, true); - console.log( - `${chalk.bgGrey(this.#getTimeStamp())} ${chalk.grey(`[${header}]`)} ${this.#parseFormatting(newContent, 'blackBright')}` - ); + const newContent = inspectContent(content, depth, true); + console.log(`${chalk.bgGrey(getTimeStamp())} ${chalk.grey(`[${header}]`)} ${parseFormatting(newContent, 'blackBright')}`); if (!sendChannel) return; const embed = new EmbedBuilder() - .setDescription(`**[${header}]** ${this.#parseFormatting(this.#stripColor(newContent), '', true)}`) - .setColor(util.colors.gray) + .setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`) + .setColor(colors.gray) .setTimestamp(); await this.channelLog({ embeds: [embed] }); - } + }, /** * Logs very verbose information. Highlight information by surrounding it in `<<>>`. @@ -219,23 +218,23 @@ export class BushLogger { * @param content The content to log, highlights displayed in bright black. * @param depth The depth the content will inspected. Defaults to `0`. */ - public static async superVerbose(header: string, content: any, depth = 0): Promise<void> { + async superVerbose(header: string, content: any, depth = 0): Promise<void> { if (!client.config.logging.verbose) return; - const newContent = this.#inspectContent(content, depth, true); + const newContent = inspectContent(content, depth, true); console.log( - `${chalk.bgHex('#949494')(this.#getTimeStamp())} ${chalk.hex('#949494')(`[${header}]`)} ${chalk.hex('#b3b3b3')(newContent)}` + `${chalk.bgHex('#949494')(getTimeStamp())} ${chalk.hex('#949494')(`[${header}]`)} ${chalk.hex('#b3b3b3')(newContent)}` ); - } + }, /** * Logs raw very verbose information. * @param header The header printed before the content, displayed in purple. * @param content The content to log. */ - public static async superVerboseRaw(header: string, ...content: any[]): Promise<void> { + async superVerboseRaw(header: string, ...content: any[]): Promise<void> { if (!client.config.logging.verbose) return; - console.log(`${chalk.bgHex('#a3a3a3')(this.#getTimeStamp())} ${chalk.hex('#a3a3a3')(`[${header}]`)}`, ...content); - } + console.log(`${chalk.bgHex('#a3a3a3')(getTimeStamp())} ${chalk.hex('#a3a3a3')(`[${header}]`)}`, ...content); + }, /** * Logs information. Highlight information by surrounding it in `<<>>`. @@ -244,19 +243,17 @@ export class BushLogger { * @param sendChannel Should this also be logged to discord? Defaults to `false`. * @param depth The depth the content will inspected. Defaults to `0`. */ - public static async info(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { + async info(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { if (!client.config.logging.info) return; - const newContent = this.#inspectContent(content, depth, true); - console.log( - `${chalk.bgCyan(this.#getTimeStamp())} ${chalk.cyan(`[${header}]`)} ${this.#parseFormatting(newContent, 'blueBright')}` - ); + const newContent = inspectContent(content, depth, true); + console.log(`${chalk.bgCyan(getTimeStamp())} ${chalk.cyan(`[${header}]`)} ${parseFormatting(newContent, 'blueBright')}`); if (!sendChannel) return; const embed = new EmbedBuilder() - .setDescription(`**[${header}]** ${this.#parseFormatting(this.#stripColor(newContent), '', true)}`) - .setColor(util.colors.info) + .setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`) + .setColor(colors.info) .setTimestamp(); await this.channelLog({ embeds: [embed] }); - } + }, /** * Logs warnings. Highlight information by surrounding it in `<<>>`. @@ -265,22 +262,19 @@ export class BushLogger { * @param sendChannel Should this also be logged to discord? Defaults to `false`. * @param depth The depth the content will inspected. Defaults to `0`. */ - public static async warn(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { - const newContent = this.#inspectContent(content, depth, true); + async warn(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { + const newContent = inspectContent(content, depth, true); console.warn( - `${chalk.bgYellow(this.#getTimeStamp())} ${chalk.yellow(`[${header}]`)} ${this.#parseFormatting( - newContent, - 'yellowBright' - )}` + `${chalk.bgYellow(getTimeStamp())} ${chalk.yellow(`[${header}]`)} ${parseFormatting(newContent, 'yellowBright')}` ); if (!sendChannel) return; const embed = new EmbedBuilder() - .setDescription(`**[${header}]** ${this.#parseFormatting(this.#stripColor(newContent), '', true)}`) - .setColor(util.colors.warn) + .setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`) + .setColor(colors.warn) .setTimestamp(); await this.channelError({ embeds: [embed] }); - } + }, /** * Logs errors. Highlight information by surrounding it in `<<>>`. @@ -289,22 +283,19 @@ export class BushLogger { * @param sendChannel Should this also be logged to discord? Defaults to `false`. * @param depth The depth the content will inspected. Defaults to `0`. */ - public static async error(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { - const newContent = this.#inspectContent(content, depth, true); + async error(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { + const newContent = inspectContent(content, depth, true); console.warn( - `${chalk.bgRedBright(this.#getTimeStamp())} ${chalk.redBright(`[${header}]`)} ${this.#parseFormatting( - newContent, - 'redBright' - )}` + `${chalk.bgRedBright(getTimeStamp())} ${chalk.redBright(`[${header}]`)} ${parseFormatting(newContent, 'redBright')}` ); if (!sendChannel) return; const embed = new EmbedBuilder() - .setDescription(`**[${header}]** ${this.#parseFormatting(this.#stripColor(newContent), '', true)}`) - .setColor(util.colors.error) + .setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`) + .setColor(colors.error) .setTimestamp(); await this.channelError({ embeds: [embed] }); return; - } + }, /** * Logs successes. Highlight information by surrounding it in `<<>>`. @@ -313,21 +304,18 @@ export class BushLogger { * @param sendChannel Should this also be logged to discord? Defaults to `false`. * @param depth The depth the content will inspected. Defaults to `0`. */ - public static async success(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { - const newContent = this.#inspectContent(content, depth, true); + async success(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { + const newContent = inspectContent(content, depth, true); console.log( - `${chalk.bgGreen(this.#getTimeStamp())} ${chalk.greenBright(`[${header}]`)} ${this.#parseFormatting( - newContent, - 'greenBright' - )}` + `${chalk.bgGreen(getTimeStamp())} ${chalk.greenBright(`[${header}]`)} ${parseFormatting(newContent, 'greenBright')}` ); if (!sendChannel) return; const embed = new EmbedBuilder() - .setDescription(`**[${header}]** ${this.#parseFormatting(this.#stripColor(newContent), '', true)}`) - .setColor(util.colors.success) + .setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`) + .setColor(colors.success) .setTimestamp(); await this.channelLog({ embeds: [embed] }).catch(() => {}); } -} +}; /** @typedef {PartialTextBasedChannelFields} vscodeDontDeleteMyImportTy */ |