From aa90f00d1125f85443cdb96abfe9a07414b9c80f Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sun, 19 Jan 2025 01:19:41 +0600 Subject: [PATCH] types(interactions): fix overloads (#10702) Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> --- packages/core/__tests__/types.test.ts | 86 +++++++++++++++++++++--- packages/core/src/api/interactions.ts | 96 +++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 11 deletions(-) diff --git a/packages/core/__tests__/types.test.ts b/packages/core/__tests__/types.test.ts index 104b5882be20..95f1ea7a04cc 100644 --- a/packages/core/__tests__/types.test.ts +++ b/packages/core/__tests__/types.test.ts @@ -12,6 +12,7 @@ const api = new API(rest); const SNOWFLAKE = '123456789012345678' as const; const TOKEN = 'token' as const; const MODAL_COMPONENTS: APIActionRowComponent[] = [] as const; +const boolValue = true as boolean; describe('Interaction with_response overloads.', () => { test('Replying returns RESTPostAPIInteractionCallbackWithResponseResult.', () => @@ -19,39 +20,82 @@ describe('Interaction with_response overloads.', () => { api.interactions.reply(SNOWFLAKE, TOKEN, { with_response: true }), )); - test('Replying returns undefined.', () => - assertType>(api.interactions.reply(SNOWFLAKE, TOKEN, {}))); + test('Replying returns undefined.', () => { + assertType>(api.interactions.reply(SNOWFLAKE, TOKEN, {})); + assertType>(api.interactions.reply(SNOWFLAKE, TOKEN, { with_response: false })); + }); + + test('Replying returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => { + assertType>( + api.interactions.reply(SNOWFLAKE, TOKEN, { with_response: boolValue }), + ); + }); test('Defer returns RESTPostAPIInteractionCallbackWithResponseResult.', () => assertType>( api.interactions.defer(SNOWFLAKE, TOKEN, { with_response: true }), )); - test('Defer returns undefined.', () => assertType>(api.interactions.defer(SNOWFLAKE, TOKEN, {}))); + test('Defer returns undefined.', () => { + assertType>(api.interactions.defer(SNOWFLAKE, TOKEN)); + assertType>(api.interactions.defer(SNOWFLAKE, TOKEN, { with_response: false })); + }); + + test('Defer returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => { + assertType>( + api.interactions.defer(SNOWFLAKE, TOKEN, { with_response: boolValue }), + ); + }); test('Defer message update returns RESTPostAPIInteractionCallbackWithResponseResult.', () => assertType>( api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN, { with_response: true }), )); - test('Defer message update returns undefined.', () => - assertType>(api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN, {}))); + test('Defer message update returns undefined.', () => { + assertType>(api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN)); + assertType>(api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN, { with_response: false })); + }); + + test('Defer message update returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => { + assertType>( + api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN, { with_response: boolValue }), + ); + }); test('Update message returns RESTPostAPIInteractionCallbackWithResponseResult.', () => assertType>( api.interactions.updateMessage(SNOWFLAKE, TOKEN, { with_response: true }), )); - test('Update message returns undefined.', () => - assertType>(api.interactions.updateMessage(SNOWFLAKE, TOKEN, {}))); + test('Update message returns undefined.', () => { + assertType>(api.interactions.updateMessage(SNOWFLAKE, TOKEN, {})); + assertType>(api.interactions.updateMessage(SNOWFLAKE, TOKEN, { with_response: false })); + }); + + test('Update message returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => { + assertType>( + api.interactions.updateMessage(SNOWFLAKE, TOKEN, { with_response: boolValue }), + ); + }); test('Create autocomplete response returns RESTPostAPIInteractionCallbackWithResponseResult.', () => assertType>( api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, { with_response: true }), )); - test('Create autocomplete response returns undefined.', () => - assertType>(api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, {}))); + test('Create autocomplete response returns undefined.', () => { + assertType>(api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, {})); + assertType>( + api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, { with_response: false }), + ); + }); + + test('Create autocomplete response returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => { + assertType>( + api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, { with_response: boolValue }), + ); + }); test('Create modal returns RESTPostAPIInteractionCallbackWithResponseResult.', () => assertType>( @@ -63,8 +107,28 @@ describe('Interaction with_response overloads.', () => { }), )); - test('Create modal returns undefined.', () => + test('Create modal returns undefined.', () => { assertType>( api.interactions.createModal(SNOWFLAKE, TOKEN, { title: '', custom_id: '', components: MODAL_COMPONENTS }), - )); + ); + assertType>( + api.interactions.createModal(SNOWFLAKE, TOKEN, { + title: '', + custom_id: '', + components: MODAL_COMPONENTS, + with_response: false, + }), + ); + }); + + test('Create modal returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => { + assertType>( + api.interactions.createModal(SNOWFLAKE, TOKEN, { + title: '', + custom_id: '', + components: MODAL_COMPONENTS, + with_response: boolValue, + }), + ); + }); }); diff --git a/packages/core/src/api/interactions.ts b/packages/core/src/api/interactions.ts index eb159c85d84c..82aac602bb95 100644 --- a/packages/core/src/api/interactions.ts +++ b/packages/core/src/api/interactions.ts @@ -73,6 +73,22 @@ export class InteractionsAPI { options?: Pick, ): Promise; + /** + * Replies to an interaction + * + * @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#create-interaction-response} + * @param interactionId - The id of the interaction + * @param interactionToken - The token of the interaction + * @param body - The callback data for replying + * @param options - The options for replying + */ + public async reply( + interactionId: Snowflake, + interactionToken: string, + body: CreateInteractionResponseOptions, + options?: Pick, + ): Promise; + public async reply( interactionId: Snowflake, interactionToken: string, @@ -125,6 +141,22 @@ export class InteractionsAPI { options?: Pick, ): Promise; + /** + * Defers the reply to an interaction + * + * @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#create-interaction-response} + * @param interactionId - The id of the interaction + * @param interactionToken - The token of the interaction + * @param body - The callback data for deferring the reply + * @param options - The options for deferring + */ + public async defer( + interactionId: Snowflake, + interactionToken: string, + body?: CreateInteractionDeferResponseOptions, + options?: Pick, + ): Promise; + public async defer( interactionId: Snowflake, interactionToken: string, @@ -176,6 +208,22 @@ export class InteractionsAPI { options?: Pick, ): Promise; + /** + * Defers an update from a message component interaction + * + * @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#create-interaction-response} + * @param interactionId - The id of the interaction + * @param interactionToken - The token of the interaction + * @param body - The callback data for deferring the update + * @param options - The options for deferring + */ + public async deferMessageUpdate( + interactionId: Snowflake, + interactionToken: string, + body?: RESTPostAPIInteractionCallbackQuery, + options?: Pick, + ): Promise; + public async deferMessageUpdate( interactionId: Snowflake, interactionToken: string, @@ -308,6 +356,22 @@ export class InteractionsAPI { options?: Pick, ): Promise; + /** + * Updates the message the component interaction was triggered on + * + * @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#create-interaction-response} + * @param interactionId - The id of the interaction + * @param interactionToken - The token of the interaction + * @param callbackData - The callback data for updating the interaction + * @param options - The options for updating the interaction + */ + public async updateMessage( + interactionId: Snowflake, + interactionToken: string, + callbackData: CreateInteractionUpdateMessageResponseOptions, + options?: Pick, + ): Promise; + public async updateMessage( interactionId: Snowflake, interactionToken: string, @@ -360,6 +424,22 @@ export class InteractionsAPI { options?: Pick, ): Promise; + /** + * Sends an autocomplete response to an interaction + * + * @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#create-interaction-response} + * @param interactionId - The id of the interaction + * @param interactionToken - The token of the interaction + * @param callbackData - The callback data for the autocomplete response + * @param options - The options for sending the autocomplete response + */ + public async createAutocompleteResponse( + interactionId: Snowflake, + interactionToken: string, + callbackData: CreateAutocompleteResponseOptions, + options?: Pick, + ): Promise; + public async createAutocompleteResponse( interactionId: Snowflake, interactionToken: string, @@ -411,6 +491,22 @@ export class InteractionsAPI { options?: Pick, ): Promise; + /** + * Sends a modal response to an interaction + * + * @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#create-interaction-response} + * @param interactionId - The id of the interaction + * @param interactionToken - The token of the interaction + * @param callbackData - The modal callback data to send + * @param options - The options for sending the modal + */ + public async createModal( + interactionId: Snowflake, + interactionToken: string, + callbackData: CreateModalResponseOptions, + options?: Pick, + ): Promise; + public async createModal( interactionId: Snowflake, interactionToken: string,