diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 4b898d7..0ee4b4a 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -16,6 +16,8 @@ import type { Result } from 'ts-results'; import type { PluggedModule } from '../structures/modules/module'; import { CommandType, controller } from '../sern'; import type { EventPlugin } from '../plugins/plugin'; +import { resolveParameters } from '../utilities/resolveParameters'; +import type { Args } from '../../types/handler'; function applicationCommandHandler< T extends CommandType.Both | CommandType.MenuUser | CommandType.MenuMsg | CommandType.Slash, @@ -29,11 +31,10 @@ function applicationCommandHandler< i => i.isChatInputCommand(), (i: ChatInputCommandInteraction) => { const ctx = Context.wrap(i); - const res = eventPlugins.map((e: EventPlugin) => { - if (![CommandType.Slash, CommandType.Both].includes(e.modType)) { - return throwError(() => SernError.NonValidModuleType); - } - return e.execute([ctx, ['slash', i.options]], controller); + const res = eventPlugins.map(e => { + return e.execute( + resolveParameters([ctx, ['slash', i.options]] + ), controller); }) as Awaited>[]; //Possible unsafe cast // could result in the promises not being resolved @@ -42,12 +43,17 @@ function applicationCommandHandler< ) .when( () => P._, - (i: MessageCtxInt | UserCtxInt) => { - // const res = eventPlugins.map(e => { - // - // - // }); - return of({}); + (ctx : UserCtxInt | MessageCtxInt) => { + //Kinda hackish + const args : [UserCtxInt] | [MessageCtxInt] = ctx.isUserContextMenuCommand() + ? [ ctx as UserCtxInt ] : [ ctx as MessageCtxInt ]; + const res = eventPlugins.map(e => { + return e.execute( + resolveParameters(args + ), controller); + + }); + return of({ res, mod, ctx }); }, ) .run(); @@ -72,59 +78,5 @@ export const onInteractionCreate = (wrapper: Wrapper) => { ) .subscribe(console.log); - /** concatMap (async interaction => { - if (interaction.isChatInputCommand()) { - return of(Files.Commands.get(interaction.commandName)) - .pipe( - filterTap(CommandType.Slash, (mod) => { - const ctx = Context.wrap(interaction); - mod.execute(ctx, ['slash', interaction.options]); - }), - ); - } - if (interaction.isContextMenuCommand()) { - return of(Files.ContextMenuUser.get(interaction.commandName)) - .pipe( - filterTap(CommandType.MenuUser, (mod) => { - mod.execute(interaction); - }), - ); - } - if (interaction.isMessageContextMenuCommand()) { - return of(Files.ContextMenuMsg.get(interaction.commandName)) - .pipe( - filterTap(CommandType.MenuMsg, (mod, plugs) => { - mod.execute(interaction); - }), - ); - } - if (interaction.isButton()) { - return of(Files.Buttons.get(interaction.customId)) - .pipe( - filterTap(CommandType.Button, (mod, plugs) => { - mod.execute(interaction); - }) - ); - } - if (interaction.isSelectMenu()) { - return of(Files.SelectMenus.get(interaction.customId)) - .pipe( - filterTap(CommandType.MenuSelect, (mod, plugs) => { - mod.execute(interaction); - }) - ); - } - else return of(); - }) - ).subscribe({ - error(e){ - throw e; - }, - next(_command) { - //every command that gets triggered ends up here - //console.log(command); - }, - }); - **/ }; diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index 96ba638..58d0b85 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -9,6 +9,7 @@ import { fmt } from '../utilities/messageHelpers'; import * as Files from '../utilities/readFile'; import { filterCorrectModule, ignoreNonBot } from './observableHandling'; import { isEventPlugin } from './readyEvent'; +import { resolveParameters } from '../utilities/resolveParameters'; export const onMessageCreate = (wrapper: Wrapper) => { const { client, defaultPrefix } = wrapper; @@ -47,7 +48,7 @@ export const onMessageCreate = (wrapper: Wrapper) => { if ((ePlug.modType & plugged.mod.type) === 0) { return Err.EMPTY; } - return ePlug.execute([ctx, args], controller); + return ePlug.execute(resolveParameters([ctx, args]), controller); }), ); return from(res).pipe(map(res => ({ plugged, ctx, args, res }))); diff --git a/src/handler/structures/modules/commands/module.ts b/src/handler/structures/modules/commands/module.ts index e013a7d..2fd3df2 100644 --- a/src/handler/structures/modules/commands/module.ts +++ b/src/handler/structures/modules/commands/module.ts @@ -9,6 +9,7 @@ import type { import type { Override } from '../../../../types/handler'; import type { CommandType } from '../../../sern'; import type { BaseModule } from '../module'; +import type { UserContextMenuCommandInteraction } from 'discord.js'; //possible refactoring to interfaces and not types export type TextCommand = { @@ -29,7 +30,7 @@ export type BothCommand = { export type ContextMenuUser = { type: CommandType.MenuUser; -} & Override Awaitable }>; +} & Override Awaitable }>; export type ContextMenuMsg = { type: CommandType.MenuMsg; } & Override Awaitable }>; diff --git a/src/handler/structures/modules/commands/moduleHandler.ts b/src/handler/structures/modules/commands/moduleHandler.ts index 52a5afb..a8ecfbb 100644 --- a/src/handler/structures/modules/commands/moduleHandler.ts +++ b/src/handler/structures/modules/commands/moduleHandler.ts @@ -28,6 +28,6 @@ export type ModuleStates = { [K in ModuleType]: { type: K } & ModuleDefs[K]; }; // A handler callback that is called on each ModuleDef -export type HandlerCallback = (mod: ModuleStates[K], plugins: SernPlugin[]) => unknown; +export type HandlerCallback = (mod: ModuleStates[K], plugins: SernPlugin[]) => unknown; //An object that acts as the mapped object to handler export type ModuleHandlers = { [K in ModuleType]: HandlerCallback }; diff --git a/src/handler/utilities/resolveParameters.ts b/src/handler/utilities/resolveParameters.ts new file mode 100644 index 0000000..5d66e42 --- /dev/null +++ b/src/handler/utilities/resolveParameters.ts @@ -0,0 +1,21 @@ +import type { CommandType } from '../sern'; +import type { ModuleDefs } from '../structures/modules/commands/moduleHandler'; +import type { ParseType } from '../../types/handler'; + +type UnionToTupleUnion = { + [K in T] : Parameters +}[T]; +type ParamMap = { + [K in T] : Parameters +}[T] + + +/** + * Identity function x => x to narrow type of parameters + * @param params + */ +export function resolveParameters + ( params: ParamMap ) : UnionToTupleUnion + { + return params; +} \ No newline at end of file