feat: assign .command to context

This commit is contained in:
MARCROCK22 2024-05-11 00:19:41 -04:00
parent 62380def98
commit c6bd0f77e2
4 changed files with 28 additions and 11 deletions

View File

@ -19,7 +19,7 @@ import type {
} from '../structures'; } from '../structures';
import { AutocompleteInteraction, BaseInteraction } from '../structures'; import { AutocompleteInteraction, BaseInteraction } from '../structures';
import type { BaseClient } from './base'; import type { BaseClient } from './base';
import { ModalContext } from '../components'; import { ComponentContext, ModalContext } from '../components';
export async function onInteractionCreate( export async function onInteractionCreate(
self: BaseClient, self: BaseClient,
@ -232,7 +232,11 @@ export async function onInteractionCreate(
if (self.components?.hasComponent(body.message.id, interaction.customId)) { if (self.components?.hasComponent(body.message.id, interaction.customId)) {
await self.components.onComponent(body.message.id, interaction); await self.components.onComponent(body.message.id, interaction);
} else { } 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; break;

View File

@ -3,6 +3,7 @@ import type {
AllChannels, AllChannels,
ButtonInteraction, ButtonInteraction,
ChannelSelectMenuInteraction, ChannelSelectMenuInteraction,
ComponentCommand,
Guild, Guild,
GuildMember, GuildMember,
MentionableSelectMenuInteraction, MentionableSelectMenuInteraction,
@ -49,6 +50,7 @@ export class ComponentContext<
super(client); super(client);
} }
command?: ComponentCommand;
metadata: CommandMetadata<UnionToTuple<M>> = {} as never; metadata: CommandMetadata<UnionToTuple<M>> = {} as never;
globalMetadata: GlobalMetadata = {}; globalMetadata: GlobalMetadata = {};

View File

@ -4,7 +4,7 @@ import { BaseCommand, type RegisteredMiddlewares, type UsingClient } from '../co
import { BaseHandler, magicImport, type Logger, type OnFailCallback } from '../common'; import { BaseHandler, magicImport, type Logger, type OnFailCallback } from '../common';
import type { ComponentInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from '../structures'; import type { ComponentInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from '../structures';
import { ComponentCommand, InteractionCommandType } from './componentcommand'; import { ComponentCommand, InteractionCommandType } from './componentcommand';
import { ComponentContext } from './componentcontext'; import type { ComponentContext } from './componentcontext';
import { ModalCommand } from './modalcommand'; import { ModalCommand } from './modalcommand';
import type { ModalContext } from './modalcontext'; 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) { for (const i of this.commands) {
try { try {
if (i.type === InteractionCommandType.COMPONENT && i.cType === interaction.componentType) { if (
// @ts-expect-error ComponentInteraction is a generic class i.type === InteractionCommandType.COMPONENT &&
const context = new ComponentContext(this.client, interaction); i.cType === context.interaction.componentType &&
const extended = this.client.options?.context?.(interaction) ?? {}; (await i.filter(context))
Object.assign(context, extended); ) {
if (!(await i.filter(context))) continue; context.command = i;
try { try {
const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares( const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares(
context, context,
@ -272,6 +272,7 @@ export class ComponentHandler extends BaseHandler {
for (const i of this.commands) { for (const i of this.commands) {
try { try {
if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) { if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) {
context.command = i;
try { try {
const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares( const resultRunGlobalMiddlewares = await BaseCommand.__runMiddlewares(
context, context,

View File

@ -1,5 +1,14 @@
import { MessageFlags } from 'discord-api-types/v10'; 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 type { CommandMetadata, ExtendContext, GlobalMetadata, RegisteredMiddlewares, UsingClient } from '../commands';
import { BaseContext } from '../commands/basecontext'; import { BaseContext } from '../commands/basecontext';
import type { import type {
@ -29,6 +38,7 @@ export class ModalContext<M extends keyof RegisteredMiddlewares = never> extends
super(client); super(client);
} }
command?: ModalCommand;
metadata: CommandMetadata<UnionToTuple<M>> = {} as never; metadata: CommandMetadata<UnionToTuple<M>> = {} as never;
globalMetadata: GlobalMetadata = {}; globalMetadata: GlobalMetadata = {};