diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index ddf4321..25ab48b 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -2,6 +2,7 @@ import type { CommandInteraction, Interaction, MessageComponentInteraction, + ModalSubmitInteraction, SelectMenuInteraction, } from 'discord.js'; import { concatMap, fromEvent, map, Observable, of, throwError } from 'rxjs'; @@ -125,6 +126,23 @@ function messageComponentInteractionHandler( .otherwise(() => throwError(() => SernError.NotSupportedInteraction)); } +function modalHandler(modul : Module|undefined, ctx: ModalSubmitInteraction) { + return of(modul).pipe( + filterCorrectModule(CommandType.Modal), + concatMap(mod => { + return of(mod.onEvent?.map(e => e.execute([ctx], controller)) ?? []).pipe( + map(res => ({ + mod, + res, + execute() { + return mod.execute(ctx); + }, + })), + ); + }) + ) +} + export function onInteractionCreate(wrapper: Wrapper) { const { client } = wrapper; @@ -146,7 +164,15 @@ export function onInteractionCreate(wrapper: Wrapper) { interaction.customId, ); return messageComponentInteractionHandler(modul, interaction); - } else return throwError(() => SernError.NotSupportedInteraction); + } + if (interaction.isModalSubmit()) { + const modul = Files.ModalSubmitCommands.get(interaction.customId); + return modalHandler(modul, interaction); + } + if (interaction.isAutocomplete()) { + const modul = Files.ApplicationCommands[1].get(interaction.commandName); + } + return of(); }), ) .subscribe({ diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index dbf58a3..e04b3f7 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -110,5 +110,9 @@ function registerModule(mod: DefinitelyDefined): Resul Files.MessageCompCommands[ComponentType.SelectMenu].set(name, mod); return Ok.EMPTY; }) + .with({ type : CommandType.Modal }, mod => { + Files.ModalSubmitCommands.set(name, mod); + return Ok.EMPTY; + }) .otherwise(() => Err.EMPTY); } diff --git a/src/handler/structures/enums.ts b/src/handler/structures/enums.ts index 70dd882..d81d7cb 100644 --- a/src/handler/structures/enums.ts +++ b/src/handler/structures/enums.ts @@ -8,6 +8,7 @@ enum CommandType { MenuMsg = 0b0001000, Button = 0b0010000, MenuSelect = 0b0100000, + Modal = 0b1000000, Both = 0b0000011, } diff --git a/src/handler/structures/module.ts b/src/handler/structures/module.ts index b8a5c49..2a82695 100644 --- a/src/handler/structures/module.ts +++ b/src/handler/structures/module.ts @@ -3,6 +3,7 @@ import type { Awaitable, ButtonInteraction, MessageContextMenuCommandInteraction, + ModalSubmitInteraction, SelectMenuInteraction, UserContextMenuCommandInteraction, } from 'discord.js'; @@ -90,6 +91,17 @@ export type SelectMenuCommand = Override< } >; +export type ModalSubmitCommand = Override< + BaseModule, + { + type : CommandType.Modal; + onEvent?: EventPlugin[]; + plugins?: CommandPlugin[]; + execute : (ctx: ModalSubmitInteraction) => Awaitable; + } + +>; + export type Module = | TextCommand | SlashCommand @@ -97,7 +109,8 @@ export type Module = | ContextMenuUser | ContextMenuMsg | ButtonCommand - | SelectMenuCommand; + | SelectMenuCommand + | ModalSubmitCommand; //https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union // Explicit Module Definitions for mapping @@ -109,4 +122,5 @@ export type ModuleDefs = { [CommandType.MenuUser]: ContextMenuUser; [CommandType.Button]: ButtonCommand; [CommandType.MenuSelect]: SelectMenuCommand; + [CommandType.Modal] : ModalSubmitCommand; }; diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 60b92ea..c41e584 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -4,6 +4,7 @@ import { join } from 'path'; import { from, Observable } from 'rxjs'; import type { Module } from '../structures/module'; +//Maybe move this? this probably doesnt belong in utlities/ export const BothCommands = new Map(); export const ApplicationCommands = { [ApplicationCommandType.User]: new Map(), @@ -20,7 +21,7 @@ export const TextCommands = { text: new Map(), aliases: new Map(), }; - +export const ModalSubmitCommands = new Map(); // Courtesy @Townsy45 function readPath(dir: string, arrayOfFiles: string[] = []): string[] { try {