From 8bc624aceb4d258a027455ce71baca67f18742aa Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Tue, 19 Apr 2022 15:02:14 -0500 Subject: [PATCH] feat(messageEvent) : more refactoring to enable plugin usage --- src/handler/events/messageEvent.ts | 69 ++++++++++++------------ src/handler/events/observableHandling.ts | 4 +- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index 2f759fb..5bab360 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -1,50 +1,47 @@ import type { Message } from 'discord.js'; -import { fromEvent, Observable, of, concatMap, mergeMap } from 'rxjs'; +import { fromEvent, Observable, of, concatMap, mergeMap, map, from } from 'rxjs'; import { Err, Ok } from 'ts-results'; +import type { Args } from '../..'; +import type { EventPlugin } from '../plugins/plugin'; import { CommandType } from '../sern'; import Context from '../structures/context'; import type Wrapper from '../structures/wrapper'; import { fmt } from '../utilities/messageHelpers'; import * as Files from '../utilities/readFile'; -import { filterCorrectModule, filterTap, ignoreNonBot } from './observableHandling'; +import { filterCorrectModule, ignoreNonBot } from './observableHandling'; export const onMessageCreate = (wrapper : Wrapper) => { const { client, defaultPrefix } = wrapper; - (> fromEvent( client, 'messageCreate')) - .pipe( + const messageEvent$ = (> fromEvent( client, 'messageCreate')); + const processMessage$ = messageEvent$.pipe( ignoreNonBot(defaultPrefix), - concatMap (async m => { - const [ prefix, ...data ] = fmt(m, defaultPrefix); - const posMod = Files.Commands.get(prefix) ?? Files.Alias.get(prefix); - const ctx = Context.wrap(m); - return of( posMod ) + map(message => { + const [prefix, ...rest] = fmt(message, defaultPrefix); + return { + ctx : Context.wrap(message), + args : ['text', rest], + mod : Files.Commands.get(prefix) ?? Files.Alias.get(prefix) + } + })); + const ensureModuleType$ = processMessage$.pipe( + concatMap(payload => of(payload.mod) .pipe( filterCorrectModule(CommandType.Text), - filterTap(CommandType.Text, async (mod,plugins) => { - const res = await Promise.all( - plugins.map(async pl => ({ - ...pl, - execute : await pl.execute([ctx, ['text', data] ], { - next : () => Ok.EMPTY, - stop : () => Err.EMPTY - }), - })) - - ); - if (res.every(pl => pl.execute.ok)) { - mod.execute(ctx, ['text', data]); - } - }) - ); - }) - ).subscribe ({ - error(e) { - throw e; - }, - next(command) { - //log on each command emitted - console.log(command); - }, - }); - + map( textCommand => ({ ...payload, textCommand })) + ))); + const processPlugins$ = ensureModuleType$.pipe( + mergeMap( ({ctx, args, textCommand}) => { + const pluginRes = textCommand.plugins.map( ePlug => { + return { + name : ePlug.name, + description : ePlug.description, + res : from((ePlug).execute([ctx, args], { + next : () => Ok.EMPTY, + stop : () => Err.EMPTY + })) + } + }); + return of( { ctx, args, mod: textCommand.mod, pluginRes }) + }) + ); }; diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index 1e117d1..3b1a47c 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -16,11 +16,11 @@ export function match( export function filterCorrectModule(cmdType : T) { return (src : Observable) => - new Observable<{cmdType : T, plug : PluggedModule}>( subscriber => { + new Observable<{ mod : ModuleDefs[T], plugins : EventPlugin[] }>( subscriber => { return src.subscribe({ next(plug) { if(match(plug, cmdType)) { - subscriber.next({cmdType, plug}); + subscriber.next({ mod : plug.mod, plugins : plug.plugins }); } else { if (plug === undefined) { return throwError(() => SernError.UndefinedModule);