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';
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;

View File

@ -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<UnionToTuple<M>> = {} as never;
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 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,

View File

@ -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<M extends keyof RegisteredMiddlewares = never> extends
super(client);
}
command?: ModalCommand;
metadata: CommandMetadata<UnionToTuple<M>> = {} as never;
globalMetadata: GlobalMetadata = {};