diff options
author | TymanWasTaken <tyman@tyman.tech> | 2021-05-16 20:30:34 -0400 |
---|---|---|
committer | TymanWasTaken <tyman@tyman.tech> | 2021-05-16 20:30:34 -0400 |
commit | 372718e567e060cead16dde5d6d190666b4dd575 (patch) | |
tree | 1fad29305b6277838833a7e8ae4381136212f301 /src/lib | |
parent | 1db014860c3cf6070bb29f75b6a8cf08070e5b9a (diff) | |
download | tanzanite-372718e567e060cead16dde5d6d190666b4dd575.tar.gz tanzanite-372718e567e060cead16dde5d6d190666b4dd575.tar.bz2 tanzanite-372718e567e060cead16dde5d6d190666b4dd575.zip |
add colored logging and improved logging code, fix a few moderation command issues, add more logging, and make ban check run every 30s not 60s
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/extensions/BotClient.ts | 40 | ||||
-rw-r--r-- | src/lib/extensions/BotCommand.ts | 5 | ||||
-rw-r--r-- | src/lib/extensions/Util.ts | 49 | ||||
-rw-r--r-- | src/lib/utils/Logger.ts | 44 |
4 files changed, 66 insertions, 72 deletions
diff --git a/src/lib/extensions/BotClient.ts b/src/lib/extensions/BotClient.ts index e2de812..99b40ef 100644 --- a/src/lib/extensions/BotClient.ts +++ b/src/lib/extensions/BotClient.ts @@ -15,27 +15,11 @@ import * as Tasks from '../../tasks'; import { v4 as uuidv4 } from 'uuid'; import { exit } from 'process'; import { Intents } from 'discord.js'; +import * as config from '../../config/options'; +import { Logger } from '../utils/Logger'; +import chalk from 'chalk'; -export interface BotConfig { - credentials: { - botToken: string; - }; - owners: string[]; - prefix: string; - dev: boolean; - db: { - username: string; - password: string; - host: string; - port: number; - }; - channels: { - log: string; - error: string; - dm: string; - command: string; - }; -} +export type BotConfig = typeof config; export class BotClient extends AkairoClient { public config: BotConfig; @@ -45,6 +29,7 @@ export class BotClient extends AkairoClient { public util: Util; public ownerID: string[]; public db: Sequelize; + public logger: Logger; constructor(config: BotConfig) { super( { @@ -114,6 +99,7 @@ export class BotClient extends AkairoClient { ); BotGuild.install(); BotMessage.install(); + this.logger = new Logger(this); } // Initialize everything @@ -134,14 +120,20 @@ export class BotClient extends AkairoClient { for (const loader of Object.keys(loaders)) { try { loaders[loader].loadAll(); - console.log('Successfully loaded ' + loader + '.'); + this.logger.log( + chalk.green('Successfully loaded ' + chalk.cyan(loader) + '.') + ); } catch (e) { - console.error('Unable to load loader ' + loader + ' with error ' + e); + console.error( + chalk.red( + 'Unable to load loader ' + chalk.cyan(loader) + ' with error ' + e + ) + ); } } await this.dbPreInit(); Object.keys(Tasks).forEach((t) => { - setInterval(() => Tasks[t](this), 60000); + setInterval(() => Tasks[t](this), 30000); }); } @@ -270,7 +262,7 @@ export class BotClient extends AkairoClient { await this._init(); await this.login(this.token); } catch (e) { - console.error(e.stack); + console.error(chalk.red(e.stack)); exit(2); } } diff --git a/src/lib/extensions/BotCommand.ts b/src/lib/extensions/BotCommand.ts index 2db93b0..c5d31e9 100644 --- a/src/lib/extensions/BotCommand.ts +++ b/src/lib/extensions/BotCommand.ts @@ -4,6 +4,11 @@ import { BotClient } from './BotClient'; export interface BotCommandOptions extends CommandOptions { slashCommandOptions?: APIApplicationCommandOption[]; + description: { + content: string; + usage: string; + examples: string[]; + }; } export class BotCommand extends Command { diff --git a/src/lib/extensions/Util.ts b/src/lib/extensions/Util.ts index 4243ebf..661392f 100644 --- a/src/lib/extensions/Util.ts +++ b/src/lib/extensions/Util.ts @@ -1,12 +1,9 @@ import { ClientUtil } from 'discord-akairo'; import { BotClient } from './BotClient'; -import { User } from 'discord.js'; import { promisify } from 'util'; import { exec } from 'child_process'; import got from 'got'; -import { TextChannel } from 'discord.js'; -import { MessageEmbed } from 'discord.js'; -import { GuildMember } from 'discord.js'; +import { MessageEmbed, GuildMember, User } from 'discord.js'; interface hastebinRes { key: string; @@ -120,14 +117,6 @@ export class Util extends ClientUtil { } /** - * Logs something but only in dev mode - * @param content The thing to log - */ - public devLog(content: unknown): void { - if (this.client.config.dev) console.log(content); - } - - /** * Resolves a user-provided string into a user object, if possible * @param text The text to try and resolve * @returns The user resolved or null @@ -184,42 +173,6 @@ export class Util extends ClientUtil { } /** - * Logs a message to console and log channel as info - * @param message The message to send - */ - public async info(message: string): Promise<void> { - console.log(`INFO: ${message}`); - const channel = (await this.client.channels.fetch( - this.client.config.channels.log - )) as TextChannel; - await channel.send(`INFO: ${message}`); - } - - /** - * Logs a message to console and log channel as a warning - * @param message The message to send - */ - public async warn(message: string): Promise<void> { - console.warn(`WARN: ${message}`); - const channel = (await this.client.channels.fetch( - this.client.config.channels.log - )) as TextChannel; - await channel.send(`WARN: ${message}`); - } - - /** - * Logs a message to console and log channel as an error - * @param message The message to send - */ - public async error(message: string): Promise<void> { - console.error(`ERROR: ${message}`); - const channel = (await this.client.channels.fetch( - this.client.config.channels.error - )) as TextChannel; - await channel.send(`ERROR: ${message}`); - } - - /** * The colors used throught the bot */ public colors = { diff --git a/src/lib/utils/Logger.ts b/src/lib/utils/Logger.ts new file mode 100644 index 0000000..455ba36 --- /dev/null +++ b/src/lib/utils/Logger.ts @@ -0,0 +1,44 @@ +import { TextChannel } from 'discord.js'; +import { BotClient } from '../extensions/BotClient'; +import chalk from 'chalk'; + +export class Logger { + private client: BotClient; + public constructor(client: BotClient) { + 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.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)); + } + } +} |