diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/extensions/BushClient.ts | 29 | ||||
-rw-r--r-- | src/lib/extensions/Util.ts | 71 | ||||
-rw-r--r-- | src/lib/models/Guild.ts | 5 | ||||
-rw-r--r-- | src/lib/models/Modlog.ts | 5 | ||||
-rw-r--r-- | src/lib/utils/AllowedMentions.ts | 6 | ||||
-rw-r--r-- | src/lib/utils/Console.ts | 94 |
6 files changed, 118 insertions, 92 deletions
diff --git a/src/lib/extensions/BushClient.ts b/src/lib/extensions/BushClient.ts index 99d0dd3..2b674b1 100644 --- a/src/lib/extensions/BushClient.ts +++ b/src/lib/extensions/BushClient.ts @@ -1,10 +1,4 @@ -import { - AkairoClient, - CommandHandler, - InhibitorHandler, - ListenerHandler, - TaskHandler -} from 'discord-akairo'; +import { AkairoClient, CommandHandler, InhibitorHandler, ListenerHandler, TaskHandler } from 'discord-akairo'; import { Guild } from 'discord.js'; import * as path from 'path'; import { Sequelize } from 'sequelize'; @@ -89,17 +83,12 @@ export class BushClient extends AkairoClient { }); this.util = new Util(this); - this.db = new Sequelize( - this.config.dev ? 'bushbot-dev' : 'bushbot', - this.config.db.username, - this.config.db.password, - { - dialect: 'postgres', - host: this.config.db.host, - port: this.config.db.port, - logging: false - } - ); + this.db = new Sequelize(this.config.dev ? 'bushbot-dev' : 'bushbot', this.config.db.username, this.config.db.password, { + dialect: 'postgres', + host: this.config.db.host, + port: this.config.db.port, + logging: false + }); this.logger = new Logger(this); } @@ -124,9 +113,7 @@ export class BushClient extends AkairoClient { loaders[loader].loadAll(); this.logger.log(chalk.green('Successfully loaded ' + chalk.cyan(loader) + '.')); } catch (e) { - console.error( - chalk.red('Unable to load loader ' + chalk.cyan(loader) + ' with error ' + e) - ); + console.error(chalk.red('Unable to load loader ' + chalk.cyan(loader) + ' with error ' + e)); } } this.taskHandler.startAll(); diff --git a/src/lib/extensions/Util.ts b/src/lib/extensions/Util.ts index 7bedfc1..431410c 100644 --- a/src/lib/extensions/Util.ts +++ b/src/lib/extensions/Util.ts @@ -5,12 +5,7 @@ import { exec } from 'child_process'; import got from 'got'; import { MessageEmbed, GuildMember, User } from 'discord.js'; import { CommandInteractionOption } from 'discord.js'; -import { - ApplicationCommandOptionType, - APIInteractionDataResolvedGuildMember, - APIInteractionDataResolvedChannel, - APIRole -} from 'discord-api-types'; +import { ApplicationCommandOptionType, APIInteractionDataResolvedGuildMember, APIInteractionDataResolvedChannel, APIRole } from 'discord-api-types'; import { GuildChannel } from 'discord.js'; import { Role } from 'discord.js'; import chalk from 'chalk'; @@ -223,20 +218,13 @@ export class Util extends ClientUtil { author = author.user; // Convert to User if GuildMember } let embed = new MessageEmbed().setTimestamp(); - if (author) - embed = embed.setAuthor( - author.username, - author.displayAvatarURL({ dynamic: true }), - `https://discord.com/users/${author.id}` - ); + if (author) embed = embed.setAuthor(author.username, author.displayAvatarURL({ dynamic: true }), `https://discord.com/users/${author.id}`); if (color) embed = embed.setColor(color); return embed; } public async mcUUID(username: string): Promise<string> { - const apiRes = (await got - .get(`https://api.ashcon.app/mojang/v2/user/${username}`) - .json()) as uuidRes; + const apiRes = (await got.get(`https://api.ashcon.app/mojang/v2/user/${username}`).json()) as uuidRes; return apiRes.uuid.replace(/-/g, ''); } @@ -244,24 +232,11 @@ export class Util extends ClientUtil { let fetchedGuild: Guild; if (guild) fetchedGuild = this.client.guilds.cache.get(guild); try { - const registered = - guild === undefined - ? await this.client.application.commands.fetch() - : await fetchedGuild.commands.fetch(); + const registered = guild === undefined ? await this.client.application.commands.fetch() : await fetchedGuild.commands.fetch(); for (const [, registeredCommand] of registered) { - if ( - !this.client.commandHandler.modules.find((cmd) => cmd.id == registeredCommand.name) - ?.execSlash || - force - ) { - guild === undefined - ? await this.client.application.commands.delete(registeredCommand.id) - : await fetchedGuild.commands.delete(registeredCommand.id); - this.client.logger.verbose( - chalk`{red Deleted slash command ${registeredCommand.name}${ - guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' - }}` - ); + if (!this.client.commandHandler.modules.find((cmd) => cmd.id == registeredCommand.name)?.execSlash || force) { + guild === undefined ? await this.client.application.commands.delete(registeredCommand.id) : await fetchedGuild.commands.delete(registeredCommand.id); + this.client.logger.verbose(chalk`{red Deleted slash command ${registeredCommand.name}${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}}`); } } @@ -277,40 +252,20 @@ export class Util extends ClientUtil { if (found?.id && !force) { if (slashdata.description !== found.description) { - guild === undefined - ? await this.client.application.commands.edit(found.id, slashdata) - : fetchedGuild.commands.edit(found.id, slashdata); - this.client.logger.verbose( - chalk`{yellow Edited slash command ${BushCommand.id}${ - guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' - }}` - ); + guild === undefined ? await this.client.application.commands.edit(found.id, slashdata) : fetchedGuild.commands.edit(found.id, slashdata); + this.client.logger.verbose(chalk`{yellow Edited slash command ${BushCommand.id}${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}}`); } } else { - guild === undefined - ? await this.client.application.commands.create(slashdata) - : fetchedGuild.commands.create(slashdata); - this.client.logger.verbose( - chalk`{green Created slash command ${BushCommand.id}${ - guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' - }}` - ); + guild === undefined ? await this.client.application.commands.create(slashdata) : fetchedGuild.commands.create(slashdata); + this.client.logger.verbose(chalk`{green Created slash command ${BushCommand.id}${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}}`); } } } - return this.client.logger.log( - chalk.green( - `Slash commands registered${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}` - ) - ); + return this.client.logger.log(chalk.green(`Slash commands registered${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}`)); } catch (e) { console.log(chalk.red(e.stack)); - return this.client.logger.error( - chalk`{red Slash commands not registered${ - guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' - }, see above error.}` - ); + return this.client.logger.error(chalk`{red Slash commands not registered${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}, see above error.}`); } } diff --git a/src/lib/models/Guild.ts b/src/lib/models/Guild.ts index c224bf5..7902461 100644 --- a/src/lib/models/Guild.ts +++ b/src/lib/models/Guild.ts @@ -8,10 +8,7 @@ export interface GuildModel { } export type GuildModelCreationAttributes = Optional<GuildModel, 'prefix'>; -export class Guild - extends BaseModel<GuildModel, GuildModelCreationAttributes> - implements GuildModel -{ +export class Guild extends BaseModel<GuildModel, GuildModelCreationAttributes> implements GuildModel { id: string; prefix: string; static initModel(seqeulize: Sequelize, client: BushClient): void { diff --git a/src/lib/models/Modlog.ts b/src/lib/models/Modlog.ts index 52a13b2..2b2050a 100644 --- a/src/lib/models/Modlog.ts +++ b/src/lib/models/Modlog.ts @@ -32,10 +32,7 @@ export interface ModlogModelCreationAttributes { guild: string; } -export class Modlog - extends BaseModel<ModlogModel, ModlogModelCreationAttributes> - implements ModlogModel -{ +export class Modlog extends BaseModel<ModlogModel, ModlogModelCreationAttributes> implements ModlogModel { id: string; type: ModlogType; user: string; diff --git a/src/lib/utils/AllowedMentions.ts b/src/lib/utils/AllowedMentions.ts index 47e440b..68c44d7 100644 --- a/src/lib/utils/AllowedMentions.ts +++ b/src/lib/utils/AllowedMentions.ts @@ -34,11 +34,7 @@ export default class AllowedMentions { } public toObject(): MessageMentionOptions { return { - parse: [ - ...(this.users ? ['users'] : []), - ...(this.roles ? ['roles'] : []), - ...(this.everyone ? ['everyone'] : []) - ] as MessageMentionTypes[] + parse: [...(this.users ? ['users'] : []), ...(this.roles ? ['roles'] : []), ...(this.everyone ? ['everyone'] : [])] as MessageMentionTypes[] }; } } diff --git a/src/lib/utils/Console.ts b/src/lib/utils/Console.ts new file mode 100644 index 0000000..36c078c --- /dev/null +++ b/src/lib/utils/Console.ts @@ -0,0 +1,94 @@ +import chalk from 'chalk'; +import { BushClient } from '../extensions/BushClient'; + +export class CustomConsole { + private client: BushClient; + public constructor(client: BushClient) { + this.client = client; + } + + private parseColors(content: any, color: 'blueBright' | 'blackBright' | 'redBright' | 'yellowBright' | 'greenBright'): string|any { + if (typeof content === 'string') { + const newContent: Array<string> = content.split(/<<|>>/); + const tempParsedArray: Array<string> = []; + newContent.forEach((value, index) => { + if (index % 2 !== 0) { + tempParsedArray.push(chalk[color](value)); + } else { + tempParsedArray.push(value); + } + }); + return tempParsedArray.join(''); + } else { + return content + } + + } + + private timeStamp(): 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}`; + } + + /** + * Logs debug information. + * @param content - The content to log. + */ + public debug(content: any): void { + console.log(`${chalk.bgGrey(this.timeStamp())} ${chalk.grey('[Debug]')}`, content); + } + + /** + * Logs verbose information. Highlight information by surrounding it in `<<>>`. + * @param header - The header displayed before the content, displayed in grey. + * @param content - The content to log, highlights displayed in bright black. + */ + public verbose(header: string, content: string): void { + return console.info(`${chalk.bgGrey(this.timeStamp())} ${chalk.grey(`[${header}]`)} `+this.parseColors(content, '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. + */ + public info(header: string, content: string): void { + return console.info(`${chalk.bgCyan(this.timeStamp())} ${chalk.cyan(`[${header}]`)} `+this.parseColors(content, 'blueBright')); + } + + /** + * 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. + */ + public warn(header: string, content: string): void { + return console.warn(`${chalk.bgYellow(this.timeStamp())} ${chalk.yellow(`[${header}]`)} `+this.parseColors(content, 'yellowBright')); + } + + /** + * 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. + */ + public error(header: string, content: string): void { + return console.error(`${chalk.bgRedBright(this.timeStamp())} ${chalk.redBright(`[${header}]`)} `+ this.parseColors(content, 'redBright')); + } + + /** + * 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. + */ + public success(header: string, content: string): void { + return console.log(`${chalk.bgGreen(this.timeStamp())} ${chalk.greenBright(`[${header}]`)} `+this.parseColors(content, 'greenBright')); + } +} |