Skip to content

Commit

Permalink
feat(interactions): add launchActivity method (#10646)
Browse files Browse the repository at this point in the history
* feat(interactions): add launchActivity method

* chore: suggestion

Co-authored-by: Jiralite <[email protected]>

* chore: suggestion

Co-authored-by: Jiralite <[email protected]>

* fix: overload and add tests

* chore: wording

* chore: wording

* chore: spacing

---------

Co-authored-by: Jiralite <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 24, 2025
1 parent 3d85d96 commit a3fa1a8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/core/__tests__/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,19 @@ describe('Interaction with_response overloads.', () => {
}),
);
});

test('Launch activity returns undefined.', () => {
assertType<Promise<undefined>>(api.interactions.launchActivity(SNOWFLAKE, TOKEN, { with_response: false }));
assertType<Promise<undefined>>(api.interactions.launchActivity(SNOWFLAKE, TOKEN));
});

test('Launch activity returns RESTPostAPIInteractionCallbackWithResponseResult.', () =>
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult>>(
api.interactions.launchActivity(SNOWFLAKE, TOKEN, { with_response: true }),
));

test('Launch activity returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () =>
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>>(
api.interactions.launchActivity(SNOWFLAKE, TOKEN, { with_response: boolValue }),
));
});
66 changes: 66 additions & 0 deletions packages/core/src/api/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,70 @@ export class InteractionsAPI {

return with_response ? response : undefined;
}

/**
* Launches an activity and returns an interaction callback object
*
* @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 launching the activity
* @param options - The options for launching the activity
*/
public async launchActivity(
interactionId: Snowflake,
interactionToken: string,
body: RESTPostAPIInteractionCallbackQuery & { with_response: true },
options?: Pick<RequestData, 'signal'>,
): Promise<RESTPostAPIInteractionCallbackWithResponseResult>;

/**
* Launches an activity
*
* @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 launching the activity
* @param options - The options for launching the activity
*/
public async launchActivity(
interactionId: Snowflake,
interactionToken: string,
body?: RESTPostAPIInteractionCallbackQuery & { with_response?: false },
options?: Pick<RequestData, 'signal'>,
): Promise<undefined>;

/**
* Launches an activity
*
* @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 launching the activity
* @param options - The options for launching the activity
*/
public async launchActivity(
interactionId: Snowflake,
interactionToken: string,
body?: RESTPostAPIInteractionCallbackQuery,
options?: Pick<RequestData, 'signal'>,
): Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>;

public async launchActivity(
interactionId: Snowflake,
interactionToken: string,
{ with_response }: RESTPostAPIInteractionCallbackQuery = {},
{ signal }: Pick<RequestData, 'signal'> = {},
) {
const response = await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
query: makeURLSearchParams({ with_response }),
auth: false,
body: {
type: InteractionResponseType.LaunchActivity,
},
signal,
});

return with_response ? response : undefined;
}
}

0 comments on commit a3fa1a8

Please sign in to comment.