mirror of
https://github.com/SrIzan10/handler.git
synced 2026-05-01 10:45:17 +00:00
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { readdirSync, statSync } from 'fs';
|
|
import { join } from 'path';
|
|
import type { Module } from '../structures/commands/module';
|
|
import { SernError } from '../structures/errors';
|
|
|
|
export const ContextMenuUser = new Map<string, Module>();
|
|
export const ContextMenuMsg = new Map<string, Module>();
|
|
export const Commands = new Map<string, Module>();
|
|
export const Alias = new Map<string, Module>();
|
|
|
|
// Courtesy @Townsy45
|
|
function readPath(dir: string, arrayOfFiles: string[] = []): string[] {
|
|
try {
|
|
const files = readdirSync(dir);
|
|
for (const file of files) {
|
|
if (statSync(dir + '/' + file).isDirectory()) 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 {commandsDir} Relative path to commands directory
|
|
* @returns {Promise<{ mod: Command; absPath: string; }[]>} data from command files
|
|
*/
|
|
|
|
export async function buildData(commandDir: string ): Promise<
|
|
{
|
|
mod: Module;
|
|
absPath: string;
|
|
}[]
|
|
> {
|
|
return Promise.all(
|
|
getCommands(commandDir).map( async (absPath) => {
|
|
const mod = (await import(absPath)).module as Module;
|
|
if (mod === undefined) throw Error(`${SernError.UNDEFINED_MODULE} ${absPath}`);
|
|
return { mod, absPath };
|
|
}),
|
|
);
|
|
}
|
|
|
|
export function getCommands(dir: string): string[] {
|
|
return readPath(join(process.cwd(), dir));
|
|
}
|