From c6bd0f77e2cd725e63a0ae6d3900708a64b1dd40 Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Sat, 11 May 2024 00:19:41 -0400 Subject: [PATCH] feat: assign .command to context --- src/client/oninteractioncreate.ts | 8 ++++++-- src/components/componentcontext.ts | 2 ++ src/components/handler.ts | 17 +++++++++-------- src/components/modalcontext.ts | 12 +++++++++++- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/client/oninteractioncreate.ts b/src/client/oninteractioncreate.ts index 8448f92..9210b88 100644 --- a/src/client/oninteractioncreate.ts +++ b/src/client/oninteractioncreate.ts @@ -19,7 +19,7 @@ import type { } from '../structures'; import { AutocompleteInteraction, BaseInteraction } from '../structures'; import type { BaseClient } from './base'; -import { ModalContext } from '../components'; +import { ComponentContext, ModalContext } from '../components'; export async function onInteractionCreate( self: BaseClient, @@ -232,7 +232,11 @@ export async function onInteractionCreate( if (self.components?.hasComponent(body.message.id, interaction.customId)) { await self.components.onComponent(body.message.id, interaction); } else { - await self.components?.executeComponent(interaction); + //@ts-expect-error + const context = new ComponentContext(self, interaction); + const extended = self.options?.context?.(interaction) ?? {}; + Object.assign(context, extended); + await self.components?.executeComponent(context); } } break; diff --git a/src/components/componentcontext.ts b/src/components/componentcontext.ts index 053e3e0..c7bf26f 100644 --- a/src/components/componentcontext.ts +++ b/src/components/componentcontext.ts @@ -3,6 +3,7 @@ import type { AllChannels, ButtonInteraction, ChannelSelectMenuInteraction, + ComponentCommand, Guild, GuildMember, MentionableSelectMenuInteraction, @@ -49,6 +50,7 @@ export class ComponentContext< super(client); } + command?: ComponentCommand; metadata: CommandMetadata> = {} as never; globalMetadata: GlobalMetadata = {}; diff --git a/src/components/handler.ts b/src/components/handler.ts index 3a43ef8..dbf404e 100644 --- a/src/components/handler.ts +++ b/src/components/handler.ts @@ -4,7 +4,7 @@ import { BaseCommand, type RegisteredMiddlewares, type UsingClient } from '../co import { BaseHandler, magicImport, type Logger, type OnFailCallback } from '../common'; import type { ComponentInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from '../structures'; import { ComponentCommand, InteractionCommandType } from './componentcommand'; -import { ComponentContext } from './componentcontext'; +import type { ComponentContext } from './componentcontext'; import { ModalCommand } from './modalcommand'; import type { ModalContext } from './modalcontext'; @@ -216,15 +216,15 @@ export class ComponentHandler extends BaseHandler { } } - async executeComponent(interaction: ComponentInteraction) { + async executeComponent(context: ComponentContext) { for (const i of this.commands) { try { - if (i.type === InteractionCommandType.COMPONENT && i.cType === interaction.componentType) { - // @ts-expect-error ComponentInteraction is a generic class - const context = new ComponentContext(this.client, interaction); - const extended = this.client.options?.context?.(interaction) ?? {}; - Object.assign(context, extended); - if (!(await i.filter(context))) continue; + if ( + i.type === InteractionCommandType.COMPONENT && + i.cType === context.interaction.componentType && + (await i.filter(context)) + ) { + context.command = i; try { const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares( context, @@ -272,6 +272,7 @@ export class ComponentHandler extends BaseHandler { for (const i of this.commands) { try { if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) { + context.command = i; try { const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares( context, diff --git a/src/components/modalcontext.ts b/src/components/modalcontext.ts index 7390e49..af3f1a3 100644 --- a/src/components/modalcontext.ts +++ b/src/components/modalcontext.ts @@ -1,5 +1,14 @@ import { MessageFlags } from 'discord-api-types/v10'; -import type { AllChannels, Guild, GuildMember, Message, ModalSubmitInteraction, ReturnCache, WebhookMessage } from '..'; +import type { + AllChannels, + Guild, + GuildMember, + Message, + ModalCommand, + ModalSubmitInteraction, + ReturnCache, + WebhookMessage, +} from '..'; import type { CommandMetadata, ExtendContext, GlobalMetadata, RegisteredMiddlewares, UsingClient } from '../commands'; import { BaseContext } from '../commands/basecontext'; import type { @@ -29,6 +38,7 @@ export class ModalContext extends super(client); } + command?: ModalCommand; metadata: CommandMetadata> = {} as never; globalMetadata: GlobalMetadata = {};