diff --git a/package-lock.json b/package-lock.json index 4c10a54..bd92654 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "sern-handler", + "name": "@sern/handler", "version": "0.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "sern-handler", + "name": "@sern/handler", "version": "0.1.0", "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index ba3c002..174ebf0 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "sern-handler", + "name": "@sern/handler", "version": "0.1.0", "description": "", "main": "dist/index.js", diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index c6ddfc4..0b9d073 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, switchMap } from 'rxjs'; +import { fromEvent, Observable, of, concatMap, map, from, every, concatAll, tap, switchMap } from 'rxjs'; import { Err, Ok } from 'ts-results'; import type { Args } from '../..'; import { CommandType } from '../sern'; @@ -8,6 +8,7 @@ import type Wrapper from '../structures/wrapper'; import { fmt } from '../utilities/messageHelpers'; import * as Files from '../utilities/readFile'; import { filterCorrectModule, ignoreNonBot } from './observableHandling'; +import { isEventPlugin } from './readyEvent'; export const onMessageCreate = (wrapper : Wrapper) => { const { client, defaultPrefix } = wrapper; @@ -24,31 +25,40 @@ export const onMessageCreate = (wrapper : Wrapper) => { mod : Files.Commands.get(prefix) ?? Files.Alias.get(prefix) } })); + const ensureModuleType$ = processMessage$.pipe( concatMap(payload => of(payload.mod) .pipe( filterCorrectModule(CommandType.Text), map( textCommand => ({ ...payload, mod : textCommand })) ))); - const processPlugins$ = ensureModuleType$.pipe( - switchMap( ({ctx, args, mod}) => { - const res = from(mod.plugins.map(ePlug => { - return (ePlug.execute([ctx, args], { + + const processEventPlugins$ = ensureModuleType$.pipe( + concatMap( ({ctx, args, mod:plugged}) => { + const eventPlugins = plugged.plugins.filter(isEventPlugin); + const res = Promise.all(eventPlugins.map(ePlug => { + if((ePlug.modTy & plugged.mod.type) === 0) { + return Err.EMPTY; + } + return ePlug.execute([ctx, args], { next : () => Ok.EMPTY, stop : () => Err.EMPTY - })) + }); })); - return res.pipe(concatAll(), every(res => res.ok)) - }) - ); - ensureModuleType$.pipe( - concatMap( pl => { - return processPlugins$.pipe( - map ( res => ({ res, pl })) + return from(res).pipe(map(res => ({ plugged, ctx, args, res }))) + })); + processEventPlugins$.subscribe( ( { plugged, ctx, args, res } ) => { + if(res.every( pl => pl.ok)) { + Promise.resolve(plugged.mod.execute(ctx, args)).then(() => + console.log(plugged) ) - }) - ).subscribe( ({ res, pl }) => { - console.log('test') - console.log(res, pl) + } + else { + console.log(plugged, "failed"); + } + }) + + + }; diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index a31498d..aad785c 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -15,11 +15,11 @@ export function match( export function filterCorrectModule(cmdType : T) { return (src : Observable) => - new Observable<{ mod : ModuleDefs[T], plugins : EventPlugin[] }>( subscriber => { + new Observable<{ mod : ModuleDefs[T], plugins : SernPlugin[] }>( subscriber => { return src.subscribe({ next(plug) { if(match(plug, cmdType)) { - subscriber.next({ mod : plug.mod, plugins : plug.plugins }); + subscriber.next({ mod : plug.mod, plugins : plug.plugins }); } else { if (plug === undefined) { return throwError(() => SernError.UndefinedModule); diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 19ccf33..b603dfd 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -1,4 +1,4 @@ -import {concatMap, from, fromEvent, map, take,concat, mergeMap, skip, Observable, of, } from 'rxjs'; +import {concatMap, from, fromEvent, map, take,concat, skip, Observable, of, } from 'rxjs'; import { basename } from 'path'; import * as Files from '../utilities/readFile'; import type Wrapper from '../structures/wrapper'; @@ -23,7 +23,7 @@ export const onReady = ( wrapper : Wrapper ) => { return plugged; })); const processPlugins$ = processCommandFiles$.pipe( - mergeMap( ({mod, plugins:allPlugins}) => { + concatMap( ({mod, plugins:allPlugins}) => { const [ cmdPlugins, eventPlugins ] = partition(isCmdPlugin, allPlugins); const cmdPluginsRes = cmdPlugins.map(plug => { return { @@ -57,7 +57,6 @@ export const onReady = ( wrapper : Wrapper ) => { ), ) .subscribe(({ plugged : { mod, plugins }, cmdPluginsRes }) => { - console.log(cmdPluginsRes) registerModule(mod.name!, mod, plugins) }) @@ -103,7 +102,7 @@ function registerModule ( function isCmdPlugin (p : SernPlugin) : p is CommandPlugin { return (p.type & PluginType.Command) !== 0; } -function isEventPlugin( p : SernPlugin) : p is EventPlugin { +export function isEventPlugin( p : SernPlugin) : p is EventPlugin { return (p.type & PluginType.Event) !== 0; }