From 57392f4e6a8eb64adf3e62dee408e5b407f19803 Mon Sep 17 00:00:00 2001 From: EvolutionX Date: Mon, 14 Feb 2022 09:43:38 +0530 Subject: [PATCH] made the code pretty :) --- src/handler/Utilities/Preprocessors/args.ts | 60 ++++++++++----------- src/handler/Utilities/messageHelpers.ts | 2 +- src/handler/Utilities/readFile.ts | 56 +++++++++---------- src/handler/logger.ts | 35 ++++++------ src/index.ts | 2 +- src/types/handler.ts | 16 +++--- 6 files changed, 82 insertions(+), 89 deletions(-) diff --git a/src/handler/Utilities/Preprocessors/args.ts b/src/handler/Utilities/Preprocessors/args.ts index 2cd02ab..8f94b0c 100644 --- a/src/handler/Utilities/Preprocessors/args.ts +++ b/src/handler/Utilities/Preprocessors/args.ts @@ -8,70 +8,70 @@ import type { possibleOutput } from '../../../types/handler'; export type ArgType = Result; /** - * - * @param {string} arg - command arguments + * + * @param {string} arg - command arguments * @param {possibleOutput} onFailure - if `Number.parseInt` returns NaN * @returns {ArgType} Attempts to use `Number.parseInt()` on `arg` */ export function parseInt(arg: string, onFailure: possibleOutput): ArgType { - const val = Number.parseInt(arg); + const val = Number.parseInt(arg); - return val === NaN ? Err(onFailure) : Ok(val); + return val === NaN ? Err(onFailure) : Ok(val); } /** - * @param {string} arg - command arguments - * @param {possibleOutput} onFailure - If cannot parse `arg` into boolean. - * @param { {yesRegex: RegExp, noRegex: RegExp} } regexes - default regexes: yes : `/^(?:y(?:es)?|👍)$/i`, no : /^(?:n(?:o)?|👎)$/i + * @param {string} arg - command arguments + * @param {possibleOutput} onFailure - If cannot parse `arg` into boolean. + * @param { {yesRegex: RegExp, noRegex: RegExp} } regexes - default regexes: yes : `/^(?:y(?:es)?|👍)$/i`, no : /^(?:n(?:o)?|👎)$/i * @returns { ArgType } attemps to parse `args` as a boolean */ export function parseBool( - arg: string, - onFailure: possibleOutput = `Cannot parse '${arg}' as a boolean`, - regexes: { - yesRegex: RegExp, - noRegex: RegExp - } = { - yesRegex: /^(?:y(?:es)?|👍)$/i, - noRegex: /^(?:n(?:o)?|👎)$/i - } + arg: string, + onFailure: possibleOutput = `Cannot parse '${arg}' as a boolean`, + regexes: { + yesRegex: RegExp; + noRegex: RegExp; + } = { + yesRegex: /^(?:y(?:es)?|👍)$/i, + noRegex: /^(?:n(?:o)?|👎)$/i, + }, ): ArgType { - if (arg.match(regexes.yesRegex)) return Ok(true); - if (arg.match(regexes.noRegex)) return Ok(false); + if (arg.match(regexes.yesRegex)) return Ok(true); + if (arg.match(regexes.noRegex)) return Ok(false); - return Err(onFailure); + return Err(onFailure); } /** - * - * @param {string} arg - command arguments + * + * @param {string} arg - command arguments * @param {string} sep - default separator = ' ' * @returns {Ok} */ export function toArr(arg: string, sep = ' '): ArgType { - return Ok(arg.split(sep)); + return Ok(arg.split(sep)); } /** - * - * @param {string} arg - command arguments - * @param {possibleOutput} onFailure - delegates `Utils.parseInt` + * + * @param {string} arg - command arguments + * @param {possibleOutput} onFailure - delegates `Utils.parseInt` * @returns {ArgType} */ export function toPositiveInt(arg: string, onFailure: possibleOutput): ArgType { - return parseInt(arg, onFailure).andThen(num => Ok(num > 0 ? num : -num)); + return parseInt(arg, onFailure).andThen((num) => Ok(num > 0 ? num : -num)); } /** - * - * @param {string} arg - command arguments - * @param {possibleOutput} onFailure - delegates `parseInt` + * + * @param {string} arg - command arguments + * @param {possibleOutput} onFailure - delegates `parseInt` * @returns {ArgType} */ export function toNegativeInt(arg: string, onFailure: possibleOutput): ArgType { - return parseInt(arg, onFailure).andThen(num => Ok(num > 0 ? -num : num)); + return parseInt(arg, onFailure).andThen((num) => Ok(num > 0 ? -num : num)); } diff --git a/src/handler/Utilities/messageHelpers.ts b/src/handler/Utilities/messageHelpers.ts index cfad6dc..6050734 100644 --- a/src/handler/Utilities/messageHelpers.ts +++ b/src/handler/Utilities/messageHelpers.ts @@ -5,7 +5,7 @@ export function isBot(message: Message) { } export function hasPrefix(message: Message, prefix: string) { - return (message.content.slice(0, prefix.length).toLowerCase().trim()) === prefix; + return message.content.slice(0, prefix.length).toLowerCase().trim() === prefix; } export function fmt(msg: Message, prefix: string): string[] { diff --git a/src/handler/Utilities/readFile.ts b/src/handler/Utilities/readFile.ts index 9a6917f..f8d694b 100644 --- a/src/handler/Utilities/readFile.ts +++ b/src/handler/Utilities/readFile.ts @@ -1,23 +1,15 @@ -import type { - ApplicationCommandOptionData -} from 'discord.js'; +import type { ApplicationCommandOptionData } from 'discord.js'; -import { - readdirSync, - statSync -} from 'fs'; +import { readdirSync, statSync } from 'fs'; -import { - basename, - join -} from 'path'; +import { basename, join } from 'path'; import type * as Sern from '../sern'; export type CommandVal = { - mod: Sern.Module, - options: ApplicationCommandOptionData[], -} + mod: Sern.Module; + options: ApplicationCommandOptionData[]; +}; export const Commands = new Map(); export const Alias = new Map(); @@ -27,11 +19,9 @@ async function readPath(dir: string, arrayOfFiles: string[] = []): Promise n.substring(0, n.length - 3); /** - * @param {Sern.Handler} handler an instance of Sern.Handler + * @param {Sern.Handler} handler an instance of Sern.Handler * @returns {Promise<{ name: string; mod: Sern.Module; absPath: string; }[]>} data from command files -*/ + */ -export async function buildData(handler: Sern.Handler) - : Promise<{ - name: string; - mod: Sern.Module; - absPath: string; - }[]> { - const commandDir = handler.commandDir; - return Promise.all((await getCommands(commandDir)) - .map(async absPath => { - return { name: basename(absPath), mod: (await import(absPath)).default as Sern.Module, absPath } - })); +export async function buildData(handler: Sern.Handler): Promise< + { + name: string; + mod: Sern.Module; + absPath: string; + }[] +> { + const commandDir = handler.commandDir; + return Promise.all( + (await getCommands(commandDir)).map(async (absPath) => { + return { name: basename(absPath), mod: (await import(absPath)).default as Sern.Module, absPath }; + }), + ); } export async function getCommands(dir: string): Promise { diff --git a/src/handler/logger.ts b/src/handler/logger.ts index b5343dd..f4cc0bc 100644 --- a/src/handler/logger.ts +++ b/src/handler/logger.ts @@ -11,10 +11,11 @@ enum sEvent { } export default class Logger { - - public clear() { console.clear() } - - public log(e : T, message: string) { + public clear() { + console.clear(); + } + + public log(e: T, message: string) { dayJS.extend(UTC); dayJS.extend(Timezone); dayJS.tz.guess(); @@ -24,18 +25,18 @@ export default class Logger { } /** - * Utilizes console.table() to print out memory usage of current process. - * Optional at startup. - * + * Utilizes console.table() to print out memory usage of current process. + * Optional at startup. + * */ - - public tableRam() { - console.table( - Object.entries(process.memoryUsage()) - .map(([k, v]: [string, number]) => { - return { [k]: `${((Math.round(v) / 1024 / 1024 * 100) / 100).toFixed(2)} MBs` }; - }) - .reduce(((r, c) => Object.assign(r, c)), {}) - ); - } + + public tableRam() { + console.table( + Object.entries(process.memoryUsage()) + .map(([k, v]: [string, number]) => { + return { [k]: `${(((Math.round(v) / 1024 / 1024) * 100) / 100).toFixed(2)} MBs` }; + }) + .reduce((r, c) => Object.assign(r, c), {}), + ); + } } diff --git a/src/index.ts b/src/index.ts index 8de4cb0..6d1af5e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import * as Sern from './handler/sern'; -import * as Utils from './handler/utilities/preprocessors/args'; +import * as Utils from './handler/Utilities/Preprocessors/args'; import * as Types from './types/handler'; module.exports = { Sern, Utils, Types }; diff --git a/src/types/handler.ts b/src/types/handler.ts index d1797ad..340620f 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -5,30 +5,30 @@ import type { CommandInteractionOptionResolver, Message, MessagePayload, - MessageOptions + MessageOptions, } from 'discord.js'; import type * as Sern from '../handler/sern'; export type Visibility = 'private' | 'public'; // Anything that can be sent in a `#send` or `#reply` -export type possibleOutput = T | MessagePayload & MessageOptions; +export type possibleOutput = T | (MessagePayload & MessageOptions); export type Nullable = T | null; export type delegate = Sern.Module['delegate']; // Thanks @cursorsdottsx export type ParseType = { [K in keyof T]: T[K] extends unknown ? [k: K, args: T[K]] : never; -} [keyof T]; +}[keyof T]; // A Sern.Module['delegate'] will carry a Context Parameter export type Context = { - message: Option, - interaction: Option -} + message: Option; + interaction: Option; +}; + +export type Arg = ParseType<{ text: string; slash: SlashOptions }>; -export type Arg = ParseType<{ text: string, slash: SlashOptions }>; - // TypeAlias for interaction.options export type SlashOptions = Omit;