feat(handler) basic event plugin support

This commit is contained in:
Jacob Nguyen
2022-04-23 11:34:01 -05:00
parent 901cb51e01
commit 02956a004c
5 changed files with 35 additions and 26 deletions

4
package-lock.json generated
View File

@@ -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": {

View File

@@ -1,5 +1,5 @@
{
"name": "sern-handler",
"name": "@sern/handler",
"version": "0.1.0",
"description": "",
"main": "dist/index.js",

View File

@@ -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");
}
})
};

View File

@@ -15,11 +15,11 @@ export function match<T extends keyof ModuleDefs>(
export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType : T) {
return (src : Observable<PluggedModule|undefined>) =>
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 : <EventPlugin[]>plug.plugins });
subscriber.next({ mod : plug.mod, plugins : plug.plugins });
} else {
if (plug === undefined) {
return throwError(() => SernError.UndefinedModule);

View File

@@ -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 <T extends ModuleType> (
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;
}