parsingParams kinda

This commit is contained in:
Jacob Nguyen
2024-05-21 01:18:20 -05:00
parent 88598b0948
commit 7f4004e043
2 changed files with 26 additions and 8 deletions

View File

@@ -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<T extends Interaction>(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];
}
}
}
/**

View File

@@ -119,9 +119,9 @@ export function createInteractionHandler<T extends Interaction>(
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<Output>(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 = { ..._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;