From 1c679bd341bbf56df4996e5029fc8aff3f4197f7 Mon Sep 17 00:00:00 2001 From: EvolutionX <85353424+EvolutionX-10@users.noreply.github.com> Date: Mon, 14 Feb 2022 09:51:51 +0530 Subject: [PATCH] Delete src/handler/Utilities directory --- src/handler/Utilities/Preprocessors/args.ts | 77 --------------------- src/handler/Utilities/messageHelpers.ts | 13 ---- src/handler/Utilities/readFile.ts | 56 --------------- 3 files changed, 146 deletions(-) delete mode 100644 src/handler/Utilities/Preprocessors/args.ts delete mode 100644 src/handler/Utilities/messageHelpers.ts delete mode 100644 src/handler/Utilities/readFile.ts diff --git a/src/handler/Utilities/Preprocessors/args.ts b/src/handler/Utilities/Preprocessors/args.ts deleted file mode 100644 index 8f94b0c..0000000 --- a/src/handler/Utilities/Preprocessors/args.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { Err, Ok, Result } from 'ts-results'; -import type { possibleOutput } from '../../../types/handler'; - -/** - * Wrapper type taking `Ok(T)` or `Err(possibleOutput)` e.g `Result = Result; - -/** - * - * @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); - - 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 - * @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, - }, -): ArgType { - if (arg.match(regexes.yesRegex)) return Ok(true); - if (arg.match(regexes.noRegex)) return Ok(false); - - return Err(onFailure); -} - -/** - * - * @param {string} arg - command arguments - * @param {string} sep - default separator = ' ' - * @returns {Ok} - */ - -export function toArr(arg: string, sep = ' '): ArgType { - return Ok(arg.split(sep)); -} - -/** - * - * @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)); -} - -/** - * - * @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)); -} diff --git a/src/handler/Utilities/messageHelpers.ts b/src/handler/Utilities/messageHelpers.ts deleted file mode 100644 index 6050734..0000000 --- a/src/handler/Utilities/messageHelpers.ts +++ /dev/null @@ -1,13 +0,0 @@ -import type { Message } from 'discord.js'; - -export function isBot(message: Message) { - return message.author.bot; -} - -export function hasPrefix(message: Message, prefix: string) { - return message.content.slice(0, prefix.length).toLowerCase().trim() === prefix; -} - -export function fmt(msg: Message, prefix: string): string[] { - return msg.content.slice(prefix.length).trim().split(/\s+/g); -} diff --git a/src/handler/Utilities/readFile.ts b/src/handler/Utilities/readFile.ts deleted file mode 100644 index f8d694b..0000000 --- a/src/handler/Utilities/readFile.ts +++ /dev/null @@ -1,56 +0,0 @@ -import type { ApplicationCommandOptionData } from 'discord.js'; - -import { readdirSync, statSync } from 'fs'; - -import { basename, join } from 'path'; - -import type * as Sern from '../sern'; - -export type CommandVal = { - mod: Sern.Module; - options: ApplicationCommandOptionData[]; -}; - -export const Commands = new Map(); -export const Alias = new Map(); - -// Courtesy of Townsy#0001 on Discord -async function readPath(dir: string, arrayOfFiles: string[] = []): Promise { - try { - const files = readdirSync(dir); - for (const file of files) { - if (statSync(dir + '/' + file).isDirectory()) await readPath(dir + '/' + file, arrayOfFiles); - else arrayOfFiles.push(join(dir, '/', file)); - } - } catch (err) { - throw err; - } - - return arrayOfFiles; -} - -export const fmtFileName = (n: string) => n.substring(0, n.length - 3); - -/** - * @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 getCommands(dir: string): Promise { - return readPath(join(process.cwd(), dir)); -}