mirror of
https://github.com/sern-handler/handler
synced 2026-06-14 20:02:16 +00:00
feat(messageEvent) : more refactoring to enable plugin usage
This commit is contained in:
@@ -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;
|
||||
(<Observable<Message>> fromEvent( client, 'messageCreate'))
|
||||
.pipe(
|
||||
const messageEvent$ = (<Observable<Message>> 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 : <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((<EventPlugin>ePlug).execute([ctx, args], {
|
||||
next : () => Ok.EMPTY,
|
||||
stop : () => Err.EMPTY
|
||||
}))
|
||||
}
|
||||
});
|
||||
return of( { ctx, args, mod: textCommand.mod, pluginRes })
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
@@ -16,11 +16,11 @@ export function match<T extends keyof ModuleDefs>(
|
||||
|
||||
export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType : T) {
|
||||
return (src : Observable<PluggedModule|undefined>) =>
|
||||
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 : <EventPlugin[]>plug.plugins });
|
||||
} else {
|
||||
if (plug === undefined) {
|
||||
return throwError(() => SernError.UndefinedModule);
|
||||
|
||||
Reference in New Issue
Block a user