feat(core): Add AbortSignal support. (#9042)

* feat: add abort signal to guilds api

* feat: add to application commands, channels, and users classes

* chore: finish up

* chore: centralize types

* chore: make requested changes

* chore: make requested changes

* refactor: consistently use empty objects

* Update packages/core/src/api/webhook.ts

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>

* chore: make requested changes

* refactor: update `setVoiceState` after rebase

* chore: requested changes

* refactor: use -types interface for query

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
Suneet Tipirneni
2023-04-01 17:11:37 -04:00
committed by GitHub
parent e2f39ccc32
commit 907eb1b470
12 changed files with 959 additions and 444 deletions

View File

@@ -1,11 +1,11 @@
import type { RawFile, REST } from '@discordjs/rest';
import type { RawFile, RequestData, REST } from '@discordjs/rest';
import { InteractionResponseType, Routes } from 'discord-api-types/v10';
import type {
Snowflake,
APICommandAutocompleteInteractionResponseCallbackData,
APIInteractionResponseCallbackData,
APIModalInteractionResponseCallbackData,
RESTGetAPIWebhookWithTokenMessageResult,
Snowflake,
} from 'discord-api-types/v10';
import type { WebhooksAPI } from './webhook.js';
@@ -18,12 +18,14 @@ export class InteractionsAPI {
* @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 data - The data to use when replying
* @param body - The callback data to use when replying
* @param options - The options to use when replying
*/
public async reply(
interactionId: Snowflake,
interactionToken: string,
{ files, ...data }: APIInteractionResponseCallbackData & { files?: RawFile[] },
{ signal }: Pick<RequestData, 'signal'> = {},
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
files,
@@ -32,6 +34,7 @@ export class InteractionsAPI {
type: InteractionResponseType.ChannelMessageWithSource,
data,
},
signal,
});
}
@@ -41,13 +44,15 @@ export class InteractionsAPI {
* @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 options - The options to use when deferring
*/
public async defer(interactionId: Snowflake, interactionToken: string) {
public async defer(interactionId: Snowflake, interactionToken: string, { signal }: Pick<RequestData, 'signal'> = {}) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
auth: false,
body: {
type: InteractionResponseType.DeferredChannelMessageWithSource,
},
signal,
});
}
@@ -57,13 +62,19 @@ export class InteractionsAPI {
* @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 options - The options to use when deferring
*/
public async deferMessageUpdate(interactionId: Snowflake, interactionToken: string) {
public async deferMessageUpdate(
interactionId: Snowflake,
interactionToken: string,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
auth: false,
body: {
type: InteractionResponseType.DeferredMessageUpdate,
},
signal,
});
}
@@ -73,14 +84,16 @@ export class InteractionsAPI {
* @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#create-followup-message}
* @param applicationId - The application id of the interaction
* @param interactionToken - The token of the interaction
* @param data - The data to use when replying
* @param body - The callback data to use when replying
* @param options - The options to use when replying
*/
public async followUp(
applicationId: Snowflake,
interactionToken: string,
data: APIInteractionResponseCallbackData & { files?: RawFile[] },
body: APIInteractionResponseCallbackData & { files?: RawFile[] },
{ signal }: Pick<RequestData, 'signal'> = {},
) {
await this.webhooks.execute(applicationId, interactionToken, data);
await this.webhooks.execute(applicationId, interactionToken, body, { signal });
}
/**
@@ -90,16 +103,20 @@ export class InteractionsAPI {
* @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#edit-followup-message}
* @param applicationId - The application id of the interaction
* @param interactionToken - The token of the interaction
* @param data - The data to use when editing the reply
* @param callbackData - The callback data to use when editing the reply
* @param messageId - The id of the message to edit. If omitted, the original reply will be edited
* @param options - The options to use when editing the reply
*/
public async editReply(
applicationId: Snowflake,
interactionToken: string,
data: APIInteractionResponseCallbackData & { files?: RawFile[] },
callbackData: APIInteractionResponseCallbackData & { files?: RawFile[] },
messageId?: Snowflake | '@original',
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.webhooks.editMessage(applicationId, interactionToken, messageId ?? '@original', data);
return this.webhooks.editMessage(applicationId, interactionToken, messageId ?? '@original', callbackData, {
signal,
});
}
/**
@@ -108,12 +125,19 @@ export class InteractionsAPI {
* @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#get-original-interaction-response}
* @param applicationId - The application id of the interaction
* @param interactionToken - The token of the interaction
* @param options - The options to use when fetching the reply
*/
public async getOriginalReply(applicationId: Snowflake, interactionToken: string) {
public async getOriginalReply(
applicationId: Snowflake,
interactionToken: string,
{ signal }: Pick<RequestData, 'signal'>,
) {
return this.webhooks.getMessage(
applicationId,
interactionToken,
'@original',
{},
{ signal },
) as Promise<RESTGetAPIWebhookWithTokenMessageResult>;
}
@@ -125,9 +149,15 @@ export class InteractionsAPI {
* @param applicationId - The application id of the interaction
* @param interactionToken - The token of the interaction
* @param messageId - The id of the message to delete. If omitted, the original reply will be deleted
* @param options - The options to use when deleting the reply
*/
public async deleteReply(applicationId: Snowflake, interactionToken: string, messageId?: Snowflake | '@original') {
await this.webhooks.deleteMessage(applicationId, interactionToken, messageId ?? '@original');
public async deleteReply(
applicationId: Snowflake,
interactionToken: string,
messageId?: Snowflake | '@original',
{ signal }: Pick<RequestData, 'signal'> = {},
) {
await this.webhooks.deleteMessage(applicationId, interactionToken, messageId ?? '@original', {}, { signal });
}
/**
@@ -136,12 +166,14 @@ export class InteractionsAPI {
* @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 data - The data to use when updating the interaction
* @param callbackData - The callback data to use when updating the interaction
* @param options - The options to use when updating the interaction
*/
public async updateMessage(
interactionId: Snowflake,
interactionToken: string,
{ files, ...data }: APIInteractionResponseCallbackData & { files?: RawFile[] },
{ signal }: Pick<RequestData, 'signal'> = {},
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
files,
@@ -150,6 +182,7 @@ export class InteractionsAPI {
type: InteractionResponseType.UpdateMessage,
data,
},
signal,
});
}
@@ -159,19 +192,22 @@ export class InteractionsAPI {
* @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 data - Data for the autocomplete response
* @param callbackData - The callback data for the autocomplete response
* @param options - The options to use when sending the autocomplete response
*/
public async createAutocompleteResponse(
interactionId: Snowflake,
interactionToken: string,
data: APICommandAutocompleteInteractionResponseCallbackData,
callbackData: APICommandAutocompleteInteractionResponseCallbackData,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
auth: false,
body: {
type: InteractionResponseType.ApplicationCommandAutocompleteResult,
data,
data: callbackData,
},
signal,
});
}
@@ -181,19 +217,22 @@ export class InteractionsAPI {
* @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 data - The modal to send
* @param callbackData - The modal callback data to send
* @param options - The options to use when sending the modal
*/
public async createModal(
interactionId: Snowflake,
interactionToken: string,
data: APIModalInteractionResponseCallbackData,
callbackData: APIModalInteractionResponseCallbackData,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
auth: false,
body: {
type: InteractionResponseType.Modal,
data,
data: callbackData,
},
signal,
});
}
}