aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2022-03-14 20:44:16 -0400
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2022-03-14 20:44:16 -0400
commit6c9710cb76347b79a2463ec0ca5bde59177b531f (patch)
treef6b00aa2af9b84ca2e99c3699145fe5475a27cf9 /src
parent59a4eb09916ad7c9e6cd8ea8b4d3a72189c284ce (diff)
downloadtanzanite-6c9710cb76347b79a2463ec0ca5bde59177b531f.tar.gz
tanzanite-6c9710cb76347b79a2463ec0ca5bde59177b531f.tar.bz2
tanzanite-6c9710cb76347b79a2463ec0ca5bde59177b531f.zip
feat(logging): over complicated repl
Diffstat (limited to 'src')
-rw-r--r--src/lib/common/AutoMod.ts6
-rw-r--r--src/lib/common/HighlightManager.ts2
-rw-r--r--src/lib/utils/BushLogger.ts54
-rw-r--r--src/listeners/client/ready.ts2
-rw-r--r--src/listeners/other/consoleListener.ts3
-rw-r--r--src/listeners/track-manual-punishments/modlogSyncBan.ts5
-rw-r--r--src/listeners/track-manual-punishments/modlogSyncKick.ts5
-rw-r--r--src/listeners/track-manual-punishments/modlogSyncTimeout.ts5
-rw-r--r--src/listeners/track-manual-punishments/modlogSyncUnban.ts5
9 files changed, 64 insertions, 23 deletions
diff --git a/src/lib/common/AutoMod.ts b/src/lib/common/AutoMod.ts
index db3e709..1aa6266 100644
--- a/src/lib/common/AutoMod.ts
+++ b/src/lib/common/AutoMod.ts
@@ -200,7 +200,7 @@ export class AutoMod {
}
},
(err: any, response: any) => {
- if (err) return console.log(err?.message);
+ if (err) return client.console.stdout(err?.message);
const normalize = (val: number, min: number, max: number) => (val - min) / (max - min);
@@ -214,10 +214,10 @@ export class AutoMod {
}
};
- console.log(chalk.cyan(this.message.content));
+ client.console.stdout(chalk.cyan(this.message.content));
Object.entries(response.data.attributeScores)
.sort(([a], [b]) => a.localeCompare(b))
- .forEach(([key, value]: any[]) => console.log(chalk.white(key), color(value.summaryScore.value)));
+ .forEach(([key, value]: any[]) => client.console.raw(chalk.white(key), color(value.summaryScore.value)));
}
);
}
diff --git a/src/lib/common/HighlightManager.ts b/src/lib/common/HighlightManager.ts
index a8bacee..4347c27 100644
--- a/src/lib/common/HighlightManager.ts
+++ b/src/lib/common/HighlightManager.ts
@@ -214,7 +214,7 @@ export class HighlightManager {
if (this.lastedDMedUserCooldown.has(user)) {
const lastDM = this.lastedDMedUserCooldown.get(user)!;
if (new Date().getTime() - lastDM.getTime() < 5 * Time.Minute) {
- console.log(`User ${user} has been dmed recently.`);
+ void client.console.verbose('Highlight', `User <<${user}>> has been dmed recently.`);
return false;
}
}
diff --git a/src/lib/utils/BushLogger.ts b/src/lib/utils/BushLogger.ts
index dc43a4f..4d0e7b9 100644
--- a/src/lib/utils/BushLogger.ts
+++ b/src/lib/utils/BushLogger.ts
@@ -1,13 +1,49 @@
import chalk from 'chalk';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Embed, Util, type Message, type PartialTextBasedChannelFields } from 'discord.js';
+import repl, { REPL_MODE_STRICT } from 'repl';
import { inspect } from 'util';
import { type BushSendMessageType } from '../extensions/discord-akairo/BushClient.js';
+const REPL = repl.start({
+ useColors: true,
+ terminal: true,
+ useGlobal: true,
+ replMode: REPL_MODE_STRICT,
+ breakEvalOnSigint: true
+});
+
/**
* Custom logging utility for the bot.
*/
export class BushLogger {
+ public static stdout(string: string): void {
+ return this.#baseLog('stdout', string);
+ }
+
+ public static stderr(string: string): void {
+ return this.#baseLog('stderr', string);
+ }
+
+ static #baseLog(type: 'log', ...content: any): void;
+ static #baseLog(type: 'stdout' | 'stderr' | 'log', content: string): void;
+ static #baseLog(type: 'stdout' | 'stderr' | 'log', content: string): void {
+ const stream: NodeJS.WriteStream = type === 'stdout' || type === 'log' ? process.stdout : process.stderr;
+
+ stream.moveCursor(0, -1);
+ stream.write('\n');
+ stream.clearLine(0);
+ // eslint-disable-next-line prefer-rest-params
+ if (type === 'log') console.log([...arguments].slice(1));
+ else stream.write(`${content}\n`);
+ stream.moveCursor(0, typeof content === 'string' ? content.split('\n').length : 1);
+ REPL.displayPrompt(true);
+ }
+
+ public static raw(...content: any[]) {
+ return this.#baseLog('log', ...content);
+ }
+
/**
* Parses the content surrounding by `<<>>` and emphasizes it with the given color or by making it bold.
* @param content The content to parse.
@@ -126,7 +162,7 @@ export class BushLogger {
public static 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);
+ this.stdout(`${chalk.bgMagenta(this.#getTimeStamp())} ${chalk.magenta('[Debug]')}\n${newContent}`);
}
/**
@@ -135,7 +171,7 @@ export class BushLogger {
*/
public static debugRaw(...content: any): void {
if (!client.config.isDevelopment) return;
- console.log(`${chalk.bgMagenta(this.#getTimeStamp())} ${chalk.magenta('[Debug]')}`, ...content);
+ this.#baseLog('log', `${chalk.bgMagenta(this.#getTimeStamp())} ${chalk.magenta('[Debug]')}`, ...content);
}
/**
@@ -148,7 +184,7 @@ export class BushLogger {
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(
+ this.stdout(
`${chalk.bgGrey(this.#getTimeStamp())} ${chalk.grey(`[${header}]`)} ${this.#parseFormatting(newContent, 'blackBright')}`
);
if (!sendChannel) return;
@@ -168,7 +204,7 @@ export class BushLogger {
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(
+ this.stdout(
`${chalk.bgHex('#949494')(this.#getTimeStamp())} ${chalk.hex('#949494')(`[${header}]`)} ${chalk.hex('#b3b3b3')(newContent)}`
);
}
@@ -180,7 +216,7 @@ export class BushLogger {
*/
public static async superVerboseRaw(header: string, ...content: any[]): Promise<void> {
if (!client.config.logging.verbose) return;
- console.info(`${chalk.bgHex('#a3a3a3')(this.#getTimeStamp())} ${chalk.hex('#a3a3a3')(`[${header}]`)}`, ...content);
+ this.raw(`${chalk.bgHex('#a3a3a3')(this.#getTimeStamp())} ${chalk.hex('#a3a3a3')(`[${header}]`)}`, ...content);
}
/**
@@ -193,7 +229,7 @@ export class BushLogger {
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(
+ this.stdout(
`${chalk.bgCyan(this.#getTimeStamp())} ${chalk.cyan(`[${header}]`)} ${this.#parseFormatting(newContent, 'blueBright')}`
);
if (!sendChannel) return;
@@ -213,7 +249,7 @@ export class BushLogger {
*/
public static async warn(header: string, content: any, sendChannel = true, depth = 0): Promise<void> {
const newContent = this.#inspectContent(content, depth, true);
- console.warn(
+ this.stderr(
`${chalk.bgYellow(this.#getTimeStamp())} ${chalk.yellow(`[${header}]`)} ${this.#parseFormatting(
newContent,
'yellowBright'
@@ -237,7 +273,7 @@ export class BushLogger {
*/
public static async error(header: string, content: any, sendChannel = true, depth = 0): Promise<void> {
const newContent = this.#inspectContent(content, depth, true);
- console.error(
+ this.stderr(
`${chalk.bgRedBright(this.#getTimeStamp())} ${chalk.redBright(`[${header}]`)} ${this.#parseFormatting(
newContent,
'redBright'
@@ -261,7 +297,7 @@ export class BushLogger {
*/
public static async success(header: string, content: any, sendChannel = true, depth = 0): Promise<void> {
const newContent = this.#inspectContent(content, depth, true);
- console.log(
+ this.stdout(
`${chalk.bgGreen(this.#getTimeStamp())} ${chalk.greenBright(`[${header}]`)} ${this.#parseFormatting(
newContent,
'greenBright'
diff --git a/src/listeners/client/ready.ts b/src/listeners/client/ready.ts
index c550c6b..d53ed9b 100644
--- a/src/listeners/client/ready.ts
+++ b/src/listeners/client/ready.ts
@@ -19,7 +19,7 @@ export default class ReadyListener extends BushListener {
userCount = `<<${client.users.cache.size.toLocaleString()}>>`;
void client.logger.success('ready', `Logged in to ${tag} serving ${guildCount} guilds and ${userCount} users.`);
- console.log(
+ client.console.stdout(
chalk.blue(
`------------------------------------------------------------------------------${
client.config.isDevelopment ? '---' : client.config.isBeta ? '----' : ''
diff --git a/src/listeners/other/consoleListener.ts b/src/listeners/other/consoleListener.ts
index 46f869f..15461f2 100644
--- a/src/listeners/other/consoleListener.ts
+++ b/src/listeners/other/consoleListener.ts
@@ -2,7 +2,8 @@ import { BushListener } from '#lib';
import { exec } from 'child_process';
import { promisify } from 'util';
-export default class ConsoleListener extends BushListener {
+// eslint-disable-next-line @typescript-eslint/no-unused-vars
+/* export default */ class ConsoleListener extends BushListener {
public constructor() {
super('console', {
emitter: 'stdin',
diff --git a/src/listeners/track-manual-punishments/modlogSyncBan.ts b/src/listeners/track-manual-punishments/modlogSyncBan.ts
index 7d77a76..b1bdcee 100644
--- a/src/listeners/track-manual-punishments/modlogSyncBan.ts
+++ b/src/listeners/track-manual-punishments/modlogSyncBan.ts
@@ -34,8 +34,9 @@ export default class ModlogSyncBanListener extends BushListener {
if (!first.executor || first.executor?.bot) return;
if (Math.abs(first.createdAt.getTime() - now.getTime()) > Time.Minute) {
- console.log(util.humanizeDuration(Math.abs(first.createdAt.getTime() - now.getTime())));
- throw new Error('Time is off by over a minute');
+ throw new Error(
+ `Time is off by over a minute: ${util.humanizeDuration(Math.abs(first.createdAt.getTime() - now.getTime()))}`
+ );
}
const { log } = await Moderation.createModLogEntry({
diff --git a/src/listeners/track-manual-punishments/modlogSyncKick.ts b/src/listeners/track-manual-punishments/modlogSyncKick.ts
index 7841686..59b43bc 100644
--- a/src/listeners/track-manual-punishments/modlogSyncKick.ts
+++ b/src/listeners/track-manual-punishments/modlogSyncKick.ts
@@ -34,8 +34,9 @@ export default class ModlogSyncKickListener extends BushListener {
if (!first.executor || first.executor?.bot) return;
if (Math.abs(first.createdAt.getTime() - now.getTime()) > Time.Minute) {
- console.log(util.humanizeDuration(Math.abs(first.createdAt.getTime() - now.getTime())));
- throw new Error('Time is off by over a minute');
+ throw new Error(
+ `Time is off by over a minute: ${util.humanizeDuration(Math.abs(first.createdAt.getTime() - now.getTime()))}`
+ );
}
const { log } = await Moderation.createModLogEntry({
diff --git a/src/listeners/track-manual-punishments/modlogSyncTimeout.ts b/src/listeners/track-manual-punishments/modlogSyncTimeout.ts
index 8a2c486..8ad8b79 100644
--- a/src/listeners/track-manual-punishments/modlogSyncTimeout.ts
+++ b/src/listeners/track-manual-punishments/modlogSyncTimeout.ts
@@ -36,8 +36,9 @@ export default class ModlogSyncTimeoutListener extends BushListener {
if (!timeOut) return;
if (Math.abs(first.createdAt.getTime() - now.getTime()) > Time.Minute) {
- console.log(util.humanizeDuration(Math.abs(first.createdAt.getTime() - now.getTime())));
- throw new Error('Time is off by over a minute');
+ throw new Error(
+ `Time is off by over a minute: ${util.humanizeDuration(Math.abs(first.createdAt.getTime() - now.getTime()))}`
+ );
}
const newTime = <string | null>timeOut.new ? new Date(<string>timeOut.new) : null;
diff --git a/src/listeners/track-manual-punishments/modlogSyncUnban.ts b/src/listeners/track-manual-punishments/modlogSyncUnban.ts
index 70c7ba8..1c5de1d 100644
--- a/src/listeners/track-manual-punishments/modlogSyncUnban.ts
+++ b/src/listeners/track-manual-punishments/modlogSyncUnban.ts
@@ -33,8 +33,9 @@ export default class ModlogSyncUnbanListener extends BushListener {
if (!first.executor || first.executor?.bot) return;
if (Math.abs(first.createdAt.getTime() - now.getTime()) > Time.Minute) {
- console.log(util.humanizeDuration(Math.abs(first.createdAt.getTime() - now.getTime())));
- throw new Error('Time is off by over a minute');
+ throw new Error(
+ `Time is off by over a minute: ${util.humanizeDuration(Math.abs(first.createdAt.getTime() - now.getTime()))}`
+ );
}
const { log } = await Moderation.createModLogEntry({