From 8866bff576f6cc56c3e5b73a12dc0ecfac93ee90 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sat, 9 Apr 2022 13:12:49 -0500 Subject: [PATCH] feat : plugin development and ensuring all module objs have name property --- src/handler/events/readyEvent.ts | 22 +++++++---- src/handler/plugins/plugin.ts | 50 ++++++++++++++++++++++++ src/handler/sern.ts | 4 +- src/handler/structures/modules/module.ts | 4 +- 4 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 src/handler/plugins/plugin.ts diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 7ef4c29..2fb2e6a 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -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 = (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 ); } diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts new file mode 100644 index 0000000..a85ca5b --- /dev/null +++ b/src/handler/plugins/plugin.ts @@ -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, + stop : () => Err + +} + +type executePlugin = { execute : ( controller : Controller ) => Result } + +interface BasePlugin extends Override{ + type : PluginType +} + +type CommandPlugin = { + type : PluginType.Command +} & BasePlugin; + +type EventPlugin = { + type : PluginType.Event +} & BasePlugin; + +export type Plugin = + CommandPlugin + | EventPlugin; + + + + diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 91b4b2b..73e66b2 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -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(); }); } diff --git a/src/handler/structures/modules/module.ts b/src/handler/structures/modules/module.ts index 606ef62..33bd905 100644 --- a/src/handler/structures/modules/module.ts +++ b/src/handler/structures/modules/module.ts @@ -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 (module : T, plugins : Plugin[]) { +}