feat: interactionCreate.ts refactoring

This commit is contained in:
jacoobes
2022-05-13 23:33:53 -05:00
parent 3dedba3493
commit c4e8e517b3
5 changed files with 43 additions and 68 deletions

View File

@@ -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<CommandType.Both>([ctx, <Args>['slash', i.options]]
), controller);
}) as Awaited<Result<void, void>>[];
//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<CommandType.MenuMsg | CommandType.MenuUser>(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);
},
});
**/
};

View File

@@ -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<CommandType.Both>([ctx, args]), controller);
}),
);
return from(res).pipe(map(res => ({ plugged, ctx, args, res })));

View File

@@ -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<BaseModule, { execute: (ctx: ContextMenuCommandInteraction) => Awaitable<void> }>;
} & Override<BaseModule, { execute: (ctx: UserContextMenuCommandInteraction) => Awaitable<void> }>;
export type ContextMenuMsg = {
type: CommandType.MenuMsg;
} & Override<BaseModule, { execute: (ctx: MessageContextMenuCommandInteraction) => Awaitable<void> }>;

View File

@@ -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<K extends ModuleType> = (mod: ModuleStates[K], plugins: SernPlugin<K>[]) => unknown;
export type HandlerCallback<K extends ModuleType> = (mod: ModuleStates[K], plugins: SernPlugin[]) => unknown;
//An object that acts as the mapped object to handler
export type ModuleHandlers = { [K in ModuleType]: HandlerCallback<K> };

View File

@@ -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<T extends CommandType> = {
[K in T] : Parameters<ModuleDefs[K]['execute']>
}[T];
type ParamMap<T extends CommandType> = {
[K in T] : Parameters<ModuleDefs[K]['execute']>
}[T]
/**
* Identity function x => x to narrow type of parameters
* @param params
*/
export function resolveParameters<T extends CommandType>
( params: ParamMap<T> ) : UnionToTupleUnion<T>
{
return params;
}