mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
Delete src/handler/Utilities directory
This commit is contained in:
@@ -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<T, possibleOutput`
|
||||
*/
|
||||
|
||||
export type ArgType<T> = Result<T, possibleOutput>;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} arg - command arguments
|
||||
* @param {possibleOutput} onFailure - if `Number.parseInt` returns NaN
|
||||
* @returns {ArgType<number>} Attempts to use `Number.parseInt()` on `arg`
|
||||
*/
|
||||
|
||||
export function parseInt(arg: string, onFailure: possibleOutput): ArgType<number> {
|
||||
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<boolean> } 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<boolean> {
|
||||
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<string[]>}
|
||||
*/
|
||||
|
||||
export function toArr(arg: string, sep = ' '): ArgType<string[]> {
|
||||
return Ok(arg.split(sep));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} arg - command arguments
|
||||
* @param {possibleOutput} onFailure - delegates `Utils.parseInt`
|
||||
* @returns {ArgType<number>}
|
||||
*/
|
||||
|
||||
export function toPositiveInt(arg: string, onFailure: possibleOutput): ArgType<number> {
|
||||
return parseInt(arg, onFailure).andThen((num) => Ok(num > 0 ? num : -num));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} arg - command arguments
|
||||
* @param {possibleOutput} onFailure - delegates `parseInt`
|
||||
* @returns {ArgType<number>}
|
||||
*/
|
||||
export function toNegativeInt(arg: string, onFailure: possibleOutput): ArgType<number> {
|
||||
return parseInt(arg, onFailure).andThen((num) => Ok(num > 0 ? -num : num));
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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<unknown>;
|
||||
options: ApplicationCommandOptionData[];
|
||||
};
|
||||
|
||||
export const Commands = new Map<string, CommandVal>();
|
||||
export const Alias = new Map<string, CommandVal>();
|
||||
|
||||
// Courtesy of Townsy#0001 on Discord
|
||||
async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<string[]> {
|
||||
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<unknown>; absPath: string; }[]>} data from command files
|
||||
*/
|
||||
|
||||
export async function buildData(handler: Sern.Handler): Promise<
|
||||
{
|
||||
name: string;
|
||||
mod: Sern.Module<unknown>;
|
||||
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<unknown>, absPath };
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getCommands(dir: string): Promise<string[]> {
|
||||
return readPath(join(process.cwd(), dir));
|
||||
}
|
||||
Reference in New Issue
Block a user