fix: errors in componentcontext class and add docs (#169)

This commit is contained in:
NoBody 2024-03-29 21:11:26 -05:00 committed by GitHub
parent 388164594a
commit 5baa921b7c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<Type extends keyof ComponentCommandInteractionMap>
extends BaseContext,
ExtendContext {}
/**
* Represents a context for interacting with components in a Discord bot.
* @template Type - The type of component interaction.
*/
export class ComponentContext<Type extends keyof ComponentCommandInteractionMap> 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<Type extends keyof ComponentCommandInteractionMap>
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<FR extends boolean = false>(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<FR extends boolean = false>(
body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest,
fetchReply?: FR,
@ -65,9 +110,19 @@ export class ComponentContext<Type extends keyof ComponentCommandInteractionMap>
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<AllChannels>;
channel(mode?: 'cache'): ReturnCache<AllChannels>;
channel(mode: 'cache' | 'rest' | 'flow' = 'cache') {
@ -76,6 +131,11 @@ export class ComponentContext<Type extends keyof ComponentCommandInteractionMap>
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<GuildMember>;
me(mode?: 'cache'): ReturnCache<GuildMember | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'cache') {
@ -89,6 +149,11 @@ export class ComponentContext<Type extends keyof ComponentCommandInteractionMap>
}
}
/**
* 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<Guild<'cached' | 'api'> | undefined>;
guild(mode?: 'cache'): ReturnCache<Guild<'cached'> | undefined>;
guild(mode: 'cache' | 'rest' | 'flow' = 'cache') {
@ -104,18 +169,30 @@ export class ComponentContext<Type extends keyof ComponentCommandInteractionMap>
}
}
/**
* 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;
}