From 2d715ca7c739b703129e1c8c9ca5f648931d214f Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Tue, 29 Mar 2022 12:37:31 -0500 Subject: [PATCH] feat(interactionHandling) make code more dry with op fn filterTap --- src/handler/events/interactionCreate.ts | 24 ++++++++---------- src/handler/events/interactionHandling.ts | 31 +++++++++++++++++++++-- src/handler/structures/errors.ts | 1 + 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index a11769b..d9f4b2d 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -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(); } diff --git a/src/handler/events/interactionHandling.ts b/src/handler/events/interactionHandling.ts index 53cd033..9b4b711 100644 --- a/src/handler/events/interactionHandling.ts +++ b/src/handler/events/interactionHandling.ts @@ -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( + cmdType : T, + tap: (mod : ModuleDefs[T]) => Awaitable +) { + return (src : Observable) => + 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() + }) + + }) + } + diff --git a/src/handler/structures/errors.ts b/src/handler/structures/errors.ts index 92187e8..0a2ac36 100644 --- a/src/handler/structures/errors.ts +++ b/src/handler/structures/errors.ts @@ -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!` }