import { CommandContext, type ReturnCache } from '../..'; import { ApplicationCommandType, MessageFlags, toSnakeCase, type InteractionCreateBodyRequest, type InteractionMessageUpdateBodyRequest, type UnionToTuple, } from '../../common'; import { Message, User, type AllChannels, type Guild, type GuildMember, type MessageCommandInteraction, type UserCommandInteraction, } from '../../structures'; import type { RegisteredMiddlewares } from '../decorators'; import type { CommandMetadata, ExtendContext, GlobalMetadata, UsingClient } from './shared'; export type InteractionTarget = T extends MessageCommandInteraction ? Message : User; export interface MenuCommandContext< T extends MessageCommandInteraction | UserCommandInteraction, M extends keyof RegisteredMiddlewares = never, > extends ExtendContext {} export class MenuCommandContext< T extends MessageCommandInteraction | UserCommandInteraction, M extends keyof RegisteredMiddlewares = never, > { constructor( readonly client: UsingClient, readonly interaction: T, readonly shardId: number, ) {} metadata: CommandMetadata> = {} as never; globalMetadata: GlobalMetadata = {}; get proxy() { return this.client.proxy; } // biome-ignore lint/suspicious/useGetterReturn: default don't exist. get target(): InteractionTarget { switch (this.interaction.data.type) { case ApplicationCommandType.Message: { const data = this.interaction.data.resolved.messages[this.interaction.data.targetId as Lowercase]; return new Message(this.client, toSnakeCase(data)) as never; } case ApplicationCommandType.User: { const data = this.interaction.data.resolved.users[this.interaction.data.targetId as Lowercase]; return new User(this.client, toSnakeCase(data)) as never; } } } get t() { return this.client.langs.get(this.interaction.locale); } write(body: InteractionCreateBodyRequest) { return this.interaction.write(body); } get modal() { return this.interaction.modal; } deferReply(ephemeral = false) { return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined); } editResponse(body: InteractionMessageUpdateBodyRequest) { return this.interaction.editResponse(body); } deleteResponse() { return this.interaction.deleteResponse(); } editOrReply(body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest) { return this.interaction.editOrReply(body as InteractionCreateBodyRequest); } fetchResponse() { return this.interaction.fetchResponse(); } channel(mode?: 'rest' | 'flow'): Promise; channel(mode?: 'cache'): ReturnCache; channel(mode: 'cache' | 'rest' | 'flow' = 'cache') { if (this.interaction?.channel && mode === 'cache') return this.client.cache.asyncCache ? Promise.resolve(this.interaction.channel) : this.interaction.channel; return this.client.channels.fetch(this.channelId, mode === 'rest'); } me(mode?: 'rest' | 'flow'): Promise; me(mode?: 'cache'): ReturnCache; me(mode: 'cache' | 'rest' | 'flow' = 'cache') { if (!this.guildId) return mode === 'cache' ? (this.client.cache.asyncCache ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': return this.client.cache.members?.get(this.client.botId, this.guildId); default: return this.client.members.fetch(this.guildId, this.client.botId, mode === 'rest'); } } guild(mode?: 'rest' | 'flow'): Promise | undefined>; guild(mode?: 'cache'): ReturnCache | undefined>; guild(mode: 'cache' | 'rest' | 'flow' = 'cache') { if (!this.guildId) return ( mode === 'cache' ? (this.client.cache.asyncCache ? Promise.resolve() : undefined) : Promise.resolve() ) as any; switch (mode) { case 'cache': return this.client.cache.guilds?.get(this.guildId); default: return this.client.guilds.fetch(this.guildId, mode === 'rest'); } } get guildId() { return this.interaction.guildId; } get channelId() { return this.interaction.channelId!; } get author() { return this.interaction.user; } get member() { return this.interaction.member; } isChat(): this is CommandContext { return this instanceof CommandContext; } isMenu(): this is MenuCommandContext { return this instanceof MenuCommandContext; } isMenuUser(): this is MenuCommandContext { return this instanceof MenuCommandContext && this.target instanceof User; } isMenuMessage(): this is MenuCommandContext { return this instanceof MenuCommandContext && this.target instanceof Message; } }