Skip to content

Commit

Permalink
add more options for logging config, timeout user fetch for level
Browse files Browse the repository at this point in the history
  • Loading branch information
IRONM00N committed Nov 28, 2023
1 parent dcd02d2 commit 474bb8b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@typescript-eslint/prefer-as-const": "warn",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-unsafe-declaration-merging": "off"
"@typescript-eslint/no-unsafe-declaration-merging": "off",
"prefer-const": "warn"
}
}
10 changes: 10 additions & 0 deletions config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,23 @@ export type Logging = {
* Whether or not to log database queries, verbose logs, or informational logs
*/
[Key in LoggingType]: boolean;
} & {
/**
* Override default behavior
*/
[Key in OverrideLog]?: boolean;
};

/**
* The logging level that can be changed.
*/
export type LoggingType = 'db' | 'verbose' | 'info';

/**
* Override the default logging behavior
*/
export type OverrideLog = 'success' | 'error' | 'warn' | 'veryVerbose' | 'debug' | 'noMessage' | 'noRaw';

/**
* Information regarding the bot's support server.
*/
Expand Down
78 changes: 65 additions & 13 deletions lib/utils/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import chalk from 'chalk';
import { EmbedBuilder, bold, escapeMarkdown, type Client, type Message, type PartialTextBasedChannelFields } from 'discord.js';
import {
BitField,
EmbedBuilder,
bold,
escapeMarkdown,
type Client,
type Message,
type PartialTextBasedChannelFields
} from 'discord.js';
import repl, { REPL_MODE_STRICT, type REPLServer } from 'node:repl';
import type { WriteStream } from 'node:tty';
import { stripVTControlCharacters as stripColor } from 'node:util';
Expand Down Expand Up @@ -117,14 +125,51 @@ function pad(num: number) {
return num.toString().padStart(2, '0');
}

/**
* The flags representing what forms of the logger are enabled, allows for default logging behavior to be overridden
* by the config.
*/
export enum LoggingFlags {
None = 0,
Info = 1 << 0,
Success = 1 << 1,
Error = 1 << 2,
Warn = 1 << 3,
Verbose = 1 << 4,
VeryVerbose = 1 << 5,
Debug = 1 << 6,
NoMessage = 1 << 7,
NoRaw = 1 << 8
}

export type LoggingFlagsKeys = keyof typeof LoggingFlags;

/**
* Custom logging utility for the bot.
*/
export class Logger {
private flags: BitField<LoggingFlagsKeys>;

/**
* @param client The client.
*/
public constructor(public client: Client) {}
public constructor(public client: Client) {
let flags = LoggingFlags.None;

const l = client.config.logging;

if (l.info) flags |= LoggingFlags.Info;
if ('success' in l ? l.success : true) flags |= LoggingFlags.Success;
if ('error' in l ? l.error : true) flags |= LoggingFlags.Error;
if ('warn' in l ? l.warn : true) flags |= LoggingFlags.Warn;
if (l.verbose) flags |= LoggingFlags.Verbose;
if ('veryVerbose' in l ? l.veryVerbose : l.verbose) flags |= LoggingFlags.VeryVerbose;
if ('debug' in l ? l.debug : client.config.isDevelopment) flags |= LoggingFlags.Debug;
if ('noMessage' in l ? l.noMessage : false) flags |= LoggingFlags.NoMessage;
if ('noRaw' in l ? l.noRaw : false) flags |= LoggingFlags.NoRaw;

this.flags = new BitField<LoggingFlagsKeys>(flags);
}

/**
* Logs information. Highlight information by surrounding it in `<<>>`.
Expand Down Expand Up @@ -174,7 +219,7 @@ export class Logger {
* @param depth The depth the content will inspected. Defaults to `0`.
*/
public debug(content: any, depth = 0): void {
if (!this.client.config.isDevelopment) return;
if (!this.flags.has('Debug')) return;
const newContent = inspectContent(content, depth, true);
console.log(`${chalk.bgMagenta(getTimeStamp())} ${chalk.magenta('[Debug]')} ${newContent}`);
}
Expand All @@ -184,7 +229,7 @@ export class Logger {
* @param content The content to log.
*/
public debugRaw(...content: any): void {
if (!this.client.config.isDevelopment) return;
if (!this.flags.has('Debug') || this.flags.has('NoRaw')) return;
console.log(`${chalk.bgMagenta(getTimeStamp())} ${chalk.magenta('[Debug]')}`, ...content);
}

Expand All @@ -196,10 +241,11 @@ export class Logger {
* @param depth The depth the content will inspected. Defaults to `0`.
*/
public async verbose(header: string, content: any, sendChannel = false, depth = 0): Promise<void> {
if (!this.client.config.logging.verbose) return;
if (!this.flags.has('Verbose')) return;
const newContent = inspectContent(content, depth, true);
console.log(`${chalk.bgGrey(getTimeStamp())} ${chalk.grey(`[${header}]`)} ${parseFormatting(newContent, 'blackBright')}`);
if (!sendChannel) return;

if (!sendChannel || this.flags.has('NoMessage')) return;
const embed = new EmbedBuilder()
.setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`)
.setColor(colors.gray)
Expand All @@ -214,7 +260,7 @@ export class Logger {
* @param depth The depth the content will inspected. Defaults to `0`.
*/
public async superVerbose(header: string, content: any, depth = 0): Promise<void> {
if (!this.client.config.logging.verbose) return;
if (!this.flags.has('VeryVerbose')) return;
const newContent = inspectContent(content, depth, true);
console.log(
`${chalk.bgHex('#949494')(getTimeStamp())} ${chalk.hex('#949494')(`[${header}]`)} ${chalk.hex('#b3b3b3')(newContent)}`
Expand All @@ -227,7 +273,7 @@ export class Logger {
* @param content The content to log.
*/
public async superVerboseRaw(header: string, ...content: any[]): Promise<void> {
if (!this.client.config.logging.verbose) return;
if (!this.flags.has('VeryVerbose') || this.flags.has('NoRaw')) return;
console.log(`${chalk.bgHex('#a3a3a3')(getTimeStamp())} ${chalk.hex('#a3a3a3')(`[${header}]`)}`, ...content);
}

Expand All @@ -239,10 +285,11 @@ export class Logger {
* @param depth The depth the content will inspected. Defaults to `0`.
*/
public async info(header: string, content: any, sendChannel = true, depth = 0): Promise<void> {
if (!this.client.config.logging.info) return;
if (!this.flags.has('Info')) return;
const newContent = inspectContent(content, depth, true);
console.log(`${chalk.bgCyan(getTimeStamp())} ${chalk.cyan(`[${header}]`)} ${parseFormatting(newContent, 'blueBright')}`);
if (!sendChannel) return;

if (!sendChannel || this.flags.has('NoMessage')) return;
const embed = new EmbedBuilder()
.setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`)
.setColor(colors.info)
Expand All @@ -258,12 +305,13 @@ export class Logger {
* @param depth The depth the content will inspected. Defaults to `0`.
*/
public async warn(header: string, content: any, sendChannel = true, depth = 0): Promise<void> {
if (!this.flags.has('Warn')) return;
const newContent = inspectContent(content, depth, true);
console.warn(
`${chalk.bgYellow(getTimeStamp())} ${chalk.yellow(`[${header}]`)} ${parseFormatting(newContent, 'yellowBright')}`
);

if (!sendChannel) return;
if (!sendChannel || this.flags.has('NoMessage')) return;
const embed = new EmbedBuilder()
.setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`)
.setColor(colors.warn)
Expand All @@ -279,11 +327,13 @@ export class Logger {
* @param depth The depth the content will inspected. Defaults to `0`.
*/
public async error(header: string, content: any, sendChannel = true, depth = 0): Promise<void> {
if (!this.flags.has('Error')) return;
const newContent = inspectContent(content, depth, true);
console.warn(
`${chalk.bgRedBright(getTimeStamp())} ${chalk.redBright(`[${header}]`)} ${parseFormatting(newContent, 'redBright')}`
);
if (!sendChannel) return;

if (!sendChannel || this.flags.has('NoMessage')) return;
const embed = new EmbedBuilder()
.setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`)
.setColor(colors.error)
Expand All @@ -300,11 +350,13 @@ export class Logger {
* @param depth The depth the content will inspected. Defaults to `0`.
*/
public async success(header: string, content: any, sendChannel = true, depth = 0): Promise<void> {
if (!this.flags.has('Success')) return;
const newContent = inspectContent(content, depth, true);
console.log(
`${chalk.bgGreen(getTimeStamp())} ${chalk.greenBright(`[${header}]`)} ${parseFormatting(newContent, 'greenBright')}`
);
if (!sendChannel) return;

if (!sendChannel || this.flags.has('NoMessage')) return;
const embed = new EmbedBuilder()
.setDescription(`**[${header}]** ${parseFormatting(stripColor(newContent), '', true)}`)
.setColor(colors.success)
Expand Down
7 changes: 6 additions & 1 deletion src/commands/leveling/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
BotCommand,
CanvasProgressBar,
Level,
Time,
emojis,
sleep,
type CommandMessage,
type OptArgType,
type SlashMessage
Expand Down Expand Up @@ -151,7 +153,10 @@ export default class LevelCommand extends BotCommand {

this.logger.debug(`hexAccentColor: ${user.hexAccentColor}`);

await user.fetch(true); // get accent color
await Promise.race([
user.fetch(true), // get accent color
sleep(2 * Time.Second) // timeout request after 2 seconds
]);

mark(`image:fetch`);

Expand Down

0 comments on commit 474bb8b

Please sign in to comment.