fix(readyEvent, partition) both were broken and failed to load plugins correctly

This commit is contained in:
Jacob Nguyen
2022-04-22 11:05:37 -05:00
parent 3e0a5b3b3b
commit 901cb51e01
4 changed files with 41 additions and 41 deletions

View File

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

View File

@@ -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<T extends keyof ModuleDefs>(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 : <EventPlugin[]>plug.plugins });
} else {
@@ -84,13 +82,16 @@ export function ignoreNonBot(prefix : string) {
});
});
}
export function partition<T,U extends T,V extends T>
(array: T[], isValid: (el : T) => el is U): [U[], V[]] {
return array.reduce(([pass, fail], elem) => {
return isValid(elem)
? [[...pass, <U>elem], fail]
: [pass, [...fail, <V>elem]];
}, [<U[]>[], <V[]>[]] );
export function partition<T,U extends T>(
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 ]
}

View File

@@ -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 : <PluggedModule>{ mod , plugins }, cmdPluginsRes })
})
return of({ plugged : <PluggedModule>{ 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 <T extends ModuleType> (
return (<HandlerCallback<T>> 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;
}

View File

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