diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-12-26 17:16:32 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-12-26 17:16:32 -0500 |
commit | fc390ffc300334c396d9d06b0feaf8fbc6ed2814 (patch) | |
tree | a6282a74cf99033291ac7bc9de123ae273d528d2 /src/lib/utils/BushLogger.ts | |
parent | 062435590980b87f5b054418ed88604e26358ae9 (diff) | |
download | tanzanite-fc390ffc300334c396d9d06b0feaf8fbc6ed2814.tar.gz tanzanite-fc390ffc300334c396d9d06b0feaf8fbc6ed2814.tar.bz2 tanzanite-fc390ffc300334c396d9d06b0feaf8fbc6ed2814.zip |
documentation, bug fixes etc
Diffstat (limited to 'src/lib/utils/BushLogger.ts')
-rw-r--r-- | src/lib/utils/BushLogger.ts | 118 |
1 files changed, 96 insertions, 22 deletions
diff --git a/src/lib/utils/BushLogger.ts b/src/lib/utils/BushLogger.ts index 5bae96d..43c9203 100644 --- a/src/lib/utils/BushLogger.ts +++ b/src/lib/utils/BushLogger.ts @@ -1,10 +1,21 @@ +import * as assert from 'assert'; import chalk from 'chalk'; // eslint-disable-next-line @typescript-eslint/no-unused-vars import { MessageEmbed, Util, type Message, type PartialTextBasedChannelFields } from 'discord.js'; import { inspect } from 'util'; import { type BushSendMessageType } from '../extensions/discord-akairo/BushClient'; +/** + * A custom logging utility for the bot. + */ 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' | '', @@ -23,6 +34,13 @@ export class BushLogger { 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 }); @@ -30,6 +48,11 @@ export class BushLogger { return content; } + /** + * 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 @@ -38,6 +61,10 @@ export class BushLogger { ); } + /** + * Generates a formatted timestamp for logging. + * @returns The formatted timestamp. + */ static #getTimeStamp(): string { const now = new Date(); const hours = now.getHours(); @@ -63,26 +90,39 @@ export class BushLogger { } /** - * Sends a message to the log channel - * @param message The parameter to pass to {@link PartialTextBasedChannelFields.send} + * 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: BushSendMessageType) { + public static async channelLog(message: BushSendMessageType): Promise<Message | null> { const channel = await util.getConfigChannel('log'); - await channel.send(message).catch(() => {}); + return await channel.send(message).catch(() => null); } /** - * Sends a message to the error channel + * 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: BushSendMessageType): Promise<Message> { + public static async channelError(message: BushSendMessageType): Promise<Message | null> { const channel = await util.getConfigChannel('error'); + if (!channel) { + void this.error( + 'BushLogger', + `Could not find error channel, was originally going to send: \n${util.inspect(message, { + colors: true + })}\n${new Error().stack?.substring(8)}`, + false + ); + 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. + * @param depth The depth the content will inspected. Defaults to `0`. */ public static debug(content: any, depth = 0): void { if (!client.config.isDevelopment) return; @@ -103,10 +143,10 @@ export class BushLogger { * 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 Should this also be logged to discord? Defaults to false. - * @param depth The depth the content will inspected. Defaults to 0. + * @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) { + public static 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.info( @@ -121,13 +161,30 @@ export class BushLogger { } /** + * Logs very 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 depth The depth the content will inspected. Defaults to `0`. + */ + public static async superVerbose(header: string, content: any, depth = 0): Promise<void> { + if (!client.config.logging.verbose) return; + const newContent = this.#inspectContent(content, depth, true); + console.info( + `${chalk.bgHex('#8423b8')(this.#getTimeStamp())} ${chalk.hex('#8423b8')(`[${header}]`)} ${this.#parseFormatting( + newContent, + '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. - * @param sendChannel Should this also be logged to discord? Defaults to false. - * @param depth The depth the content will inspected. Defaults to 0. + * @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) { + public static 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.info( @@ -145,10 +202,10 @@ export class BushLogger { * 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 false. - * @param depth The depth the content will inspected. Defaults to 0. + * @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) { + public static async warn(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { const newContent = this.#inspectContent(content, depth, true); console.warn( `${chalk.bgYellow(this.#getTimeStamp())} ${chalk.yellow(`[${header}]`)} ${this.#parseFormatting( @@ -169,10 +226,10 @@ export class BushLogger { * 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 false. - * @param depth The depth the content will inspected. Defaults to 0. + * @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) { + public static async error(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { const newContent = this.#inspectContent(content, depth, true); console.error( `${chalk.bgRedBright(this.#getTimeStamp())} ${chalk.redBright(`[${header}]`)} ${this.#parseFormatting( @@ -193,10 +250,10 @@ export class BushLogger { * 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 false. - * @param depth The depth the content will inspected. Defaults to 0. + * @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) { + public static async success(header: string, content: any, sendChannel = true, depth = 0): Promise<void> { const newContent = this.#inspectContent(content, depth, true); console.log( `${chalk.bgGreen(this.#getTimeStamp())} ${chalk.greenBright(`[${header}]`)} ${this.#parseFormatting( @@ -211,6 +268,23 @@ export class BushLogger { .setTimestamp(); await this.channelLog({ embeds: [embed] }).catch(() => {}); } + + /** + * Asserts a condition. If the condition is false, an error is thrown. + * @param type The type of assertion. + * @param actual The value to test. + * @param expected The expected value. + * @param message The error to throw if the assertion fails. + */ + public static assert<T>(type: AssertTypeEqual, actual: unknown, expected: T, message: Error): asserts actual is T; + public static assert<T>(type: AssertTypeNotEqual, actual: unknown, expected: T, message: Error): void; + public static assert(type: AssertType, actual: unknown, expected: unknown, message: Error): void { + assert[type](actual, expected, message); + } } +export type AssertTypeEqual = 'deepEqual' | 'deepStrictEqual' | 'equal' | 'strictEqual'; +export type AssertTypeNotEqual = 'notDeepEqual' | 'notDeepStrictEqual' | 'notEqual' | 'notStrictEqual'; +export type AssertType = AssertTypeEqual | AssertTypeNotEqual; + /** @typedef {PartialTextBasedChannelFields} vscodeDontDeleteMyImportTy */ |