feat(interactionHandling) make code more dry with op fn filterTap

This commit is contained in:
Jacob Nguyen
2022-03-29 12:37:31 -05:00
parent 26c202294f
commit 2d715ca7c7
3 changed files with 41 additions and 15 deletions

View File

@@ -8,7 +8,7 @@ import type { ContextMenuMsg, ContextMenuUser } from '../structures/commands/mod
import Context from '../structures/context';
import type Wrapper from '../structures/wrapper';
import * as Files from '../utilities/readFile';
import { is } from './interactionHandling';
import { filterTap, is } from './interactionHandling';
export const onInteractionCreate = ( wrapper : Wrapper ) => {
@@ -20,31 +20,29 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
if (interaction.isChatInputCommand()) {
return of(Files.Commands.get(interaction.commandName))
.pipe(
filter(mod => is(mod, CommandType.SLASH)),
tap ( mod => {
filterTap(CommandType.SLASH, mod => {
const ctx = Context.wrap(interaction);
(mod as SlashCommand)!.execute(ctx, ['slash', interaction.options]);
}),
mod.execute(ctx, ['slash', interaction.options]);
})
);
}
if (interaction.isContextMenuCommand()) {
return of(Files.ContextMenuUser.get(interaction.commandName))
.pipe(
filter( mod => is(mod, CommandType.MENU_USER)),
tap ( mod => {
filterTap(CommandType.MENU_USER, mod => {
const ctx = Context.wrap(interaction);
(mod as ContextMenuUser)!.execute(ctx);
})
mod.execute(ctx);
}),
)
}
if (interaction.isMessageContextMenuCommand()) {
return of(Files.ContextMenuMsg.get(interaction.commandName))
.pipe(
filter( mod => is(mod, CommandType.MENU_MSG)),
tap ( mod => {
filterTap(CommandType.MENU_MSG, mod => {
const ctx = Context.wrap(interaction);
(mod as ContextMenuMsg)!.execute(ctx);
})
mod.execute(ctx);
}),
)
}
else { return of(); }

View File

@@ -1,7 +1,34 @@
import type { Awaitable } from "discord.js";
import type { CommandType } from "../sern";
import type { Module } from "../structures/structxports";
import { ignoreElements, Observable, throwError } from 'rxjs';
import type { ModuleDefs } from "../structures/commands/moduleHandler";
import { SernError } from "../structures/errors";
export function is(mod: Module | undefined, type : CommandType) : boolean {
export function match(mod: Module | undefined, type : CommandType) : boolean {
return mod !== undefined && (mod.type & type) != 0;
}
export function filterTap<T extends keyof ModuleDefs>(
cmdType : T,
tap: (mod : ModuleDefs[T]) => Awaitable<void>
) {
return (src : Observable<Module|undefined>) =>
new Observable( subscriber => {
return src.subscribe({
next(modul) {
if(match(modul, cmdType)) {
tap(modul as ModuleDefs[T]);
} else {
if (modul === undefined) {
return throwError(() => SernError.UNDEFINED_MODULE);
}
return throwError(() => SernError.MISMATCH_MODULE_TYPE);
}
},
error: (e) => subscriber.error(e),
complete: () => subscriber.complete()
})
})
}

View File

@@ -3,4 +3,5 @@ export enum SernError {
NO_ALIAS = 'You cannot provide an array with elements to a slash command.',
NOT_VALID_MOD_TYPE = 'Detected an unknown module type',
UNDEFINED_MODULE = `A module could not be detected at`
MISMATCH_MODULE_TYPE = `A module type mismatched with event emitted!`
}