From d29298c17a1d67146bdddb9cf07a16924c55ed3a Mon Sep 17 00:00:00 2001 From: jacoobes Date: Sat, 14 May 2022 02:16:13 -0500 Subject: [PATCH] feat: Add messageComponent handler --- src/handler/events/interactionCreate.ts | 29 +++++++++++++++++++++++-- src/handler/structures/errors.ts | 2 ++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index aff662d..c105530 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -18,6 +18,8 @@ import { CommandType, controller } from '../sern'; import { resolveParameters } from '../utilities/resolveParameters'; import type { Args } from '../../types/handler'; import type { MessageComponentInteraction } from 'discord.js'; +import { ButtonInteraction, ComponentType, SelectMenuInteraction } from 'discord.js'; + function applicationCommandHandler< T extends CommandType.Both | CommandType.MenuUser | CommandType.MenuMsg | CommandType.Slash, @@ -60,10 +62,33 @@ function applicationCommandHandler< } function messageComponentInteractionHandler( - modul: PluggedModule | undefined, + mod: PluggedModule | undefined, interaction: MessageComponentInteraction, ) { - return of(modul); + if (mod === undefined) { + return throwError(() => SernError.UndefinedModule); + } + const eventPlugins = mod.plugins.filter(isEventPlugin); + return match(interaction) + .with({ componentType : ComponentType.Button }, (i : ButtonInteraction) => { + const res = eventPlugins.map(e => { + return e.execute(resolveParameters([i]), controller); + }); + return of({ res, mod, i}); + }) + .with( { componentType : ComponentType.SelectMenu }, (i : SelectMenuInteraction) => { + const res = eventPlugins.map(e => { + return e.execute(resolveParameters([i]), controller); + }); + return of({ res, mod, i}); + }) + .with( { componentType : ComponentType.TextInput }, _ => { + return throwError(() => SernError.NotImplemented); + } ) + .with( { componentType : P._ }, i => { + return throwError(() => SernError.NotSupportedInteraction); + }) + .exhaustive(); } export const onInteractionCreate = (wrapper: Wrapper) => { diff --git a/src/handler/structures/errors.ts b/src/handler/structures/errors.ts index 68aa695..1eacdb1 100644 --- a/src/handler/structures/errors.ts +++ b/src/handler/structures/errors.ts @@ -4,4 +4,6 @@ export enum SernError { NonValidModuleType = 'Detected an unknown module type', UndefinedModule = `A module could not be detected at`, MismatchModule = `A module type mismatched with event emitted!`, + NotImplemented = 'This feature has not yet been implemented', + NotSupportedInteraction = `This interaction is not supported.`, }