From de7ddc390f560ce762d865442560f5e24ec02798 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 10 Apr 2022 21:54:52 -0500 Subject: [PATCH] feat : more plugin work ( currently not finsihed), removed init function --- src/handler/events/interactionCreate.ts | 2 +- src/handler/events/observableHandling.ts | 10 ++++ src/handler/events/readyEvent.ts | 72 +++++++++++++++++------- src/handler/plugins/plugin.ts | 9 ++- src/handler/structures/wrapper.ts | 1 - 5 files changed, 67 insertions(+), 27 deletions(-) diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 4aee381..cae3387 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -57,7 +57,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { else return of(); }) ).subscribe({ - error(e) { + error(e){ throw e; }, next(command) { diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index f93cf5d..ca650b8 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -58,3 +58,13 @@ export function ignoreNonBot(prefix : string) { }); }); } +export function partition + (array: T[], isValid: (el : T) => el is U): [U[], V[]] { + return array.reduce(([pass, fail], elem) => { + return isValid(elem) + ? [[...pass, elem], fail] + : [pass, [...fail, elem]]; + }, [[], []] ); +} + + diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 3cb7307..c94231c 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -1,20 +1,29 @@ -import { first, from, fromEvent } from 'rxjs'; +import { concatMap, first, from, fromEvent, map, tap } from 'rxjs'; import { basename } from 'path'; import * as Files from '../utilities/readFile'; 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'; +import type { CommandPlugin, SernPlugin } from '../plugins/plugin'; +import { partition } from './observableHandling'; +import type { Client } from 'discord.js'; +import { Err, Ok } from 'ts-results'; export const onReady = ( wrapper : Wrapper ) => { - const { client, init, commands } = wrapper; + const { client, commands } = wrapper; fromEvent(client, 'ready') - .pipe(first()) + .pipe( + take(1), + concatMap ( _ => { + + + + }) + ) .subscribe(() => { - init?.( wrapper ); Files.buildData( commands ) - .then( createCommandCache ); + .then( deployCommands(client) ); }) }; @@ -49,22 +58,45 @@ const handler = ( name : string ) => const registerModules = (name : string, mod : ModuleStates[T]) => (> handler(name)[mod.type])(mod); - -function setCommands ( { plugged, absPath } : { plugged: PluggedModule, absPath : string } ) { - const name = plugged.mod.name ?? Files.fmtFileName(basename(absPath)); - // making all modules have name property - if (plugged.mod.name === undefined ) { - registerModules(name, { name, ...plugged.mod }); - } else { - registerModules(name, plugged.mod); - } +function setCommands ( plugged : PluggedModule ) { + registerModules(plugged.mod.name!, plugged.mod); } -function createCommandCache( - arr: {plugged: PluggedModule, absPath: string}[] - ) { - // possible mem leak? - from(arr).subscribe ( setCommands ); +function deployCommands (wrapper : Client) { + + return function (arr : { plugged : PluggedModule, absPath : string}[]) { + from(arr) + .pipe( + map (({plugged, absPath}) => { + const name = plugged.mod.name ?? Files.fmtFileName(basename(absPath)); + if (plugged.mod.name === undefined ) { + return { mod: { name, ...plugged.mod }, plugins : plugged.plugins }; + } + return plugged; + }), + concatMap( ({ plugins, mod} ) => { + const [ cmdPlugins, eventPlugins ] = partition(plugins, isCmdPlugin); + + return from(cmdPlugins) + .pipe( + + + ) + }), + tap (plug => deployPlugins(plug, wrapper)), + tap ( setCommands ), + ).subscribe ( ); + } } +function isCmdPlugin ( p : SernPlugin) : p is CommandPlugin { + return (p.type & 0) !== 0; +} +// 0b0 +// 0b0 +function deployPlugins(plugged: PluggedModule, client : Client) { + const { plugins, mod } = plugged; + const [ cmdPlugins, eventPlugins ] = partition(plugins, isCmdPlugin) + +} diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts index 7d888e5..bc94b55 100644 --- a/src/handler/plugins/plugin.ts +++ b/src/handler/plugins/plugin.ts @@ -12,9 +12,10 @@ // // +import type { Client } from "discord.js"; import type { Err, Ok, Result } from "ts-results"; import type { Override, Wrapper } from "../.."; -import { apply, BaseModule, sernModule } from "../structures/modules/module"; +import type { BaseModule } from "../structures/modules/module"; export enum PluginType { Command = 0b00, @@ -36,7 +37,7 @@ interface BasePlugin extends Override{ export type CommandPlugin = { type : PluginType.Command } & Override Result + execute : (wrapper:Client, controller:Controller) => Result }>; export type EventPlugin = { @@ -48,9 +49,7 @@ export type SernPlugin = | EventPlugin; -sernModule( - -) + diff --git a/src/handler/structures/wrapper.ts b/src/handler/structures/wrapper.ts index 54168d1..198ebb5 100644 --- a/src/handler/structures/wrapper.ts +++ b/src/handler/structures/wrapper.ts @@ -14,7 +14,6 @@ interface Wrapper { readonly client: Client; readonly defaultPrefix: string; readonly commands: string; - init?: (handler: Wrapper) => void; readonly events? : DiscordEvent[]; }