diff --git a/src/handler/sern.ts b/src/handler/sern.ts index beb01b6..16c0a8d 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -3,7 +3,7 @@ import { onReady } from './events/readyEvent'; import { onMessageCreate } from './events/messageEvent'; import { onInteractionCreate } from './events/interactionCreate'; import { Err, Ok } from 'ts-results'; -import { buildData } from './utilities/readFile'; +import { buildData, ExternalEventEmitters } from './utilities/readFile'; import type { EventModule } from './structures/module'; import { from, fromEvent, map, throwError } from 'rxjs'; import { match } from 'ts-pattern'; @@ -13,6 +13,7 @@ import { SernError } from './structures/errors'; import type { SpreadParams } from '../types/handler'; import * as Files from './utilities/readFile'; import { basename } from 'path'; +import type { EventEmitter } from 'events'; export function init(wrapper: Wrapper) { const { events } = wrapper; @@ -24,6 +25,13 @@ export function init(wrapper: Wrapper) { onInteractionCreate(wrapper); } +export function addExternal(emitter: T) { + if (ExternalEventEmitters.has(emitter.constructor.name)) { + throw Error(`${emitter.constructor.name} already exists!`); + } + ExternalEventEmitters.set(emitter.constructor.name, emitter); +} + function processEvents(wrapper: Wrapper, events: string | EventModule[] | (() => EventModule[])) { const eventStream = eventObservable$(wrapper, events); const processPlugins$ = eventStream.pipe(map(mod => mod)); //for now, until i figure out what to do with how plugins are registered @@ -56,7 +64,16 @@ function processEvents(wrapper: Wrapper, events: string | EventModule[] | (() => m.execute as SpreadParams, ), ) - .when(isExternalEvent, m => fromEvent(m.emitter, m.name!, m.execute)) + .when(isExternalEvent, m => { + if (!ExternalEventEmitters.has(m.emitter)) { + throw Error( + SernError.UndefinedSernEmitter + + `Could not locate + a dependency ${m.emitter} to call this event listener`, + ); + } + return fromEvent(ExternalEventEmitters.get(m.emitter)!, m.name!, m.execute); + }) .run(); }), ); diff --git a/src/handler/structures/errors.ts b/src/handler/structures/errors.ts index ff3ac37..86a90b0 100644 --- a/src/handler/structures/errors.ts +++ b/src/handler/structures/errors.ts @@ -5,5 +5,5 @@ export enum SernError { NotSupportedInteraction = `This interaction is not supported.`, PluginFailure = `A plugin failed to call controller.next()`, MismatchEvent = `You cannot use message when an interaction fired or vice versa`, - UndefinedSernEmitter = `Could not find a Sern emitter`, + UndefinedSernEmitter = `Could not find an Emitter`, } diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 0de41a1..7bb63c8 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -6,6 +6,7 @@ import type { Module } from '../structures/module'; import { SernError } from '../structures/errors'; import type { Result } from 'ts-results'; import { Err, Ok } from 'ts-results'; +import type { EventEmitter } from 'events'; //Maybe move this? this probably doesnt belong in utlities/ export const BothCommands = new Map(); @@ -25,6 +26,11 @@ export const TextCommands = { aliases: new Map(), }; export const ModalSubmitCommands = new Map(); +/** + * keeps all external emitters stored here + */ +export const ExternalEventEmitters = new Map(); + // Courtesy @Townsy45 function readPath(dir: string, arrayOfFiles: string[] = []): string[] { try {