diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 9505238..2f7a4e0 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -131,16 +131,14 @@ export function onInteractionCreate (wrapper: Wrapper) { ePlugArr.push(res as Awaited>); } if(ePlugArr.every(e => e.ok)) { - wrapper.sernEmitter?.emit('sern.command.success', [mod!]); await execute(); + wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! }); } else { - wrapper.sernEmitter?.emit('sern.command.fail', [mod!]); - console.log(ePlugArr); - console.log(mod, 'failed'); + wrapper.sernEmitter?.emit('module.activate', { success: false, module: mod!, reason : SernError.PluginFailure }); } }, error(err) { - wrapper.sernEmitter?.emit('sern.error', err); + wrapper.sernEmitter?.emit('error', err); } }); } \ No newline at end of file diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index be4fe77..278e345 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -9,6 +9,7 @@ import { fmt } from '../utilities/messageHelpers'; import * as Files from '../utilities/readFile'; import { filterCorrectModule, ignoreNonBot } from './observableHandling'; import { CommandType } from '../structures/enums'; +import { SernError } from '../structures/errors'; export const onMessageCreate = (wrapper: Wrapper) => { const { client, defaultPrefix } = wrapper; @@ -50,11 +51,18 @@ export const onMessageCreate = (wrapper: Wrapper) => { }), ); - processEventPlugins$.subscribe(({ mod, ctx, args, res }) => { - if (res.every(pl => pl.ok)) { - Promise.resolve(mod.execute(ctx, args)).then(() => console.log(mod)); - } else { - console.log(mod, 'failed'); - } - }); -}; + processEventPlugins$.subscribe({ + next({ mod, ctx, args, res }) { + if (res.every(pl => pl.ok)) { + Promise.resolve(mod.execute(ctx, args)).then(() => { + wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! }); + }); + } else { + wrapper.sernEmitter?.emit('module.activate', { success: false, module: mod!, reason: SernError.PluginFailure }); + } + }, + error(e) { + wrapper.sernEmitter?.emit('error', e); + } + }); + }; diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 1ef8319..76d256d 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -63,9 +63,9 @@ export const onReady = (wrapper: Wrapper) => { if(res.err) { throw Error(SernError.NonValidModuleType); } + wrapper.sernEmitter?.emit('module.register', { success : true, module : mod } ); } else { - console.log(`Failed to load command ${mod.name!}`); - console.log(mod); + wrapper.sernEmitter?.emit('module.register', { success : false, module : mod, reason : SernError.PluginFailure } ); } }); }; diff --git a/src/handler/sernEmitter.ts b/src/handler/sernEmitter.ts index 0c894f6..b482d93 100644 --- a/src/handler/sernEmitter.ts +++ b/src/handler/sernEmitter.ts @@ -1,12 +1,15 @@ import { EventEmitter } from 'events'; import type { Module } from './structures/module'; -import type { Nullish } from '../types/handler'; +import type { Err } from 'ts-results'; + +type Payload = + { success : true, module : Module } + | { success : false, module: Module | undefined, reason : string | Error } type SernEventsMapping = { - ['sern.command.registered'] : [ Module ]; - ['sern.command.success'] : [ Module ]; - ['sern.command.fail'] : [ Nullish ]; - ['sern.error'] : [ Error ]; + ['module.register'] : [ Payload ]; + ['module.activate'] : [ Payload ]; + ['error'] : [ Error | string ]; } export default class SernEmitter extends EventEmitter { @@ -17,7 +20,7 @@ export default class SernEmitter extends EventEmitter { public override once(eventName: T, listener: (...args: SernEventsMapping[T][]) => void): this { return super.once(eventName,listener); } - public override emit(eventName: T, args : SernEventsMapping[T]): boolean { + public override emit(eventName: T, ...args : SernEventsMapping[T]): boolean { return super.emit(eventName, ...args); } } diff --git a/src/handler/structures/errors.ts b/src/handler/structures/errors.ts index 171a3a1..82d2f9c 100644 --- a/src/handler/structures/errors.ts +++ b/src/handler/structures/errors.ts @@ -7,4 +7,5 @@ export enum SernError { NotImplemented = 'This feature has not yet been implemented', NotSupportedInteraction = `This interaction is not supported.`, NotValidEventName = `Supplied a non valid event name`, + PluginFailure = `A plugin failed to call controller.next()`, }