feat : refactor command objects. Deciding not to use a class

This commit is contained in:
Jacob Nguyen
2022-03-13 23:05:53 -05:00
parent 586d386afb
commit 5a1870be73
4 changed files with 32 additions and 69 deletions

View File

@@ -1,20 +0,0 @@
import type { ApplicationCommandOptionData } from 'discord.js';
import type { CommandType } from '../../sern';
/**
* An object that gets imported and acts as a command.
* @typedef {object} Module<T=string>
* @property {string} desc
* @property {CommandType} type
* @property {(eventParams : Context, args : Ok<T=string) => Awaitable<possibleOutput | void>)} execute
* @prop {(ctx: Context, args: Arg) => Utils.ArgType<T>} parse
*/
interface CommandOptions {
commandType : CommandType,
alias : string[] | [],
options?: ApplicationCommandOptionData[],
name? : string | undefined
}
export default CommandOptions;

View File

@@ -1,48 +0,0 @@
import type { ApplicationCommandOptionData, 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 { CommandType } from "../../sern";
import { SernError } from "../errors";
import type CommandOptions from "./command-options";
export abstract class Command {
private name? : string | undefined;
private ctx : Context = new Context( None, None );
private commandType : CommandType;
private options : ApplicationCommandOptionData[] | undefined;
private alias : string[] | undefined;
private constructor (
{ name, commandType, options, alias } : CommandOptions
) {
this.name = name;
this.commandType = commandType;
switch ( commandType ) {
case CommandType.TEXT : {
this.options = undefined;
this.alias = alias;
} break;
case CommandType.SLASH : {
if(alias.length < 0) throw Error(SernError.NO_ALIAS);
this.options = options;
} break;
case CommandType.BOTH : {
this.options = options;
this.alias = alias;
} break
}
}
abstract execute<T> ( args: Ok<T> ) : Awaitable<possibleOutput | void>;
abstract parse?<T> ( args: Arg ) : Utils.ArgType<T>;
public setCtx ( context: Context ) { this.ctx = context; }
public getName () { return this.name; }
public getCommandType () { return this.commandType; }
public getOptions() { return this.options; }
public getAlias() { return this.alias }
}

View File

@@ -0,0 +1,32 @@
import type { ApplicationCommandOptionData, Awaitable } from "discord.js";
import type { possibleOutput } from "../../../types/handler";
import type { CommandType } from "../../sern";
interface BaseModule {
name? : string;
description : string;
execute() : Awaitable<possibleOutput | void>
plugins? : [] //TODO
}
type TextCommand = { moduleType : CommandType.TEXT; alias : string[] | [] };
type SlashCommand = { moduleType : CommandType.SLASH; options : ApplicationCommandOptionData[] | [] };
type BothCommand = { moduleType : CommandType.BOTH; alias : string[] | []; options : ApplicationCommandOptionData[] | [] }
export type Module =
BaseModule & (
TextCommand
| SlashCommand
| BothCommand
);