feat(commands) create module handler to deal with incoming data

This commit is contained in:
Jacob Nguyen
2022-03-16 00:34:05 -05:00
parent 3178f18d56
commit 4102c7102d
3 changed files with 24 additions and 34 deletions

View File

@@ -4,19 +4,18 @@ import type { CommandType } from "../../sern";
interface BaseModule {
export interface BaseModule {
name? : string;
description : string;
execute() : Awaitable<possibleOutput | void>
}
export type Text= { type : CommandType.TEXT; alias : string[] | [] };
export type Slash={ type : CommandType.SLASH; options : ApplicationCommandOptionData[] | [] };
export type Both= { type : CommandType.BOTH; alias : string[] | []; options : ApplicationCommandOptionData[] | [] }
export type Text = { type : CommandType.TEXT; alias : string[] | [] };
export type Slash = { type : CommandType.SLASH; options : ApplicationCommandOptionData[] | [] };
export type Both = { type : CommandType.BOTH; alias : string[] | []; options : ApplicationCommandOptionData[] | [] }
export type Module =
BaseModule
& Slash | Both | Text;
(BaseModule & Slash) | (BaseModule & Both) | (BaseModule & Text);
@@ -26,4 +25,3 @@ export type Module =

View File

@@ -1,30 +1,22 @@
import { CommandType } from "../../sern";
import { SernError } from "../errors";
import type { Modules } from "../structxports";
import type { Text, Both, Slash, BaseModule } from "./module";
export type TextAction = ( mod : Modules.Text ) => unknown;
export type BothAction = ( mod : Modules.Both) => unknown;
export type SlashAction= ( mod : Modules.Slash) => unknown;
export type Action =
TextAction
| BothAction
| SlashAction;
export function onModule<T extends Modules.Module> ( mod: T, action : Action ) : unknown {
switch (mod.type) {
case CommandType.TEXT : {
return (action as TextAction)(mod);
}
case CommandType.SLASH : {
return (action as SlashAction)(mod);
}
case CommandType.BOTH : {
return (action as BothAction)(mod);
}
default : throw Error(SernError.NOT_VALID_MOD_TYPE);
}
//https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union
// Explicit Module Definitions for mapping
export type ModuleDefs = {
[CommandType.TEXT] : Text & BaseModule,
[CommandType.SLASH] : Slash & BaseModule,
[CommandType.BOTH] : Both & BaseModule,
}
//Keys of ModuleDefs
export type ModuleType = keyof ModuleDefs;
// The keys mapped to a constructed union with its type
export type ModuleStates = { [ K in ModuleType ] : { type : K } & ModuleDefs[K] };
// A handler callback that is called on each ModuleDef
export type HandlerCallback<K extends ModuleType>= ( params : ModuleStates[K] ) => unknown;
//An object that acts as the mapped object to handler
export type ModuleHandlers = { [K in ModuleType] : HandlerCallback<K> };

View File

@@ -1,5 +1,5 @@
import Context from './context';
import * as ModuleTypes from './commands/module';
import * as Modules from './commands/module';
import type Wrapper from './wrapper';
export { Context, ModuleTypes, Wrapper };
export { Context, Modules, Wrapper };