From 92c39b91c4bee7f716ef0b5e1a557b9348b12cd4 Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Fri, 10 May 2024 21:02:25 -0400 Subject: [PATCH] fix: "bro y todo ese codigo repetido" --- src/client/httpclient.ts | 2 +- src/client/oninteractioncreate.ts | 14 ++++++- src/commands/applications/chat.ts | 13 +++++-- src/commands/applications/menu.ts | 62 +----------------------------- src/commands/basecontext.ts | 24 ++++++++++++ src/components/componentcommand.ts | 61 +---------------------------- src/components/handler.ts | 20 +++++++--- src/components/modalcommand.ts | 62 +----------------------------- src/components/modalcontext.ts | 4 ++ 9 files changed, 68 insertions(+), 194 deletions(-) diff --git a/src/client/httpclient.ts b/src/client/httpclient.ts index 33119c1..a5d7dd4 100644 --- a/src/client/httpclient.ts +++ b/src/client/httpclient.ts @@ -95,7 +95,7 @@ export class HttpClient extends BaseClient { return this.onPacket(res, req); }); this.app.listen(port, () => { - this.logger.info(`Listening to port ${port}`); + this.logger.info(`Listening to :${port}/interactions`); }); } else { this.logger.warn('No UWS installed.'); diff --git a/src/client/oninteractioncreate.ts b/src/client/oninteractioncreate.ts index 11bd03d..e30ee24 100644 --- a/src/client/oninteractioncreate.ts +++ b/src/client/oninteractioncreate.ts @@ -1,8 +1,10 @@ import { ApplicationCommandType, InteractionType, type APIInteraction } from 'discord-api-types/v10'; import { + BaseCommand, CommandContext, MenuCommandContext, OptionResolver, + type RegisteredMiddlewares, type Command, type ContextMenuCommand, type ContextOptionsResolved, @@ -101,7 +103,11 @@ export async function onInteractionCreate( return command.onBotPermissionsFail(context, interaction.appPermissions.keys(permissions)); } } - const resultRunGlobalMiddlewares = await command.__runGlobalMiddlewares(context); + const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares( + context, + (self.options?.globalMiddlewares ?? []) as keyof RegisteredMiddlewares, + true, + ); if (resultRunGlobalMiddlewares.pass) { return; } @@ -109,7 +115,11 @@ export async function onInteractionCreate( return command.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error'); } - const resultRunMiddlewares = await command.__runMiddlewares(context); + const resultRunMiddlewares = await BaseCommand.__runMiddlewares( + context, + command.middlewares as keyof RegisteredMiddlewares, + false, + ); if (resultRunMiddlewares.pass) { return; } diff --git a/src/commands/applications/chat.ts b/src/commands/applications/chat.ts index f35e1a6..baa9c03 100644 --- a/src/commands/applications/chat.ts +++ b/src/commands/applications/chat.ts @@ -6,7 +6,14 @@ import { type APIApplicationCommandSubcommandGroupOption, type LocaleString, } from 'discord-api-types/v10'; -import type { PermissionStrings, SeyfertNumberOption, SeyfertStringOption } from '../..'; +import type { + ComponentContext, + MenuCommandContext, + ModalContext, + PermissionStrings, + SeyfertNumberOption, + SeyfertStringOption, +} from '../..'; import type { Attachment } from '../../builders'; import { magicImport, type FlatObjectKeys } from '../../common'; import type { AllChannels, AutocompleteInteraction, GuildRole, InteractionGuildMember, User } from '../../structures'; @@ -103,7 +110,7 @@ type ContextOptionsAux = { export type ContextOptions = ContextOptionsAux; -class BaseCommand { +export class BaseCommand { middlewares: (keyof RegisteredMiddlewares)[] = []; __filePath?: string; @@ -178,7 +185,7 @@ class BaseCommand { /** @internal */ static __runMiddlewares( - context: CommandContext<{}, never>, + context: CommandContext<{}, never> | ComponentContext | MenuCommandContext | ModalContext, middlewares: (keyof RegisteredMiddlewares)[], global: boolean, ): Promise<{ error?: string; pass?: boolean }> { diff --git a/src/commands/applications/menu.ts b/src/commands/applications/menu.ts index 81d1a61..b08f8fb 100644 --- a/src/commands/applications/menu.ts +++ b/src/commands/applications/menu.ts @@ -2,7 +2,7 @@ import type { ApplicationCommandType, LocaleString } from 'discord-api-types/v10 import { magicImport, type PermissionStrings } from '../../common'; import type { IntegrationTypes, InteractionContextTypes, RegisteredMiddlewares } from '../decorators'; import type { MenuCommandContext } from './menucontext'; -import type { PassFunction, StopFunction, UsingClient } from './shared'; +import type { UsingClient } from './shared'; export abstract class ContextMenuCommand { middlewares: (keyof RegisteredMiddlewares)[] = []; @@ -23,66 +23,6 @@ export abstract class ContextMenuCommand { name_localizations?: Partial>; description_localizations?: Partial>; - /** @internal */ - static __runMiddlewares( - context: MenuCommandContext, - middlewares: (keyof RegisteredMiddlewares)[], - global: boolean, - ): Promise<{ error?: string; pass?: boolean }> { - if (!middlewares.length) { - return Promise.resolve({}); - } - let index = 0; - - return new Promise(res => { - let running = true; - const pass: PassFunction = () => { - if (!running) { - return; - } - running = false; - return res({ pass: true }); - }; - function next(obj: any) { - if (!running) { - return; - } - // biome-ignore lint/style/noArguments: yes - if (arguments.length) { - // @ts-expect-error - context[global ? 'globalMetadata' : 'metadata'][middlewares[index]] = obj; - } - if (++index >= middlewares.length) { - running = false; - return res({}); - } - context.client.middlewares![middlewares[index]]({ context, next, stop, pass }); - } - const stop: StopFunction = err => { - if (!running) { - return; - } - running = false; - return res({ error: err }); - }; - context.client.middlewares![middlewares[0]]({ context, next, stop, pass }); - }); - } - - /** @internal */ - __runMiddlewares(context: MenuCommandContext) { - return ContextMenuCommand.__runMiddlewares(context, this.middlewares as (keyof RegisteredMiddlewares)[], false); - } - - /** @internal */ - __runGlobalMiddlewares(context: MenuCommandContext) { - return ContextMenuCommand.__runMiddlewares( - context, - (context.client.options?.globalMiddlewares ?? []) as (keyof RegisteredMiddlewares)[], - true, - ); - } - toJSON() { return { name: this.name, diff --git a/src/commands/basecontext.ts b/src/commands/basecontext.ts index b92e8bc..6dd2f75 100644 --- a/src/commands/basecontext.ts +++ b/src/commands/basecontext.ts @@ -38,4 +38,28 @@ export class BaseContext { isModal(): this is ModalContext { return false; } + + isButton(): this is ComponentContext<'Button'> { + return false; + } + + isChannelSelectMenu(): this is ComponentContext<'ChannelSelect'> { + return false; + } + + isRoleSelectMenu(): this is ComponentContext<'RoleSelect'> { + return false; + } + + isMentionableSelectMenu(): this is ComponentContext<'MentionableSelect'> { + return false; + } + + isUserSelectMenu(): this is ComponentContext<'UserSelect'> { + return false; + } + + isStringSelectMenu(): this is ComponentContext<'StringSelect'> { + return false; + } } diff --git a/src/components/componentcommand.ts b/src/components/componentcommand.ts index cdf744e..ac5b3ce 100644 --- a/src/components/componentcommand.ts +++ b/src/components/componentcommand.ts @@ -1,6 +1,6 @@ import { ComponentType } from 'discord-api-types/v10'; import type { ContextComponentCommandInteractionMap, ComponentContext } from './componentcontext'; -import type { PassFunction, RegisteredMiddlewares, StopFunction, UsingClient } from '../commands'; +import type { RegisteredMiddlewares, UsingClient } from '../commands'; export const InteractionCommandType = { COMPONENT: 0, @@ -33,63 +33,4 @@ export abstract class ComponentCommand { } middlewares: (keyof RegisteredMiddlewares)[] = []; - /** @internal */ - static __runMiddlewares( - context: ComponentContext, - middlewares: (keyof RegisteredMiddlewares)[], - global: boolean, - ): Promise<{ error?: string; pass?: boolean }> { - if (!middlewares.length) { - return Promise.resolve({}); - } - let index = 0; - - return new Promise(res => { - let running = true; - const pass: PassFunction = () => { - if (!running) { - return; - } - running = false; - return res({ pass: true }); - }; - function next(obj: any) { - if (!running) { - return; - } - // biome-ignore lint/style/noArguments: yes - if (arguments.length) { - // @ts-expect-error - context[global ? 'globalMetadata' : 'metadata'][middlewares[index]] = obj; - } - if (++index >= middlewares.length) { - running = false; - return res({}); - } - context.client.middlewares![middlewares[index]]({ context, next, stop, pass }); - } - const stop: StopFunction = err => { - if (!running) { - return; - } - running = false; - return res({ error: err }); - }; - context.client.middlewares![middlewares[0]]({ context, next, stop, pass }); - }); - } - - /** @internal */ - __runMiddlewares(context: ComponentContext) { - return ComponentCommand.__runMiddlewares(context, this.middlewares as (keyof RegisteredMiddlewares)[], false); - } - - /** @internal */ - __runGlobalMiddlewares(context: ComponentContext) { - return ComponentCommand.__runMiddlewares( - context, - (context.client.options?.globalMiddlewares ?? []) as (keyof RegisteredMiddlewares)[], - true, - ); - } } diff --git a/src/components/handler.ts b/src/components/handler.ts index 3d729f3..6927d32 100644 --- a/src/components/handler.ts +++ b/src/components/handler.ts @@ -1,6 +1,6 @@ import type { ComponentCallback, ListenerOptions, ModalSubmitCallback } from '../builders/types'; import { LimitedCollection } from '../collection'; -import type { UsingClient } from '../commands'; +import { BaseCommand, type RegisteredMiddlewares, type UsingClient } from '../commands'; import { BaseHandler, magicImport, type Logger, type OnFailCallback } from '../common'; import type { ComponentInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from '../structures'; import { ComponentCommand, InteractionCommandType } from './componentcommand'; @@ -160,7 +160,7 @@ export class ComponentHandler extends BaseHandler { component = this.callback(paths[i].file); if (!component) continue; } catch (e) { - if (e instanceof Error && e.message === 'paths[i].file is not a constructor') { + if (e instanceof Error && e.message.includes('is not a constructor')) { this.logger.warn( `${paths[i].path .split(process.cwd()) @@ -215,7 +215,11 @@ export class ComponentHandler extends BaseHandler { Object.assign(context, extended); if (!(await i.filter(context))) continue; try { - const resultRunGlobalMiddlewares = await i.__runGlobalMiddlewares(context); + const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares( + context, + (context.client.options?.globalMiddlewares ?? []) as keyof RegisteredMiddlewares, + true, + ); if (resultRunGlobalMiddlewares.pass) { return; } @@ -223,7 +227,7 @@ export class ComponentHandler extends BaseHandler { return i.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error'); } - const resultRunMiddlewares = await i.__runMiddlewares(context); + const resultRunMiddlewares = await BaseCommand.__runMiddlewares(context, i.middlewares, false); if (resultRunMiddlewares.pass) { return; } @@ -258,7 +262,11 @@ export class ComponentHandler extends BaseHandler { try { if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) { try { - const resultRunGlobalMiddlewares = await i.__runGlobalMiddlewares(context); + const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares( + context, + (context.client.options?.globalMiddlewares ?? []) as keyof RegisteredMiddlewares, + true, + ); if (resultRunGlobalMiddlewares.pass) { return; } @@ -266,7 +274,7 @@ export class ComponentHandler extends BaseHandler { return i.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error'); } - const resultRunMiddlewares = await i.__runMiddlewares(context); + const resultRunMiddlewares = await BaseCommand.__runMiddlewares(context, i.middlewares, false); if (resultRunMiddlewares.pass) { return; } diff --git a/src/components/modalcommand.ts b/src/components/modalcommand.ts index 6c5647e..33a7ea2 100644 --- a/src/components/modalcommand.ts +++ b/src/components/modalcommand.ts @@ -1,4 +1,4 @@ -import type { RegisteredMiddlewares, PassFunction, StopFunction, UsingClient } from '../commands'; +import type { RegisteredMiddlewares, UsingClient } from '../commands'; import { InteractionCommandType } from './componentcommand'; import type { ModalContext } from './modalcontext'; @@ -23,64 +23,4 @@ export abstract class ModalCommand { onInternalError(client: UsingClient, error?: unknown): any { client.logger.fatal(error); } - - /** @internal */ - static __runMiddlewares( - context: ModalContext, - middlewares: (keyof RegisteredMiddlewares)[], - global: boolean, - ): Promise<{ error?: string; pass?: boolean }> { - if (!middlewares.length) { - return Promise.resolve({}); - } - let index = 0; - - return new Promise(res => { - let running = true; - const pass: PassFunction = () => { - if (!running) { - return; - } - running = false; - return res({ pass: true }); - }; - function next(obj: any) { - if (!running) { - return; - } - // biome-ignore lint/style/noArguments: yes - if (arguments.length) { - // @ts-expect-error - context[global ? 'globalMetadata' : 'metadata'][middlewares[index]] = obj; - } - if (++index >= middlewares.length) { - running = false; - return res({}); - } - context.client.middlewares![middlewares[index]]({ context, next, stop, pass }); - } - const stop: StopFunction = err => { - if (!running) { - return; - } - running = false; - return res({ error: err }); - }; - context.client.middlewares![middlewares[0]]({ context, next, stop, pass }); - }); - } - - /** @internal */ - __runMiddlewares(context: ModalContext) { - return ModalCommand.__runMiddlewares(context, this.middlewares as (keyof RegisteredMiddlewares)[], false); - } - - /** @internal */ - __runGlobalMiddlewares(context: ModalContext) { - return ModalCommand.__runMiddlewares( - context, - (context.client.options?.globalMiddlewares ?? []) as (keyof RegisteredMiddlewares)[], - true, - ); - } } diff --git a/src/components/modalcontext.ts b/src/components/modalcontext.ts index 270705b..7390e49 100644 --- a/src/components/modalcontext.ts +++ b/src/components/modalcontext.ts @@ -32,6 +32,10 @@ export class ModalContext extends metadata: CommandMetadata> = {} as never; globalMetadata: GlobalMetadata = {}; + get customId() { + return this.interaction.customId; + } + get components() { return this.interaction.components; }