From 5baa921b7c7887bb1d03085a723c98fd5129447d Mon Sep 17 00:00:00 2001 From: NoBody Date: Fri, 29 Mar 2024 21:11:26 -0500 Subject: [PATCH] fix: errors in componentcontext class and add docs (#169) --- src/components/componentcontext.ts | 91 +++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 7 deletions(-) diff --git a/src/components/componentcontext.ts b/src/components/componentcontext.ts index 628a9fa..5068b3c 100644 --- a/src/components/componentcontext.ts +++ b/src/components/componentcontext.ts @@ -16,13 +16,27 @@ import type { } from '..'; import type { ExtendContext, UsingClient } from '../commands'; import { BaseContext } from '../commands/basecontex'; -import type { InteractionCreateBodyRequest, InteractionMessageUpdateBodyRequest, When } from '../common'; +import type { + ComponentInteractionMessageUpdate, + InteractionCreateBodyRequest, + InteractionMessageUpdateBodyRequest, + When, +} from '../common'; export interface ComponentContext extends BaseContext, ExtendContext {} +/** + * Represents a context for interacting with components in a Discord bot. + * @template Type - The type of component interaction. + */ export class ComponentContext extends BaseContext { + /** + * Creates a new instance of the ComponentContext class. + * @param client - The UsingClient instance. + * @param interaction - The component interaction object. + */ constructor( readonly client: UsingClient, public interaction: ComponentCommandInteractionMap[Type] | ComponentInteraction, @@ -30,34 +44,65 @@ export class ComponentContext super(client); } + /** + * Gets the proxy object. + */ get proxy() { return this.client.proxy; } + /** + * Gets the language object for the interaction's locale. + */ get t() { return this.client.langs!.get(this.interaction?.locale ?? this.client.langs?.defaultLang ?? 'en-US'); } + /** + * Gets the custom ID of the interaction. + */ get customId() { return this.interaction.customId; } - get write() { - return this.interaction.write; + /** + * Writes a response to the interaction. + * @param body - The body of the response. + * @param fetchReply - Whether to fetch the reply or not. + */ + write(body: InteractionCreateBodyRequest, fetchReply?: FR) { + return this.interaction.write(body, fetchReply); } + /** + * Defers the reply to the interaction. + * @param ephemeral - Whether the reply should be ephemeral or not. + */ deferReply(ephemeral = false) { return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined); } - get editResponse() { - return this.interaction.editResponse; + /** + * Edits the response of the interaction. + * @param body - The updated body of the response. + */ + editResponse(body: InteractionMessageUpdateBodyRequest) { + return this.interaction.editResponse(body); } - get update() { - return this.interaction.update; + /** + * Updates the interaction with new data. + * @param body - The updated body of the interaction. + */ + update(body: ComponentInteractionMessageUpdate) { + return this.interaction.update(body); } + /** + * Edits the response or replies to the interaction. + * @param body - The body of the response or updated body of the interaction. + * @param fetchReply - Whether to fetch the reply or not. + */ editOrReply( body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, fetchReply?: FR, @@ -65,9 +110,19 @@ export class ComponentContext return this.interaction.editOrReply(body as InteractionCreateBodyRequest, fetchReply); } + /** + * Deletes the response of the interaction. + * @returns A promise that resolves when the response is deleted. + */ deleteResponse() { return this.interaction.deleteResponse(); } + + /** + * Gets the channel of the interaction. + * @param mode - The mode to fetch the channel. + * @returns A promise that resolves to the channel. + */ channel(mode?: 'rest' | 'flow'): Promise; channel(mode?: 'cache'): ReturnCache; channel(mode: 'cache' | 'rest' | 'flow' = 'cache') { @@ -76,6 +131,11 @@ export class ComponentContext return this.client.channels.fetch(this.channelId, mode === 'rest'); } + /** + * Gets the bot member in the guild of the interaction. + * @param mode - The mode to fetch the member. + * @returns A promise that resolves to the bot member. + */ me(mode?: 'rest' | 'flow'): Promise; me(mode?: 'cache'): ReturnCache; me(mode: 'cache' | 'rest' | 'flow' = 'cache') { @@ -89,6 +149,11 @@ export class ComponentContext } } + /** + * Gets the guild of the interaction. + * @param mode - The mode to fetch the guild. + * @returns A promise that resolves to the guild. + */ guild(mode?: 'rest' | 'flow'): Promise | undefined>; guild(mode?: 'cache'): ReturnCache | undefined>; guild(mode: 'cache' | 'rest' | 'flow' = 'cache') { @@ -104,18 +169,30 @@ export class ComponentContext } } + /** + * Gets the ID of the guild of the interaction. + */ get guildId() { return this.interaction.guildId; } + /** + * Gets the ID of the channel of the interaction. + */ get channelId() { return this.interaction.channelId!; } + /** + * Gets the author of the interaction. + */ get author() { return this.interaction.user; } + /** + * Gets the member of the interaction. + */ get member() { return this.interaction.member; }