feat(command.ts) Add command class

This commit is contained in:
Jacob Nguyen
2022-03-11 15:07:43 -06:00
parent 30b7f59d4f
commit f1ffff48c8
3 changed files with 34 additions and 8 deletions

View File

@@ -0,0 +1,26 @@
import type { Awaitable } from "discord.js";
import type { possibleOutput, Arg } from "../../../types/handler";
import Context from "../context";
import type * as Utils from '../../utilities/preprocessors/args';
import { None, Ok } from "ts-results";
import type { CommandType } from "../../sern";
export abstract class Command {
protected name : string | undefined;
protected _ctx : Context = new Context( None, None );
protected commandType : CommandType;
protected constructor (
name : string | undefined,
commandType : CommandType
) {
this.name = name;
this.commandType = commandType;
}
abstract execute<T> ( args: Ok<T> ) : Awaitable<possibleOutput | void>;
abstract parse?<T> (args: Arg) : Utils.ArgType<T>;
private set ctx ( context: Context ) { this._ctx = context; }
}

View File

@@ -2,15 +2,16 @@ import type { ApplicationCommandOptionData } from 'discord.js';
import type Module from '../structures/module';
import { readdirSync, statSync } from 'fs';
import { basename, join } from 'path';
import { join } from 'path';
import type { Command } from '../structures/commands/command';
export type CommandVal = {
mod: Module<unknown> & { name : string };
mod: Command;
options: ApplicationCommandOptionData[];
};
export const Commands = new Map<string, CommandVal>();
export const Alias = new Map<string, CommandVal>();
export const Alias = new Map<string, Exclude< CommandVal, 'options' >>();
// Courtesy @Townsy45
function readPath(dir: string, arrayOfFiles: string[] = []): string[] {
@@ -31,20 +32,19 @@ 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
* @param {commandsDir} Relative path to commands directory
* @returns {Promise<{ mod: Command; absPath: string; }[]>} data from command files
*/
export async function buildData(commandDir: string ): Promise<
{
name: string;
mod: Module<unknown>;
mod: Command;
absPath: string;
}[]
> {
return Promise.all(
getCommands(commandDir).map( async (absPath) => {
return { name: basename(absPath), mod: (await import(absPath)).default as Module<unknown>, absPath };
return { mod: (await import(absPath)).default as Command, absPath };
}),
);
}