From 2b81d53503209a738b70d238512c82542d3d88e8 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Tue, 17 May 2022 13:59:21 -0500 Subject: [PATCH] feat: Optional plugins to reduce bloat --- src/handler/events/interactionCreate.ts | 20 +++++++++--------- src/handler/events/messageEvent.ts | 4 ++-- src/handler/events/readyEvent.ts | 4 ++-- src/handler/plugins/plugin.ts | 8 +++++-- src/handler/structures/module.ts | 28 ++++++++++++------------- src/handler/utilities/predicates.ts | 14 ++++++++++--- 6 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 557ec7b..8e4fb46 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -29,10 +29,10 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command //SUPPORT COMMANDTYPE.BOTH return mod$(CommandType.Slash).pipe( concatMap(m => { - return of(m.onEvent.map(e => e.execute( + return of(m.onEvent?.map(e => e.execute( [ctx, ['slash', i.options]], controller - ))).pipe(map(res => ({ m, res, execute() { return m.execute(ctx, ['slash', i.options]); } }) )); + )) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx, ['slash', i.options]); } }) )); }), ); }, @@ -42,10 +42,10 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command .when(isMessageCtxMenuCmd, ctx => { return mod$(CommandType.MenuMsg).pipe( concatMap(m => { - return of(m.onEvent.map(e => e.execute( + return of(m.onEvent?.map(e => e.execute( [ctx], controller - ))).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) )); + )) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) )); }), ); }, @@ -53,10 +53,10 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command .when(isUserContextMenuCmd, ctx => { return mod$(CommandType.MenuUser).pipe( concatMap(m => { - return of(m.onEvent.map(e => e.execute( + return of(m.onEvent?.map(e => e.execute( [ctx], controller - ))).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) )); + )) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) )); }), ); }) @@ -75,20 +75,20 @@ function messageComponentInteractionHandler( .when(isButton, ctx => { return mod$(CommandType.Button).pipe( concatMap(m => { - return of(m.onEvent.map(e => e.execute( + return of(m.onEvent?.map(e => e.execute( [ctx], controller - ))).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) )); + )) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) )); }), ); }) .when(isSelectMenu, (ctx: SelectMenuInteraction) => { return mod$(CommandType.MenuSelect).pipe( concatMap(m => { - return of(m.onEvent.map(e => e.execute( + return of(m.onEvent?.map(e => e.execute( [ctx], controller - ))).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) )); + )) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) )); }), ); }) diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index 6a0b476..ce2d079 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -41,12 +41,12 @@ export const onMessageCreate = (wrapper: Wrapper) => { const processEventPlugins$ = ensureModuleType$.pipe( concatMap(({ ctx, args, mod }) => { const res = Promise.all( - mod.onEvent.map(ePlug => { + mod.onEvent?.map(ePlug => { if ((ePlug.modType & mod.type) === 0) { return Err.EMPTY; } return ePlug.execute([ctx, args], controller); - }), + }) ?? [], ); return from(res).pipe(map(res => ({ mod, ctx, args, res }))); }), diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index c1bb14c..0f0c1b3 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -27,13 +27,13 @@ export const onReady = (wrapper: Wrapper) => { ); const processPlugins$ = processCommandFiles$.pipe( concatMap((mod) => { - const cmdPluginsRes = mod.plugins.map(plug => { + const cmdPluginsRes = mod.plugins?.map(plug => { return { ...plug, name: plug?.name ?? 'Unnamed Plugin', execute: plug.execute(client, mod, controller), }; - }); + }) ?? []; return of({ mod, cmdPluginsRes }); }), ); diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts index 93ef489..52ed9a9 100644 --- a/src/handler/plugins/plugin.ts +++ b/src/handler/plugins/plugin.ts @@ -58,6 +58,10 @@ export function plugins(...plug: CommandPlugin[] | EventP return plug; } -export function sernModule(mod: ModuleDefs[T]): Module { - return mod; +export function sernModule(plugins : CommandPlugin[], onEvent: EventPlugin[], mod: Omit) { + // return { + // plugins, + // onEvent, + // ...mod, + // }; } diff --git a/src/handler/structures/module.ts b/src/handler/structures/module.ts index ac84611..fbc9dbc 100644 --- a/src/handler/structures/module.ts +++ b/src/handler/structures/module.ts @@ -21,48 +21,48 @@ export interface BaseModule { //possible refactoring types into interfaces and not types export type TextCommand = { type: CommandType.Text; - onEvent: EventPlugin[]; - plugins: CommandPlugin[]; + onEvent?: EventPlugin[] + plugins?: CommandPlugin[]; alias?: string[]; } & BaseModule; export type SlashCommand = { type: CommandType.Slash; - onEvent: EventPlugin[]; - plugins: CommandPlugin[]; + onEvent?: EventPlugin[] + plugins?: (CommandPlugin)[]; options: ApplicationCommandOptionData[] | []; } & BaseModule; export type BothCommand = { type: CommandType.Both; - onEvent: EventPlugin[] - plugins: CommandPlugin[] + onEvent?: EventPlugin[] + plugins?: (CommandPlugin)[] alias?: string[]; options: ApplicationCommandOptionData[] | []; } & BaseModule; export type ContextMenuUser = { type: CommandType.MenuUser; - onEvent: EventPlugin[]; - plugins: CommandPlugin[]; + onEvent?: EventPlugin[]; + plugins?: (CommandPlugin)[]; } & Override Awaitable }>; export type ContextMenuMsg = { type: CommandType.MenuMsg; - onEvent: EventPlugin[]; - plugins: CommandPlugin[]; + onEvent?: EventPlugin[]; + plugins?: CommandPlugin[]; } & Override Awaitable }>; export type ButtonCommand = { type: CommandType.Button; - onEvent: EventPlugin[]; - plugins: CommandPlugin[]; + onEvent?: EventPlugin[]; + plugins?: CommandPlugin[]; } & Override Awaitable }>; export type SelectMenuCommand = { type: CommandType.MenuSelect; - onEvent: EventPlugin[]; - plugins: CommandPlugin[]; + onEvent?: EventPlugin[]; + plugins?: CommandPlugin[]; } & Override Awaitable }>; export type Module = diff --git a/src/handler/utilities/predicates.ts b/src/handler/utilities/predicates.ts index d4fd3c6..359c361 100644 --- a/src/handler/utilities/predicates.ts +++ b/src/handler/utilities/predicates.ts @@ -1,7 +1,15 @@ import type { Module, ModuleDefs } from '../structures/module'; -import type { Awaitable, ChatInputCommandInteraction, CommandInteraction } from 'discord.js'; -import type { ButtonInteraction, MessageComponentInteraction, SelectMenuInteraction } from 'discord.js'; -import type { MessageContextMenuCommandInteraction, UserContextMenuCommandInteraction } from 'discord.js'; +import type { + Awaitable, + ButtonInteraction, + ChatInputCommandInteraction, + CommandInteraction, + MessageComponentInteraction, + MessageContextMenuCommandInteraction, + SelectMenuInteraction, + UserContextMenuCommandInteraction, +} from 'discord.js'; + export function correctModuleType(