mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
parsingParams kinda
This commit is contained in:
@@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user