From 2dd41834f40469a632f81367b3c3d6d7796ec4ac Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 10 Apr 2022 02:00:43 -0500 Subject: [PATCH] feat : can register plugin modules now --- src/handler/events/readyEvent.ts | 16 ++++++++++------ src/handler/plugins/plugin.ts | 16 ++++++++++++---- src/handler/structures/modules/module.ts | 12 ++++++------ src/handler/utilities/readFile.ts | 11 ++++++----- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 2fb2e6a..3cb7307 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -5,6 +5,7 @@ import type Wrapper from '../structures/wrapper'; import type { Module } from '../structures/structxports'; import type { HandlerCallback, ModuleHandlers, ModuleStates, ModuleType } from '../structures/modules/commands/moduleHandler'; import { CommandType } from '../sern'; +import type { PluggedModule } from '../structures/modules/module'; export const onReady = ( wrapper : Wrapper ) => { const { client, init, commands } = wrapper; @@ -48,19 +49,22 @@ const handler = ( name : string ) => const registerModules = (name : string, mod : ModuleStates[T]) => (> handler(name)[mod.type])(mod); -function setCommands ( { mod, absPath } : { mod : Module, absPath : string } ) { - const name = mod.name ?? Files.fmtFileName(basename(absPath)); + +function setCommands ( { plugged, absPath } : { plugged: PluggedModule, absPath : string } ) { + const name = plugged.mod.name ?? Files.fmtFileName(basename(absPath)); // making all modules have name property - if (mod.name === undefined ) { - registerModules(name, { name, ...mod }); + if (plugged.mod.name === undefined ) { + registerModules(name, { name, ...plugged.mod }); } else { - registerModules(name, mod); + registerModules(name, plugged.mod); } } function createCommandCache( - arr: {mod: Module, absPath: string}[] + arr: {plugged: PluggedModule, absPath: string}[] ) { // possible mem leak? from(arr).subscribe ( setCommands ); } + + diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts index 43c562c..7d888e5 100644 --- a/src/handler/plugins/plugin.ts +++ b/src/handler/plugins/plugin.ts @@ -14,7 +14,7 @@ import type { Err, Ok, Result } from "ts-results"; import type { Override, Wrapper } from "../.."; -import type { BaseModule } from "../structures/modules/module"; +import { apply, BaseModule, sernModule } from "../structures/modules/module"; export enum PluginType { Command = 0b00, @@ -27,15 +27,17 @@ interface Controller { } -type executePlugin = { execute : ( controller : Controller ) => Result } +type executeCmdPlugin = { execute : ( wrapper : Wrapper, controller : Controller ) => Result } -interface BasePlugin extends Override{ +interface BasePlugin extends Override{ type : PluginType } export type CommandPlugin = { type : PluginType.Command -} & Override Result}>; +} & Override Result +}>; export type EventPlugin = { type : PluginType.Event @@ -46,5 +48,11 @@ export type SernPlugin = | EventPlugin; +sernModule( + +) + + + diff --git a/src/handler/structures/modules/module.ts b/src/handler/structures/modules/module.ts index ed75ebc..cf05e3a 100644 --- a/src/handler/structures/modules/module.ts +++ b/src/handler/structures/modules/module.ts @@ -1,4 +1,4 @@ -import type { Awaitable, ChatInputCommandInteraction, Interaction } from "discord.js"; +import type { Awaitable, ChatInputCommandInteraction } from "discord.js"; import type { Args, Module } from "../../.."; import type { CommandPlugin, EventPlugin, SernPlugin } from "../../plugins/plugin"; import type Context from "../context"; @@ -9,8 +9,8 @@ export interface BaseModule { execute: (ctx: Context, args: Args) => Awaitable; } -export interface PluggedModule { - mod : T; +export interface PluggedModule { + mod : Module; plugins : SernPlugin[]; } @@ -26,10 +26,10 @@ export function apply(...plugins: SernPlugin[]) { return plugins; } -export function sernModule - (plugins : () => SernPlugin[], mod : T, ) : PluggedModule { +export function sernModule + (plugins : SernPlugin[], mod : Module, ) : PluggedModule { return { mod, - plugins : plugins() + plugins } } diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 9266f99..8ebebc1 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -2,6 +2,7 @@ import { readdirSync, statSync } from 'fs'; import { join } from 'path'; import type { Module } from '../structures/modules/commands/module'; import { SernError } from '../structures/errors'; +import type { PluggedModule } from '../structures/modules/module'; //We can look into lazily loading modules once everything is set export const ContextMenuUser = new Map(); @@ -32,20 +33,20 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3); /** * * @param {commandsDir} Relative path to commands directory - * @returns {Promise<{ mod: Command; absPath: string; }[]>} data from command files + * @returns {Promise<{ mod: PluggedModule; absPath: string; }[]>} data from command files */ export async function buildData(commandDir: string ): Promise< { - mod: Module; + plugged: PluggedModule; absPath: string; }[] > { return Promise.all( getCommands(commandDir).map( async (absPath) => { - const mod = (await import(absPath)).module; - if (mod === undefined) throw Error(`${SernError.UndefinedModule} ${absPath}`); - return { mod, absPath }; + const plugged = (await import(absPath)).module; + if (plugged === undefined) throw Error(`${SernError.UndefinedModule} ${absPath}`); + return { plugged , absPath }; }), ); }