diff --git a/src/handler/events/dispatchers.ts b/src/handler/events/dispatchers.ts index 1eeb856..c0baf3f 100644 --- a/src/handler/events/dispatchers.ts +++ b/src/handler/events/dispatchers.ts @@ -1,5 +1,5 @@ import type { Processed } from '../../types/core'; -import type { BothCommand, CommandModule, Module } from '../../types/module'; +import type { BothCommand, CommandModule, EventModule, Module } from '../../types/module'; import { EventEmitter } from 'node:events'; import * as assert from 'node:assert'; import { concatMap, from, fromEvent, map, OperatorFunction, pipe } from 'rxjs'; @@ -9,7 +9,7 @@ import { AutocompleteInteraction, BaseInteraction, Message } from 'discord.js'; import { treeSearch } from '../../core/functions'; import { SernError } from '../../core/structures/errors'; import { Args } from '../../types/handler'; -import { CommandType, Context } from '../../core'; +import { CommandType, Context, EventType } from '../../core'; import { isAutocomplete } from '../../core/predicates'; export function dispatchInteraction< @@ -24,7 +24,7 @@ export function dispatchInteraction< args: createArgs(payload.event), }; } - +//TODO: refactor dispatchers so that it implements a strategy for each different type of payload? export function dispatchMessage(module: Processed, args: [Context, Args]) { return { module, @@ -46,6 +46,8 @@ export function dispatchAutocomplete(payload: { module: Processed, } + + export function contextArgs( wrappable: Message | BaseInteraction, messageArgs?: string[], @@ -55,6 +57,7 @@ export function contextArgs( return [ctx, args] as [Context, Args]; } + export function interactionArg(interaction: T) { return [interaction] as [T]; } diff --git a/src/handler/events/generic.ts b/src/handler/events/generic.ts index 7630474..591d642 100644 --- a/src/handler/events/generic.ts +++ b/src/handler/events/generic.ts @@ -1,18 +1,16 @@ import { - BaseInteraction, Interaction, InteractionType, Message, } from 'discord.js'; import { EMPTY, Observable, concatMap, filter, from, map, of, throwError, tap } from 'rxjs'; -import { CommandType, ModuleManager } from '../../core'; +import { ModuleManager } from '../../core'; import { SernError } from '../../core/structures/errors'; import { callPlugin, everyPluginOk, filterMap, filterMapTo } from '../../core/operators'; import { defaultModuleLoader } from '../../core/module-loading'; import { ImportPayload, Processed } from '../../types/core'; import { CommandModule, Module } from '../../types/module'; -import { contextArgs, createDispatcher, dispatchAutocomplete, dispatchInteraction, dispatchMessage, interactionArg } from './dispatchers'; -import { isAutocomplete } from '../../core/predicates'; +import { contextArgs, createDispatcher, dispatchMessage } from './dispatchers'; import { ObservableInput, pipe, switchMap } from 'rxjs'; import { SernEmitter } from '../../core'; import { errTap } from '../../core/operators'; diff --git a/src/handler/events/interactions.ts b/src/handler/events/interactions.ts index 3f1e803..356f44d 100644 --- a/src/handler/events/interactions.ts +++ b/src/handler/events/interactions.ts @@ -10,6 +10,7 @@ import { DependencyList } from '../../types/core'; export function makeInteractionHandler([emitter, _, _1, modules, client]: DependencyList ) { const interactionStream$ = sharedObservable(client, 'interactionCreate'); const handle = createInteractionHandler(interactionStream$, modules); + const interactionHandler$ = merge( handle(isMessageComponent), handle(isAutocomplete), diff --git a/src/handler/events/messages.ts b/src/handler/events/messages.ts index b7b6ea2..58eabec 100644 --- a/src/handler/events/messages.ts +++ b/src/handler/events/messages.ts @@ -31,12 +31,12 @@ export function makeMessageHandler( const messageStream$ = sharedObservable(client, 'messageCreate'); const handler = createMessageHandler(messageStream$, defaultPrefix, modules); - const messageHandler = handler(isNonBot(defaultPrefix) as (m: Message) => m is Message); - return messageHandler.pipe( + const prefixedMessages$ = handler(isNonBot(defaultPrefix) as (m: Message) => m is Message); + + return prefixedMessages$.pipe( makeModuleExecutor(module => { emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure)); }), concatMap(payload => executeModule(emitter, payload)), - ); } diff --git a/src/handler/events/ready.ts b/src/handler/events/ready.ts index e3c754b..eaa9245 100644 --- a/src/handler/events/ready.ts +++ b/src/handler/events/ready.ts @@ -12,12 +12,12 @@ import { buildModules, callInitPlugins } from './generic'; export function startReadyEvent( [sEmitter, errorHandler, , moduleManager, client]: DependencyList, - input: ObservableInput, + allPaths: ObservableInput, ) { const ready$ = fromEvent(client!, 'ready').pipe(take(1)); return ready$ .pipe( - buildModules(input, sEmitter), + buildModules(allPaths, sEmitter), callInitPlugins({ onStop: module => { sEmitter.emit( diff --git a/src/handler/events/user-defined.ts b/src/handler/events/user-defined.ts index f01bf20..326bd4a 100644 --- a/src/handler/events/user-defined.ts +++ b/src/handler/events/user-defined.ts @@ -1,4 +1,4 @@ -import { catchError, finalize, map, mergeAll, of } from 'rxjs'; +import { ObservableInput, catchError, finalize, map, mergeAll, of } from 'rxjs'; import type { Dependencies, Processed, Wrapper } from '../../types/core'; import type { CommandModule, EventModule } from '../../types/module'; import type { EventEmitter } from 'node:events'; @@ -11,9 +11,11 @@ import { handleError } from '../../core/contracts/error-handling'; import { useContainerRaw } from '../../core/dependencies'; import { buildModules, callInitPlugins } from './generic'; + + export function makeEventsHandler( [s, err, log, client]: [SernEmitter, ErrorHandling, Logging | undefined, EventEmitter], - eventsPath: string, + allPaths: ObservableInput, containerGetter: Wrapper['containerConfig'], ) { const lazy = (k: string) => containerGetter.get(k as keyof Dependencies)[0]; @@ -33,7 +35,7 @@ export function makeEventsHandler( }; of(null) .pipe( - buildModules(eventsPath, s), + buildModules(allPaths, s), callInitPlugins({ onStop: module => s.emit('module.register', SernEmitter.failure(module, SernError.PluginFailure)),