diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 2d1d457..9505238 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -1,5 +1,4 @@ import type { - Awaitable, CommandInteraction, Interaction, MessageComponentInteraction, @@ -28,7 +27,6 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command const mod$ = (cmdTy : T) => of(mod).pipe( filterCorrectModule(cmdTy) ); - return match(interaction) .when(isChatInputCommand, i => { const ctx = Context.wrap(i); @@ -133,14 +131,16 @@ 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(); } else { + wrapper.sernEmitter?.emit('sern.command.fail', [mod!]); console.log(ePlugArr); console.log(mod, 'failed'); } }, error(err) { - console.log(err); + wrapper.sernEmitter?.emit('sern.error', err); } }); } \ No newline at end of file diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index 7866a7d..ddc301b 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -14,7 +14,7 @@ export function filterCorrectModule(cmdType: T) { return throwError(() => SernError.UndefinedModule); } if (correctModuleType(mod, cmdType)) { - subscriber.next(mod); + subscriber.next(mod!); } else { return throwError(() => SernError.MismatchModule); } diff --git a/src/handler/sernEmitter.ts b/src/handler/sernEmitter.ts new file mode 100644 index 0000000..0c894f6 --- /dev/null +++ b/src/handler/sernEmitter.ts @@ -0,0 +1,25 @@ +import { EventEmitter } from 'events'; +import type { Module } from './structures/module'; +import type { Nullish } from '../types/handler'; + +type SernEventsMapping = { + ['sern.command.registered'] : [ Module ]; + ['sern.command.success'] : [ Module ]; + ['sern.command.fail'] : [ Nullish ]; + ['sern.error'] : [ Error ]; +} + +export default class SernEmitter extends EventEmitter { + + public override on(eventName: T, listener: (...args: SernEventsMapping[T][]) => void): this { + return super.on(eventName,listener); + } + 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 { + return super.emit(eventName, ...args); + } +} + + diff --git a/src/handler/structures/errors.ts b/src/handler/structures/errors.ts index 1eacdb1..171a3a1 100644 --- a/src/handler/structures/errors.ts +++ b/src/handler/structures/errors.ts @@ -6,4 +6,5 @@ export enum SernError { MismatchModule = `A module type mismatched with event emitted!`, NotImplemented = 'This feature has not yet been implemented', NotSupportedInteraction = `This interaction is not supported.`, + NotValidEventName = `Supplied a non valid event name`, } diff --git a/src/handler/structures/wrapper.ts b/src/handler/structures/wrapper.ts index bdbf2ed..646ba5c 100644 --- a/src/handler/structures/wrapper.ts +++ b/src/handler/structures/wrapper.ts @@ -1,5 +1,6 @@ import type { Client } from 'discord.js'; import type { DiscordEvent, EventEmitterRegister } from '../../types/handler'; +import type SernEmitter from '../sernEmitter'; /** * An object to be passed into Sern.Handler constructor. @@ -7,11 +8,11 @@ import type { DiscordEvent, EventEmitterRegister } from '../../types/handler'; * @property {readonly Client} client * @property {readonly string} defaultPrefix * @property {readonly string} commands - * @prop {(handler : Handler) => void)} init * @prop { readonly DiscordEvent[] } events */ interface Wrapper { readonly client: Client; + readonly sernEmitter? : SernEmitter readonly defaultPrefix?: string; readonly commands: string; readonly events?: (DiscordEvent | EventEmitterRegister)[]; diff --git a/src/index.ts b/src/index.ts index 1948fb5..4fd70b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ export * as Sern from './handler/sern'; export * from './types/handler'; export * from './handler/structures/structxports'; -export * from './handler/plugins/plugin'; \ No newline at end of file +export * from './handler/plugins/plugin';