mirror of
https://github.com/SrIzan10/handler.git
synced 2026-05-01 10:45:17 +00:00
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import type { ApplicationCommandOptionData } from 'discord.js';
|
|
import type * as Sern from '../sern';
|
|
import type Module from '../structures/module';
|
|
|
|
import { readdirSync, statSync } from 'fs';
|
|
import { basename, join } from 'path';
|
|
|
|
export type CommandVal = {
|
|
mod: Module<unknown> & { name : string };
|
|
options: ApplicationCommandOptionData[];
|
|
};
|
|
|
|
export const Commands = new Map<string, CommandVal>();
|
|
export const Alias = new Map<string, CommandVal>();
|
|
|
|
// Courtesy @Townsy45
|
|
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: Module<unknown>; absPath: string; }[]>} data from command files
|
|
*/
|
|
|
|
export async function buildData(handler: Sern.Handler): Promise<
|
|
{
|
|
name: string;
|
|
mod: 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 Module<unknown>, absPath };
|
|
}),
|
|
);
|
|
}
|
|
|
|
export async function getCommands(dir: string): Promise<string[]> {
|
|
return readPath(join(process.cwd(), dir));
|
|
}
|