Skip to content

Commit

Permalink
parsingParams kinda
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed May 21, 2024
1 parent 88598b0 commit 7f4004e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
23 changes: 20 additions & 3 deletions src/core/id.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
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.
* @returns An array of unique string IDs based on the type and properties of the interaction object.
*/
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];
}
}
}
/**
Expand Down
11 changes: 6 additions & 5 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 7f4004e

Please sign in to comment.