feat: Optional plugins to reduce bloat

This commit is contained in:
Jacob Nguyen
2022-05-17 13:59:21 -05:00
parent 430315ad02
commit 2b81d53503
6 changed files with 45 additions and 33 deletions

View File

@@ -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); } }) ));
}),
);
})

View File

@@ -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 })));
}),

View File

@@ -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 });
}),
);

View File

@@ -58,6 +58,10 @@ export function plugins<T extends CommandType>(...plug: CommandPlugin[] | EventP
return plug;
}
export function sernModule<T extends CommandType>(mod: ModuleDefs[T]): Module {
return mod;
export function sernModule<T extends CommandType>(plugins : CommandPlugin[], onEvent: EventPlugin<T>[], mod: Omit<ModuleDefs[T], 'onEvent' | 'plugins' >) {
// return {
// plugins,
// onEvent,
// ...mod,
// };
}

View File

@@ -21,48 +21,48 @@ export interface BaseModule {
//possible refactoring types into interfaces and not types
export type TextCommand = {
type: CommandType.Text;
onEvent: EventPlugin<CommandType.Text>[];
plugins: CommandPlugin[];
onEvent?: EventPlugin<CommandType.Text>[]
plugins?: CommandPlugin[];
alias?: string[];
} & BaseModule;
export type SlashCommand = {
type: CommandType.Slash;
onEvent: EventPlugin<CommandType.Slash>[];
plugins: CommandPlugin[];
onEvent?: EventPlugin<CommandType.Slash>[]
plugins?: (CommandPlugin)[];
options: ApplicationCommandOptionData[] | [];
} & BaseModule;
export type BothCommand = {
type: CommandType.Both;
onEvent: EventPlugin<CommandType.Both>[]
plugins: CommandPlugin[]
onEvent?: EventPlugin<CommandType.Both>[]
plugins?: (CommandPlugin)[]
alias?: string[];
options: ApplicationCommandOptionData[] | [];
} & BaseModule;
export type ContextMenuUser = {
type: CommandType.MenuUser;
onEvent: EventPlugin<CommandType.MenuUser>[];
plugins: CommandPlugin[];
onEvent?: EventPlugin<CommandType.MenuUser>[];
plugins?: (CommandPlugin)[];
} & Override<BaseModule, { execute: (ctx: UserContextMenuCommandInteraction) => Awaitable<void> }>;
export type ContextMenuMsg = {
type: CommandType.MenuMsg;
onEvent: EventPlugin<CommandType.MenuMsg>[];
plugins: CommandPlugin[];
onEvent?: EventPlugin<CommandType.MenuMsg>[];
plugins?: CommandPlugin[];
} & Override<BaseModule, { execute: (ctx: MessageContextMenuCommandInteraction) => Awaitable<void> }>;
export type ButtonCommand = {
type: CommandType.Button;
onEvent: EventPlugin<CommandType.Button>[];
plugins: CommandPlugin[];
onEvent?: EventPlugin<CommandType.Button>[];
plugins?: CommandPlugin[];
} & Override<BaseModule, { execute: (ctx: ButtonInteraction) => Awaitable<void> }>;
export type SelectMenuCommand = {
type: CommandType.MenuSelect;
onEvent: EventPlugin<CommandType.MenuSelect>[];
plugins: CommandPlugin[];
onEvent?: EventPlugin<CommandType.MenuSelect>[];
plugins?: CommandPlugin[];
} & Override<BaseModule, { execute: (ctx: SelectMenuInteraction) => Awaitable<void> }>;
export type Module =

View File

@@ -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<T extends keyof ModuleDefs>(