feat : plugin development and ensuring all module objs have name

property
This commit is contained in:
Jacob Nguyen
2022-04-09 13:12:49 -05:00
parent 490d6403d7
commit 8866bff576
4 changed files with 68 additions and 12 deletions

View File

@@ -20,27 +20,27 @@ export const onReady = ( wrapper : Wrapper ) => {
// Refactor : ? Possibly repetitive and verbose.
const handler = ( name : string ) =>
({
[CommandType.TEXT] : mod => {
[CommandType.Text] : mod => {
mod.alias.forEach ( a => Files.Alias.set(a,mod));
Files.Commands.set( name, mod );
},
[CommandType.SLASH]: mod => {
[CommandType.Slash]: mod => {
Files.Commands.set( name , mod);
},
[CommandType.BOTH] : mod => {
[CommandType.Both] : mod => {
Files.Commands.set ( name, mod);
mod.alias.forEach (a => Files.Alias.set(a, mod));
},
[CommandType.MENU_USER] : mod => {
[CommandType.MenuUser] : mod => {
Files.ContextMenuUser.set ( name, mod );
},
[CommandType.MENU_MSG] : mod => {
[CommandType.MenuMsg] : mod => {
Files.ContextMenuMsg.set (name, mod );
},
[CommandType.BUTTON] : mod => {
[CommandType.Button] : mod => {
Files.Buttons.set(name, mod);
},
[CommandType.MENU_SELECT] : mod => {
[CommandType.MenuSelect] : mod => {
Files.SelectMenus.set(name, mod);
},
} as ModuleHandlers);
@@ -50,11 +50,17 @@ const registerModules = <T extends ModuleType >(name : string, mod : ModuleState
function setCommands ( { mod, absPath } : { mod : Module, absPath : string } ) {
const name = mod.name ?? Files.fmtFileName(basename(absPath));
registerModules(name, mod);
// making all modules have name property
if (mod.name === undefined ) {
registerModules(name, { name, ...mod });
} else {
registerModules(name, mod);
}
}
function createCommandCache(
arr: {mod: Module, absPath: string}[]
) {
// possible mem leak?
from(arr).subscribe ( setCommands );
}

View File

@@ -0,0 +1,50 @@
//
// Plugins can be inserted on all commands and are emitted
//
// 1.) on ready event, where all commands are loaded.
// 2.) on corresponding observable (command triggers)
//
// The goal of plugins is to organize commands and
// provide extensions to repetitive patterns
// examples include refreshing modules,
// categorizing commands, cooldowns, permissions, etc
// Plugins are reminisce of middleware in express.
//
//
import type { Err, Ok, Result } from "ts-results";
import type { Override } from "../..";
import type { BaseModule } from "../structures/modules/module";
export enum PluginType {
Command = 0b00,
Event = 0b01
}
interface Controller {
next : () => Ok<void>,
stop : () => Err<void>
}
type executePlugin = { execute : ( controller : Controller ) => Result<void, void> }
interface BasePlugin extends Override<BaseModule, executePlugin>{
type : PluginType
}
type CommandPlugin = {
type : PluginType.Command
} & BasePlugin;
type EventPlugin = {
type : PluginType.Event
} & BasePlugin;
export type Plugin =
CommandPlugin
| EventPlugin;

View File

@@ -12,8 +12,6 @@ import { SernError } from './structures/errors';
import { onReady } from './events/readyEvent';
import { onMessageCreate } from './events/messageEvent';
import { onInteractionCreate } from './events/interactionCreate';
import type { Module } from '..';
import { Modified, Modifiers } from './structures/modifiers';
export function init( wrapper : Wrapper ) {
const { events, client } = wrapper;
@@ -26,7 +24,7 @@ export function init( wrapper : Wrapper ) {
//TODO : Add event listener for any other generic node js event emitter
function eventObserver(client: Client, events: DiscordEvent[] ) {
events.forEach( ( [event, cb] ) => {
if (event === 'ready') throw Error(SernError.RESERVED_EVENT);
if (event === 'ready') throw Error(SernError.ReservedEvent);
fromEvent(client, event, cb).subscribe();
});
}

View File

@@ -1,5 +1,5 @@
import type { Awaitable, ChatInputCommandInteraction, Interaction } from "discord.js";
import type { Args } from "../../..";
import type { Args, Module } from "../../..";
import type Context from "../context";
export interface BaseModule {
@@ -9,5 +9,7 @@ export interface BaseModule {
}
export function sernModule<T extends Module> (module : T, plugins : Plugin[]) {
}