mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
made the code pretty :)
This commit is contained in:
@@ -8,70 +8,70 @@ import type { possibleOutput } from '../../../types/handler';
|
||||
export type ArgType<T> = Result<T, possibleOutput>;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} arg - command arguments
|
||||
*
|
||||
* @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);
|
||||
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<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
|
||||
}
|
||||
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);
|
||||
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<string[]>}
|
||||
*/
|
||||
|
||||
export function toArr(arg: string, sep = ' '): ArgType<string[]> {
|
||||
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<number>}
|
||||
*/
|
||||
|
||||
export function toPositiveInt(arg: string, onFailure: possibleOutput): ArgType<number> {
|
||||
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<number>}
|
||||
*/
|
||||
export function toNegativeInt(arg: string, onFailure: possibleOutput): ArgType<number> {
|
||||
return parseInt(arg, onFailure).andThen(num => Ok(num > 0 ? -num : num));
|
||||
return parseInt(arg, onFailure).andThen((num) => Ok(num > 0 ? -num : num));
|
||||
}
|
||||
|
||||
@@ -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[] {
|
||||
|
||||
@@ -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<unknown>,
|
||||
options: ApplicationCommandOptionData[],
|
||||
}
|
||||
mod: Sern.Module<unknown>;
|
||||
options: ApplicationCommandOptionData[];
|
||||
};
|
||||
|
||||
export const Commands = new Map<string, CommandVal>();
|
||||
export const Alias = new Map<string, CommandVal>();
|
||||
@@ -27,11 +19,9 @@ async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<strin
|
||||
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));
|
||||
}
|
||||
if (statSync(dir + '/' + file).isDirectory()) await readPath(dir + '/' + file, arrayOfFiles);
|
||||
else arrayOfFiles.push(join(dir, '/', file));
|
||||
}
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
@@ -42,21 +32,23 @@ async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<strin
|
||||
export const fmtFileName = (n: string) => 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<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 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[]> {
|
||||
|
||||
@@ -11,10 +11,11 @@ enum sEvent {
|
||||
}
|
||||
|
||||
export default class Logger {
|
||||
|
||||
public clear() { console.clear() }
|
||||
|
||||
public log<T extends sEvent>(e : T, message: string) {
|
||||
public clear() {
|
||||
console.clear();
|
||||
}
|
||||
|
||||
public log<T extends sEvent>(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), {}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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 `<TextChannel>#send` or `<CommandInteraction>#reply`
|
||||
export type possibleOutput<T = string> = T | MessagePayload & MessageOptions;
|
||||
export type possibleOutput<T = string> = T | (MessagePayload & MessageOptions);
|
||||
export type Nullable<T> = T | null;
|
||||
export type delegate = Sern.Module<unknown>['delegate'];
|
||||
|
||||
// Thanks @cursorsdottsx
|
||||
export type ParseType<T> = {
|
||||
[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<Message>,
|
||||
interaction: Option<CommandInteraction>
|
||||
}
|
||||
message: Option<Message>;
|
||||
interaction: Option<CommandInteraction>;
|
||||
};
|
||||
|
||||
export type Arg = ParseType<{ text: string; slash: SlashOptions }>;
|
||||
|
||||
export type Arg = ParseType<{ text: string, slash: SlashOptions }>;
|
||||
|
||||
// TypeAlias for interaction.options
|
||||
export type SlashOptions = Omit<CommandInteractionOptionResolver, 'getMessage' | 'getFocused'>;
|
||||
|
||||
Reference in New Issue
Block a user