Skip to content

types(interactions): fix overloads #10702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 75 additions & 11 deletions packages/core/__tests__/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,90 @@ const api = new API(rest);
const SNOWFLAKE = '123456789012345678' as const;
const TOKEN = 'token' as const;
const MODAL_COMPONENTS: APIActionRowComponent<APIModalActionRowComponent>[] = [] as const;
const boolValue = true as boolean;

describe('Interaction with_response overloads.', () => {
test('Replying returns RESTPostAPIInteractionCallbackWithResponseResult.', () =>
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult>>(
api.interactions.reply(SNOWFLAKE, TOKEN, { with_response: true }),
));

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

test('Replying returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => {
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>>(
api.interactions.reply(SNOWFLAKE, TOKEN, { with_response: boolValue }),
);
});

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

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

test('Defer returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => {
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>>(
api.interactions.defer(SNOWFLAKE, TOKEN, { with_response: boolValue }),
);
});

test('Defer message update returns RESTPostAPIInteractionCallbackWithResponseResult.', () =>
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult>>(
api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN, { with_response: true }),
));

test('Defer message update returns undefined.', () =>
assertType<Promise<undefined>>(api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN, {})));
test('Defer message update returns undefined.', () => {
assertType<Promise<undefined>>(api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN));
assertType<Promise<undefined>>(api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN, { with_response: false }));
});

test('Defer message update returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => {
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>>(
api.interactions.deferMessageUpdate(SNOWFLAKE, TOKEN, { with_response: boolValue }),
);
});

test('Update message returns RESTPostAPIInteractionCallbackWithResponseResult.', () =>
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult>>(
api.interactions.updateMessage(SNOWFLAKE, TOKEN, { with_response: true }),
));

test('Update message returns undefined.', () =>
assertType<Promise<undefined>>(api.interactions.updateMessage(SNOWFLAKE, TOKEN, {})));
test('Update message returns undefined.', () => {
assertType<Promise<undefined>>(api.interactions.updateMessage(SNOWFLAKE, TOKEN, {}));
assertType<Promise<undefined>>(api.interactions.updateMessage(SNOWFLAKE, TOKEN, { with_response: false }));
});

test('Update message returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => {
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>>(
api.interactions.updateMessage(SNOWFLAKE, TOKEN, { with_response: boolValue }),
);
});

test('Create autocomplete response returns RESTPostAPIInteractionCallbackWithResponseResult.', () =>
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult>>(
api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, { with_response: true }),
));

test('Create autocomplete response returns undefined.', () =>
assertType<Promise<undefined>>(api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, {})));
test('Create autocomplete response returns undefined.', () => {
assertType<Promise<undefined>>(api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, {}));
assertType<Promise<undefined>>(
api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, { with_response: false }),
);
});

test('Create autocomplete response returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => {
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>>(
api.interactions.createAutocompleteResponse(SNOWFLAKE, TOKEN, { with_response: boolValue }),
);
});

test('Create modal returns RESTPostAPIInteractionCallbackWithResponseResult.', () =>
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult>>(
Expand All @@ -63,8 +107,28 @@ describe('Interaction with_response overloads.', () => {
}),
));

test('Create modal returns undefined.', () =>
test('Create modal returns undefined.', () => {
assertType<Promise<undefined>>(
api.interactions.createModal(SNOWFLAKE, TOKEN, { title: '', custom_id: '', components: MODAL_COMPONENTS }),
));
);
assertType<Promise<undefined>>(
api.interactions.createModal(SNOWFLAKE, TOKEN, {
title: '',
custom_id: '',
components: MODAL_COMPONENTS,
with_response: false,
}),
);
});

test('Create modal returns either RESTPostAPIInteractionCallbackWithResponseResult or undefined.', () => {
assertType<Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>>(
api.interactions.createModal(SNOWFLAKE, TOKEN, {
title: '',
custom_id: '',
components: MODAL_COMPONENTS,
with_response: boolValue,
}),
);
});
});
96 changes: 96 additions & 0 deletions packages/core/src/api/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ export class InteractionsAPI {
options?: Pick<RequestData, 'signal'>,
): Promise<undefined>;

/**
* 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<RequestData, 'signal'>,
): Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>;

public async reply(
interactionId: Snowflake,
interactionToken: string,
Expand Down Expand Up @@ -125,6 +141,22 @@ export class InteractionsAPI {
options?: Pick<RequestData, 'signal'>,
): Promise<undefined>;

/**
* 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<RequestData, 'signal'>,
): Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>;

public async defer(
interactionId: Snowflake,
interactionToken: string,
Expand Down Expand Up @@ -176,6 +208,22 @@ export class InteractionsAPI {
options?: Pick<RequestData, 'signal'>,
): Promise<undefined>;

/**
* 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<RequestData, 'signal'>,
): Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>;

public async deferMessageUpdate(
interactionId: Snowflake,
interactionToken: string,
Expand Down Expand Up @@ -308,6 +356,22 @@ export class InteractionsAPI {
options?: Pick<RequestData, 'signal'>,
): Promise<undefined>;

/**
* 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<RequestData, 'signal'>,
): Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>;

public async updateMessage(
interactionId: Snowflake,
interactionToken: string,
Expand Down Expand Up @@ -360,6 +424,22 @@ export class InteractionsAPI {
options?: Pick<RequestData, 'signal'>,
): Promise<undefined>;

/**
* 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<RequestData, 'signal'>,
): Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>;

public async createAutocompleteResponse(
interactionId: Snowflake,
interactionToken: string,
Expand Down Expand Up @@ -411,6 +491,22 @@ export class InteractionsAPI {
options?: Pick<RequestData, 'signal'>,
): Promise<undefined>;

/**
* 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<RequestData, 'signal'>,
): Promise<RESTPostAPIInteractionCallbackWithResponseResult | undefined>;

public async createModal(
interactionId: Snowflake,
interactionToken: string,
Expand Down
Loading