import type { ReturnCache } from '../..'; import type { GuildMemberStructure, GuildStructure, InteractionGuildMemberStructure, UserStructure, WebhookMessageStructure, } from '../../client/transformers'; import type { InteractionCreateBodyRequest, InteractionMessageUpdateBodyRequest, MakeRequired, MessageWebhookCreateBodyRequest, ModalCreateBodyRequest, UnionToTuple, When, } from '../../common'; import type { AllChannels, EntryPointInteraction } from '../../structures'; import { MessageFlags } from '../../types'; import { BaseContext } from '../basecontext'; import type { RegisteredMiddlewares } from '../decorators'; import type { EntryPointCommand } from './entryPoint'; import type { CommandMetadata, ExtendContext, GlobalMetadata, UsingClient } from './shared'; export interface EntryPointContext extends BaseContext, ExtendContext {} export class EntryPointContext extends BaseContext { constructor( readonly client: UsingClient, readonly interaction: EntryPointInteraction, readonly shardId: number, readonly command: EntryPointCommand, ) { super(client); } metadata: CommandMetadata> = {} as never; globalMetadata: GlobalMetadata = {}; get t() { return this.client.t(this.interaction.locale ?? this.client.langs.defaultLang ?? 'en-US'); } get fullCommandName() { return this.command.name; } write( body: InteractionCreateBodyRequest, withResponse?: WR, ): Promise> { return this.interaction.write(body, withResponse); } modal(body: ModalCreateBodyRequest) { return this.interaction.modal(body); } deferReply( ephemeral = false, withResponse?: WR, ): Promise> { return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined, withResponse); } editResponse(body: InteractionMessageUpdateBodyRequest): Promise { return this.interaction.editResponse(body); } deleteResponse() { return this.interaction.deleteResponse(); } editOrReply( body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, withResponse?: WR, ): Promise> { return this.interaction.editOrReply(body as InteractionCreateBodyRequest, withResponse); } followup(body: MessageWebhookCreateBodyRequest): Promise { return this.interaction.followup(body); } fetchResponse(): Promise { return this.interaction.fetchResponse(); } channel(mode?: 'rest' | 'flow'): Promise; channel(mode: 'cache'): ReturnCache; channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { if (mode === 'cache') return this.client.cache.adapter.isAsync ? 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' = 'flow') { if (!this.guildId) return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': return ( this.client.cache.members?.get(this.client.botId, this.guildId) || (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) ); 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' = 'flow') { if (!this.guildId) return ( mode === 'cache' ? (this.client.cache.adapter.isAsync ? 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.channel.id; } get author(): UserStructure { return this.interaction.user; } get member(): InteractionGuildMemberStructure | undefined { return this.interaction.member; } isEntryPoint(): this is EntryPointContext { return true; } inGuild(): this is GuildEntryPointContext { return !!this.guildId; } } export interface GuildEntryPointContext extends Omit, 'guildId' | 'member'>, 'guild' | 'me'> { guild(mode?: 'rest' | 'flow'): Promise>; guild(mode: 'cache'): ReturnCache | undefined>; me(mode?: 'rest' | 'flow'): Promise; me(mode: 'cache'): ReturnCache; }