diff --git a/src/core/id.ts b/src/core/id.ts index 173d25cd..41cec965 100644 --- a/src/core/id.ts +++ b/src/core/id.ts @@ -1,6 +1,15 @@ import { ApplicationCommandType, ComponentType, Interaction, InteractionType } from 'discord.js'; import { CommandType, EventType } from './structures/enums'; +const parseParams = (event: { customId: string }, id: string) => { + const hasSlash = event.customId.indexOf('/') + if(hasSlash === -1) { + return { id }; + } + const baseid = event.customId.substring(0, hasSlash); + const params = event.customId.substring(hasSlash+1); + return { id: baseid, params } +} /** * Construct unique ID for a given interaction object. * @param event The interaction object for which to create an ID. @@ -8,12 +17,20 @@ import { CommandType, EventType } from './structures/enums'; */ export function reconstruct(event: T) { switch (event.type) { - case InteractionType.MessageComponent: return [`${event.customId}_C${event.componentType}`]; + case InteractionType.MessageComponent: { + let id = `${event.customId}_C${event.componentType}`; + const data = parseParams(event, id) + return [data]; + } case InteractionType.ApplicationCommand: case InteractionType.ApplicationCommandAutocomplete: - return [`${event.commandName}_A${event.commandType}`, `${event.commandName}_B`]; + return [{ id: `${event.commandName}_A${event.commandType}` }, { id: `${event.commandName}_B` }]; //Modal interactions are classified as components for sern - case InteractionType.ModalSubmit: return [`${event.customId}_M`]; + case InteractionType.ModalSubmit: { + let id = `${event.customId}_M`; + const data = parseParams(event, id); + return [data]; + } } } /** diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index 26cc93ac..6d51fe3e 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -119,9 +119,9 @@ export function createInteractionHandler( async event => { const possibleIds = Id.reconstruct(event); let modules = possibleIds - .map(id => mg.get(id)) + .map(({ id }) => mg.get(id)) .filter((id): id is Module => id !== undefined); - + if(modules.length == 0) { return Err.EMPTY; } @@ -198,12 +198,12 @@ export function createResultResolver(config: { } }; }; + export async function callInitPlugins(module: Module, deps: Dependencies, sEmitter?: Emitter) { let _module = module; for(const plugin of _module.plugins ?? []) { const res = await plugin.execute({ - module, - absPath: _module.meta.absPath , + module, absPath: _module.meta.absPath , updateModule: (partial: Partial) => { _module = { ..._module, ...partial }; return _module; @@ -217,9 +217,10 @@ export async function callInitPlugins(module: Module, deps: Dependencies, sEmitt } return _module } + async function callPlugins({ args, module, deps }: ExecutePayload) { let state = {}; - for(const plugin of module.onEvent) { + for(const plugin of module.onEvent??[]) { const result = await plugin.execute(...args, { state, deps, type: module.type === CommandType.Text?'text':'slash' }); if(result.isErr()) { return result;