diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index d236129..c6ddfc4 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -1,5 +1,5 @@ import type { Message } from 'discord.js'; -import { fromEvent, Observable, of, concatMap, mergeMap, map, from, every, concatAll, concat, tap } from 'rxjs'; +import { fromEvent, Observable, of, concatMap, mergeMap, map, from, every, concatAll, concat, tap, switchMap } from 'rxjs'; import { Err, Ok } from 'ts-results'; import type { Args } from '../..'; import { CommandType } from '../sern'; @@ -27,14 +27,13 @@ export const onMessageCreate = (wrapper : Wrapper) => { const ensureModuleType$ = processMessage$.pipe( concatMap(payload => of(payload.mod) .pipe( - tap(console.log), filterCorrectModule(CommandType.Text), map( textCommand => ({ ...payload, mod : textCommand })) ))); const processPlugins$ = ensureModuleType$.pipe( - mergeMap( ({ctx, args, mod}) => { + switchMap( ({ctx, args, mod}) => { const res = from(mod.plugins.map(ePlug => { - return from((ePlug).execute([ctx, args], { + return (ePlug.execute([ctx, args], { next : () => Ok.EMPTY, stop : () => Err.EMPTY })) diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index f37308b..a31498d 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -1,5 +1,4 @@ import type { Awaitable, Message } from 'discord.js'; -import type { CommandType } from '../sern'; import { Observable, throwError } from 'rxjs'; import type { ModuleDefs } from '../structures/modules/commands/moduleHandler'; import { SernError } from '../structures/errors'; @@ -19,7 +18,6 @@ export function filterCorrectModule(cmdType : T) { new Observable<{ mod : ModuleDefs[T], plugins : EventPlugin[] }>( subscriber => { return src.subscribe({ next(plug) { - console.log(plug) if(match(plug, cmdType)) { subscriber.next({ mod : plug.mod, plugins : plug.plugins }); } else { @@ -84,13 +82,16 @@ 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]]; - }, [[], []] ); +export function partition( + condition : (el : T) => el is U, + array : T[] +) : [ U[], T[] ] { + const uArr : U[] = []; + const vArr : T[] = []; + for (const el of array ) { + (condition(el) ? uArr : vArr).push(el) + } + return [ uArr, vArr ] } diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index b2f1e2a..19ccf33 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -1,16 +1,17 @@ -import { from, fromEvent, map, take,concat, concatAll, mergeMap, skip, Observable, tap, of, concatMap} from 'rxjs'; +import {concatMap, from, fromEvent, map, take,concat, mergeMap, skip, Observable, of, } from 'rxjs'; import { basename } from 'path'; import * as Files from '../utilities/readFile'; import type Wrapper from '../structures/wrapper'; import type { HandlerCallback, ModuleHandlers, ModuleStates, ModuleType } from '../structures/modules/commands/moduleHandler'; import { CommandType } from '../sern'; -import type { CommandPlugin, PluginType, SernPlugin } from '../plugins/plugin'; +import { CommandPlugin, EventPlugin, PluginType, SernPlugin } from '../plugins/plugin'; import { partition } from './observableHandling'; import { Err, Ok, Result } from 'ts-results'; import type { PluggedModule } from '../structures/modules/module'; import type { Awaitable } from 'discord.js'; export const onReady = ( wrapper : Wrapper ) => { + const { client, commands } = wrapper; const ready$ = fromEvent(client, 'ready').pipe(take(1),skip(1)); const processCommandFiles$ = Files.buildData(commands).pipe( @@ -23,18 +24,19 @@ export const onReady = ( wrapper : Wrapper ) => { })); const processPlugins$ = processCommandFiles$.pipe( mergeMap( ({mod, plugins:allPlugins}) => { - const [ cmdPlugins, plugins ] = partition(allPlugins, isCmdPlugin); - const cmdPluginsRes = cmdPlugins.map(plug => { return { + const [ cmdPlugins, eventPlugins ] = partition(isCmdPlugin, allPlugins); + const cmdPluginsRes = cmdPlugins.map(plug => { + return { ...plug, - name: plug.name ?? 'Unnamed Plugin', + name: plug?.name ?? 'Unnamed Plugin', execute : plug.execute(client, mod, { next : () => Ok.EMPTY, stop : () => Err.EMPTY }) } }); - return of({ plugged : { mod , plugins }, cmdPluginsRes }) - }) + return of({ plugged : { mod , plugins : eventPlugins }, cmdPluginsRes }) + }), ); @@ -46,24 +48,19 @@ export const onReady = ( wrapper : Wrapper ) => { name: string; description: string; }[]; - }>).subscribe(({plugged, cmdPluginsRes }) => { - + }>).pipe ( + concatMap( pl => + from(Promise.all(pl.cmdPluginsRes.map(async e=> ({...e, execute : await e.execute })))) + .pipe( + map(res => ({...pl, cmdPluginsRes : res })), + ) + ), +) + .subscribe(({ plugged : { mod, plugins }, cmdPluginsRes }) => { + console.log(cmdPluginsRes) + registerModule(mod.name!, mod, plugins) }) - /** - ({ res, plugged: { mod, plugins }}) => { - res.then( result => { - if(result.ok) { - console.log(`${mod.name!} has been registered`) - registerModule(mod.name!, mod, plugins) - } else { - // TODO: add event emitter for command failures - console.log('a plugin failed to load'); - console.log(`Did not register command ${mod.name!}`) - console.log(mod); - } - }); - }) - **/ + } @@ -103,8 +100,11 @@ function registerModule ( return (> handler(name)[mod.type])(mod, plugins); } -function isCmdPlugin ( p : SernPlugin) : p is CommandPlugin { - return (p.type & 0) === 0; +function isCmdPlugin (p : SernPlugin) : p is CommandPlugin { + return (p.type & PluginType.Command) !== 0; +} +function isEventPlugin( p : SernPlugin) : p is EventPlugin { + return (p.type & PluginType.Event) !== 0; } diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts index 802882c..0184544 100644 --- a/src/handler/plugins/plugin.ts +++ b/src/handler/plugins/plugin.ts @@ -19,8 +19,8 @@ import type { ModuleDefs } from "../structures/modules/commands/moduleHandler"; import type { BaseModule, PluggedModule } from "../structures/modules/module"; export enum PluginType { - Command = 0b00, - Event = 0b01 + Command = 0b01, + Event = 0b10 } export interface Controller {