From 907eb1b4708bdaf30f4e59f4016ef8a717f47a4c Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com> Date: Sat, 1 Apr 2023 17:11:37 -0400 Subject: [PATCH] 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> --- packages/core/src/api/applicationCommands.ts | 163 +++-- packages/core/src/api/channel.ts | 220 +++++-- packages/core/src/api/guild.ts | 594 ++++++++++++------- packages/core/src/api/interactions.ts | 81 ++- packages/core/src/api/invite.ts | 15 +- packages/core/src/api/oauth2.ts | 44 +- packages/core/src/api/roleConnections.ts | 20 +- packages/core/src/api/sticker.ts | 12 +- packages/core/src/api/thread.ts | 68 ++- packages/core/src/api/user.ts | 75 ++- packages/core/src/api/voice.ts | 9 +- packages/core/src/api/webhook.ts | 102 +++- 12 files changed, 959 insertions(+), 444 deletions(-) diff --git a/packages/core/src/api/applicationCommands.ts b/packages/core/src/api/applicationCommands.ts index 67df3a3e7..4d80d4bd0 100644 --- a/packages/core/src/api/applicationCommands.ts +++ b/packages/core/src/api/applicationCommands.ts @@ -1,4 +1,4 @@ -import { makeURLSearchParams, type REST } from '@discordjs/rest'; +import { makeURLSearchParams, type RequestData, type REST } from '@discordjs/rest'; import { Routes, type RESTGetAPIApplicationCommandPermissionsResult, @@ -26,11 +26,17 @@ export class ApplicationCommandsAPI { * * @see {@link https://discord.com/developers/docs/interactions/application-commands#get-global-application-commands} * @param applicationId - The application id to fetch commands for + * @param query - The query options to use when fetching commands * @param options - The options to use when fetching commands */ - public async getGlobalCommands(applicationId: Snowflake, options: RESTGetAPIApplicationCommandsQuery = {}) { + public async getGlobalCommands( + applicationId: Snowflake, + query: RESTGetAPIApplicationCommandsQuery = {}, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.applicationCommands(applicationId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -39,11 +45,17 @@ export class ApplicationCommandsAPI { * * @see {@link https://discord.com/developers/docs/interactions/application-commands#create-global-application-command} * @param applicationId - The application id to create the command for - * @param data - The data to use when creating the command + * @param body - The data to use when creating the command + * @param options - The options to use when creating the command */ - public async createGlobalCommand(applicationId: Snowflake, data: RESTPostAPIApplicationCommandsJSONBody) { + public async createGlobalCommand( + applicationId: Snowflake, + body: RESTPostAPIApplicationCommandsJSONBody, + { signal }: Pick = {}, + ) { return this.rest.post(Routes.applicationCommands(applicationId), { - body: data, + body, + signal, }) as Promise; } @@ -53,11 +65,16 @@ export class ApplicationCommandsAPI { * @see {@link https://discord.com/developers/docs/interactions/application-commands#get-global-application-command} * @param applicationId - The application id to fetch the command from * @param commandId - The command id to fetch + * @param options - The options to use when fetching the command */ - public async getGlobalCommand(applicationId: Snowflake, commandId: Snowflake) { - return this.rest.get( - Routes.applicationCommand(applicationId, commandId), - ) as Promise; + public async getGlobalCommand( + applicationId: Snowflake, + commandId: Snowflake, + { signal }: Pick = {}, + ) { + return this.rest.get(Routes.applicationCommand(applicationId, commandId), { + signal, + }) as Promise; } /** @@ -66,15 +83,18 @@ export class ApplicationCommandsAPI { * @see {@link https://discord.com/developers/docs/interactions/application-commands#edit-global-application-command} * @param applicationId - The application id of the command * @param commandId - The id of the command to edit - * @param data - The data to use when editing the command + * @param body - The data to use when editing the command + * @param options - The options for editing the command */ public async editGlobalCommand( applicationId: Snowflake, commandId: Snowflake, - data: RESTPatchAPIApplicationCommandJSONBody, + body: RESTPatchAPIApplicationCommandJSONBody, + { signal }: Pick = {}, ) { return this.rest.patch(Routes.applicationCommand(applicationId, commandId), { - body: data, + body, + signal, }) as Promise; } @@ -84,9 +104,14 @@ export class ApplicationCommandsAPI { * @see {@link https://discord.com/developers/docs/interactions/application-commands#delete-global-application-command} * @param applicationId - The application id of the command * @param commandId - The id of the command to delete + * @param options - The options for deleting a command */ - public async deleteGlobalCommand(applicationId: Snowflake, commandId: Snowflake) { - await this.rest.delete(Routes.applicationCommand(applicationId, commandId)); + public async deleteGlobalCommand( + applicationId: Snowflake, + commandId: Snowflake, + { signal }: Pick = {}, + ) { + await this.rest.delete(Routes.applicationCommand(applicationId, commandId), { signal }); } /** @@ -94,11 +119,17 @@ export class ApplicationCommandsAPI { * * @see {@link https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-global-application-commands} * @param applicationId - The application id to overwrite commands for - * @param data - The data to use when overwriting commands + * @param body - The data to use when overwriting commands + * @param options - The options for overwriting commands */ - public async bulkOverwriteGlobalCommands(applicationId: Snowflake, data: RESTPutAPIApplicationCommandsJSONBody) { + public async bulkOverwriteGlobalCommands( + applicationId: Snowflake, + body: RESTPutAPIApplicationCommandsJSONBody, + { signal }: Pick = {}, + ) { return this.rest.put(Routes.applicationCommands(applicationId), { - body: data, + body, + signal, }) as Promise; } @@ -108,15 +139,18 @@ export class ApplicationCommandsAPI { * @see {@link https://discord.com/developers/docs/interactions/application-commands#get-guild-application-commands} * @param applicationId - The application id to fetch commands for * @param guildId - The guild id to fetch commands for - * @param data - The data to use when fetching commands + * @param query - The data to use when fetching commands + * @param options - The options to use when fetching commands */ public async getGuildCommands( applicationId: Snowflake, guildId: Snowflake, - data: RESTGetAPIApplicationGuildCommandsQuery = {}, + query: RESTGetAPIApplicationGuildCommandsQuery = {}, + { signal }: Pick = {}, ) { return this.rest.get(Routes.applicationGuildCommands(applicationId, guildId), { - query: makeURLSearchParams(data), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -126,15 +160,18 @@ export class ApplicationCommandsAPI { * @see {@link https://discord.com/developers/docs/interactions/application-commands#create-guild-application-command} * @param applicationId - The application id to create the command for * @param guildId - The guild id to create the command for - * @param data - The data to use when creating the command + * @param body - The data to use when creating the command + * @param options - The options to use when creating the command */ public async createGuildCommand( applicationId: Snowflake, guildId: Snowflake, - data: RESTPostAPIApplicationCommandsJSONBody, + body: RESTPostAPIApplicationCommandsJSONBody, + { signal }: Pick = {}, ) { return this.rest.post(Routes.applicationGuildCommands(applicationId, guildId), { - body: data, + body, + signal, }) as Promise; } @@ -145,11 +182,17 @@ export class ApplicationCommandsAPI { * @param applicationId - The application id to fetch the command from * @param guildId - The guild id to fetch the command from * @param commandId - The command id to fetch + * @param options - The options to use when fetching the command */ - public async getGuildCommand(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) { - return this.rest.get( - Routes.applicationGuildCommand(applicationId, guildId, commandId), - ) as Promise; + public async getGuildCommand( + applicationId: Snowflake, + guildId: Snowflake, + commandId: Snowflake, + { signal }: Pick = {}, + ) { + return this.rest.get(Routes.applicationGuildCommand(applicationId, guildId, commandId), { + signal, + }) as Promise; } /** @@ -159,16 +202,19 @@ export class ApplicationCommandsAPI { * @param applicationId - The application id of the command * @param guildId - The guild id of the command * @param commandId - The command id to edit - * @param data - The data to use when editing the command + * @param body - The data to use when editing the command + * @param options - The options to use when editing the command */ public async editGuildCommand( applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake, - data: RESTPatchAPIApplicationCommandJSONBody, + body: RESTPatchAPIApplicationCommandJSONBody, + { signal }: Pick = {}, ) { return this.rest.patch(Routes.applicationGuildCommand(applicationId, guildId, commandId), { - body: data, + body, + signal, }) as Promise; } @@ -179,9 +225,15 @@ export class ApplicationCommandsAPI { * @param applicationId - The application id of the command * @param guildId - The guild id of the command * @param commandId - The id of the command to delete + * @param options - The options for deleting the command */ - public async deleteGuildCommand(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) { - await this.rest.delete(Routes.applicationGuildCommand(applicationId, guildId, commandId)); + public async deleteGuildCommand( + applicationId: Snowflake, + guildId: Snowflake, + commandId: Snowflake, + { signal }: Pick = {}, + ) { + await this.rest.delete(Routes.applicationGuildCommand(applicationId, guildId, commandId), { signal }); } /** @@ -190,15 +242,18 @@ export class ApplicationCommandsAPI { * @see {@link https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-guild-application-commands} * @param applicationId - The application id to overwrite commands for * @param guildId - The guild id to overwrite commands for - * @param data - The data to use when overwriting commands + * @param body - The data to use when overwriting commands + * @param options - The options to use when overwriting the commands */ public async bulkOverwriteGuildCommands( applicationId: Snowflake, guildId: Snowflake, - data: RESTPutAPIApplicationCommandsJSONBody, + body: RESTPutAPIApplicationCommandsJSONBody, + { signal }: Pick = {}, ) { return this.rest.put(Routes.applicationGuildCommands(applicationId, guildId), { - body: data, + body, + signal, }) as Promise; } @@ -209,11 +264,17 @@ export class ApplicationCommandsAPI { * @param applicationId - The application id to get the permissions for * @param guildId - The guild id of the command * @param commandId - The command id to get the permissions for + * @param options - The option for fetching the command */ - public async getGuildCommandPermissions(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) { - return this.rest.get( - Routes.applicationCommandPermissions(applicationId, guildId, commandId), - ) as Promise; + public async getGuildCommandPermissions( + applicationId: Snowflake, + guildId: Snowflake, + commandId: Snowflake, + { signal }: Pick = {}, + ) { + return this.rest.get(Routes.applicationCommandPermissions(applicationId, guildId, commandId), { + signal, + }) as Promise; } /** @@ -222,11 +283,16 @@ export class ApplicationCommandsAPI { * @see {@link https://discord.com/developers/docs/interactions/application-commands#get-application-command-permissions} * @param applicationId - The application id to get the permissions for * @param guildId - The guild id to get the permissions for + * @param options - The options for fetching permissions */ - public async getGuildCommandsPermissions(applicationId: Snowflake, guildId: Snowflake) { - return this.rest.get( - Routes.guildApplicationCommandsPermissions(applicationId, guildId), - ) as Promise; + public async getGuildCommandsPermissions( + applicationId: Snowflake, + guildId: Snowflake, + { signal }: Pick = {}, + ) { + return this.rest.get(Routes.guildApplicationCommandsPermissions(applicationId, guildId), { + signal, + }) as Promise; } /** @@ -237,19 +303,22 @@ export class ApplicationCommandsAPI { * @param applicationId - The application id to edit the permissions for * @param guildId - The guild id to edit the permissions for * @param commandId - The id of the command to edit the permissions for - * @param data - The data to use when editing the permissions + * @param body - The data to use when editing the permissions + * @param options - The options to use when editing the permissions */ public async editGuildCommandPermissions( userToken: string, applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake, - data: RESTPutAPIApplicationCommandPermissionsJSONBody, + body: RESTPutAPIApplicationCommandPermissionsJSONBody, + { signal }: Pick = {}, ) { return this.rest.put(Routes.applicationCommandPermissions(applicationId, guildId, commandId), { headers: { Authorization: `Bearer ${userToken.replace('Bearer ', '')}` }, auth: false, - body: data, + body, + signal, }) as Promise; } } diff --git a/packages/core/src/api/channel.ts b/packages/core/src/api/channel.ts index 2d56fba71..af4596845 100644 --- a/packages/core/src/api/channel.ts +++ b/packages/core/src/api/channel.ts @@ -1,4 +1,4 @@ -import { makeURLSearchParams, type RawFile, type REST } from '@discordjs/rest'; +import { makeURLSearchParams, type RawFile, type REST, type RequestData } from '@discordjs/rest'; import { Routes, type RESTDeleteAPIChannelResult, @@ -33,15 +33,18 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#create-message} * @param channelId - The id of the channel to send the message in - * @param data - The data to use when sending the message + * @param body - The data to use when sending the message + * @param options - The options to use when sending the message */ public async createMessage( channelId: Snowflake, { files, ...body }: RESTPostAPIChannelMessageJSONBody & { files?: RawFile[] }, + { signal }: Pick = {}, ) { return this.rest.post(Routes.channelMessages(channelId), { files, body, + signal, }) as Promise; } @@ -51,16 +54,19 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#edit-message} * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to edit - * @param data - The data to use when editing the message + * @param body - The data to use when editing the message + * @param options - The options to use when editing the message */ public async editMessage( channelId: Snowflake, messageId: Snowflake, { files, ...body }: RESTPostAPIChannelMessageJSONBody & { files?: RawFile[] }, + { signal }: Pick, ) { return this.rest.patch(Routes.channelMessage(channelId, messageId), { files, body, + signal, }) as Promise; } @@ -71,16 +77,19 @@ export class ChannelsAPI { * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to get the reactions for * @param emoji - The emoji to get the reactions for - * @param options - The options to use when fetching the reactions + * @param query - The query options to use when fetching the reactions + * @param options - The options for fetching the message reactions */ public async getMessageReactions( channelId: Snowflake, messageId: Snowflake, emoji: string, - options: RESTGetAPIChannelMessageReactionUsersQuery = {}, + query: RESTGetAPIChannelMessageReactionUsersQuery = {}, + { signal }: Pick = {}, ) { return this.rest.get(Routes.channelMessageReaction(channelId, messageId, encodeURIComponent(emoji)), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -91,9 +100,17 @@ export class ChannelsAPI { * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to delete the reaction for * @param emoji - The emoji to delete the reaction for + * @param options - The options for deleting the reaction */ - public async deleteOwnMessageReaction(channelId: Snowflake, messageId: Snowflake, emoji: string) { - await this.rest.delete(Routes.channelMessageOwnReaction(channelId, messageId, encodeURIComponent(emoji))); + public async deleteOwnMessageReaction( + channelId: Snowflake, + messageId: Snowflake, + emoji: string, + { signal }: Pick = {}, + ) { + await this.rest.delete(Routes.channelMessageOwnReaction(channelId, messageId, encodeURIComponent(emoji)), { + signal, + }); } /** @@ -104,9 +121,18 @@ export class ChannelsAPI { * @param messageId - The id of the message to delete the reaction for * @param emoji - The emoji to delete the reaction for * @param userId - The id of the user to delete the reaction for + * @param options - The options for deleting the reaction */ - public async deleteUserMessageReaction(channelId: Snowflake, messageId: Snowflake, emoji: string, userId: Snowflake) { - await this.rest.delete(Routes.channelMessageUserReaction(channelId, messageId, encodeURIComponent(emoji), userId)); + public async deleteUserMessageReaction( + channelId: Snowflake, + messageId: Snowflake, + emoji: string, + userId: Snowflake, + { signal }: Pick = {}, + ) { + await this.rest.delete(Routes.channelMessageUserReaction(channelId, messageId, encodeURIComponent(emoji), userId), { + signal, + }); } /** @@ -115,9 +141,14 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#delete-all-reactions} * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to delete the reactions for + * @param options - The options for deleting the reactions */ - public async deleteAllMessageReactions(channelId: Snowflake, messageId: Snowflake) { - await this.rest.delete(Routes.channelMessageAllReactions(channelId, messageId)); + public async deleteAllMessageReactions( + channelId: Snowflake, + messageId: Snowflake, + { signal }: Pick = {}, + ) { + await this.rest.delete(Routes.channelMessageAllReactions(channelId, messageId), { signal }); } /** @@ -127,9 +158,15 @@ export class ChannelsAPI { * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to delete the reactions for * @param emoji - The emoji to delete the reactions for + * @param options - The options for deleting the reactions */ - public async deleteAllMessageReactionsForEmoji(channelId: Snowflake, messageId: Snowflake, emoji: string) { - await this.rest.delete(Routes.channelMessageReaction(channelId, messageId, encodeURIComponent(emoji))); + public async deleteAllMessageReactionsForEmoji( + channelId: Snowflake, + messageId: Snowflake, + emoji: string, + { signal }: Pick = {}, + ) { + await this.rest.delete(Routes.channelMessageReaction(channelId, messageId, encodeURIComponent(emoji)), { signal }); } /** @@ -139,9 +176,15 @@ export class ChannelsAPI { * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to add the reaction to * @param emoji - The emoji to add the reaction with + * @param options - The options for adding the reaction */ - public async addMessageReaction(channelId: Snowflake, messageId: Snowflake, emoji: string) { - await this.rest.put(Routes.channelMessageOwnReaction(channelId, messageId, encodeURIComponent(emoji))); + public async addMessageReaction( + channelId: Snowflake, + messageId: Snowflake, + emoji: string, + { signal }: Pick = {}, + ) { + await this.rest.put(Routes.channelMessageOwnReaction(channelId, messageId, encodeURIComponent(emoji)), { signal }); } /** @@ -149,9 +192,10 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#get-channel} * @param channelId - The id of the channel + * @param options - The options for fetching the channel */ - public async get(channelId: Snowflake) { - return this.rest.get(Routes.channel(channelId)) as Promise; + public async get(channelId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.channel(channelId), { signal }) as Promise; } /** @@ -159,10 +203,15 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#modify-channel} * @param channelId - The id of the channel to edit - * @param data - The new channel data + * @param body - The new channel data + * @param options - The options for editing the channel */ - public async edit(channelId: Snowflake, data: RESTPatchAPIChannelJSONBody) { - return this.rest.patch(Routes.channel(channelId), { body: data }) as Promise; + public async edit( + channelId: Snowflake, + body: RESTPatchAPIChannelJSONBody, + { signal }: Pick = {}, + ) { + return this.rest.patch(Routes.channel(channelId), { body, signal }) as Promise; } /** @@ -170,9 +219,10 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#deleteclose-channel} * @param channelId - The id of the channel to delete + * @param options - The options for deleting the channel */ - public async delete(channelId: Snowflake) { - return this.rest.delete(Routes.channel(channelId)) as Promise; + public async delete(channelId: Snowflake, { signal }: Pick = {}) { + return this.rest.delete(Routes.channel(channelId), { signal }) as Promise; } /** @@ -180,11 +230,17 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#get-channel-messages} * @param channelId - The id of the channel to fetch messages from - * @param options - The options to use when fetching messages + * @param query - The query options to use when fetching messages + * @param options - The options for fetching the messages */ - public async getMessages(channelId: Snowflake, options: RESTGetAPIChannelMessagesQuery = {}) { + public async getMessages( + channelId: Snowflake, + query: RESTGetAPIChannelMessagesQuery = {}, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.channelMessages(channelId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -193,9 +249,10 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#trigger-typing-indicator} * @param channelId - The id of the channel to show the typing indicator in + * @param options - The options for showing the typing indicator */ - public async showTyping(channelId: Snowflake) { - await this.rest.post(Routes.channelTyping(channelId)); + public async showTyping(channelId: Snowflake, { signal }: Pick = {}) { + await this.rest.post(Routes.channelTyping(channelId), { signal }); } /** @@ -203,9 +260,10 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#get-pinned-messages} * @param channelId - The id of the channel to fetch pinned messages from + * @param options - The options for fetching the pinned messages */ - public async getPins(channelId: Snowflake) { - return this.rest.get(Routes.channelPins(channelId)) as Promise; + public async getPins(channelId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.channelPins(channelId), { signal }) as Promise; } /** @@ -214,10 +272,14 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#pin-message} * @param channelId - The id of the channel to pin the message in * @param messageId - The id of the message to pin - * @param reason - The reason for pinning the message + * @param options - The options for pinning the message */ - public async pinMessage(channelId: Snowflake, messageId: Snowflake, reason?: string) { - await this.rest.put(Routes.channelPin(channelId, messageId), { reason }); + public async pinMessage( + channelId: Snowflake, + messageId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.put(Routes.channelPin(channelId, messageId), { reason, signal }); } /** @@ -226,10 +288,14 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#delete-message} * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to delete - * @param reason - The reason for deleting the message + * @param options - The options for deleting the message */ - public async deleteMessage(channelId: Snowflake, messageId: Snowflake, reason?: string) { - await this.rest.delete(Routes.channelMessage(channelId, messageId), { reason }); + public async deleteMessage( + channelId: Snowflake, + messageId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.delete(Routes.channelMessage(channelId, messageId), { reason, signal }); } /** @@ -238,9 +304,14 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#bulk-delete-messages} * @param channelId - The id of the channel the messages are in * @param messageIds - The ids of the messages to delete + * @param options - The options for deleting the messages */ - public async bulkDeleteMessages(channelId: Snowflake, messageIds: Snowflake[], reason?: string): Promise { - await this.rest.post(Routes.channelBulkDelete(channelId), { reason, body: { messages: messageIds } }); + public async bulkDeleteMessages( + channelId: Snowflake, + messageIds: Snowflake[], + { reason, signal }: Pick = {}, + ): Promise { + await this.rest.post(Routes.channelBulkDelete(channelId), { reason, body: { messages: messageIds }, signal }); } /** @@ -249,9 +320,12 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#get-channel-message} * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to fetch + * @param options - The options for fetching the message */ - public async getMessage(channelId: Snowflake, messageId: Snowflake) { - return this.rest.get(Routes.channelMessage(channelId, messageId)) as Promise; + public async getMessage(channelId: Snowflake, messageId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.channelMessage(channelId, messageId), { + signal, + }) as Promise; } /** @@ -260,11 +334,16 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#crosspost-message} * @param channelId - The id of the channel the message is in * @param messageId - The id of the message to crosspost + * @param options - The options for crossposting the message */ - public async crosspostMessage(channelId: Snowflake, messageId: Snowflake) { - return this.rest.post( - Routes.channelMessageCrosspost(channelId, messageId), - ) as Promise; + public async crosspostMessage( + channelId: Snowflake, + messageId: Snowflake, + { signal }: Pick = {}, + ) { + return this.rest.post(Routes.channelMessageCrosspost(channelId, messageId), { + signal, + }) as Promise; } /** @@ -273,10 +352,14 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#unpin-message} * @param channelId - The id of the channel to unpin the message in * @param messageId - The id of the message to unpin - * @param reason - The reason for unpinning the message + * @param options - The options for unpinning the message */ - public async unpinMessage(channelId: Snowflake, messageId: Snowflake, reason?: string) { - await this.rest.delete(Routes.channelPin(channelId, messageId), { reason }); + public async unpinMessage( + channelId: Snowflake, + messageId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.delete(Routes.channelPin(channelId, messageId), { reason, signal }); } /** @@ -285,10 +368,16 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#follow-announcement-channel} * @param channelId - The id of the announcement channel to follow * @param webhookChannelId - The id of the webhook channel to follow the announcements in + * @param options - The options for following the announcement channel */ - public async followAnnouncements(channelId: Snowflake, webhookChannelId: Snowflake) { + public async followAnnouncements( + channelId: Snowflake, + webhookChannelId: Snowflake, + { signal }: Pick = {}, + ) { return this.rest.post(Routes.channelFollowers(channelId), { body: { webhook_channel_id: webhookChannelId }, + signal, }) as Promise; } @@ -297,12 +386,18 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#create-channel-invite} * @param channelId - The id of the channel to create an invite for - * @param data - The data to use when creating the invite + * @param body - The data to use when creating the invite + * @param options - The options for creating the invite */ - public async createInvite(channelId: Snowflake, data: RESTPostAPIChannelInviteJSONBody, reason?: string) { + public async createInvite( + channelId: Snowflake, + body: RESTPostAPIChannelInviteJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.post(Routes.channelInvites(channelId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -311,9 +406,10 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#get-channel-invites} * @param channelId - The id of the channel to fetch invites from + * @param options - The options for fetching the invites */ - public async getInvites(channelId: Snowflake) { - return this.rest.get(Routes.channelInvites(channelId)) as Promise; + public async getInvites(channelId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.channelInvites(channelId), { signal }) as Promise; } /** @@ -323,15 +419,18 @@ export class ChannelsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#list-private-archived-threads} * @param channelId - The id of the channel to fetch archived threads from * @param archivedStatus - The archived status of the threads to fetch - * @param options - The options to use when fetching archived threads + * @param query - The options to use when fetching archived threads + * @param options - The options for fetching archived threads */ public async getArchivedThreads( channelId: Snowflake, archivedStatus: 'private' | 'public', - options: RESTGetAPIChannelThreadsArchivedQuery = {}, + query: RESTGetAPIChannelThreadsArchivedQuery = {}, + { signal }: Pick = {}, ) { return this.rest.get(Routes.channelThreads(channelId, archivedStatus), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -340,14 +439,17 @@ export class ChannelsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#list-joined-private-archived-threads} * @param channelId - The id of the channel to fetch joined archived threads from - * @param options - The options to use when fetching joined archived threads + * @param query - The options to use when fetching joined archived threads + * @param options - The options for fetching joined archived threads */ public async getJoinedPrivateArchivedThreads( channelId: Snowflake, - options: RESTGetAPIChannelThreadsArchivedQuery = {}, + query: RESTGetAPIChannelThreadsArchivedQuery = {}, + { signal }: Pick = {}, ) { return this.rest.get(Routes.channelJoinedArchivedThreads(channelId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } diff --git a/packages/core/src/api/guild.ts b/packages/core/src/api/guild.ts index ab202176a..8b4161c67 100644 --- a/packages/core/src/api/guild.ts +++ b/packages/core/src/api/guild.ts @@ -1,5 +1,4 @@ -import { makeURLSearchParams } from '@discordjs/rest'; -import type { RawFile, REST } from '@discordjs/rest'; +import { makeURLSearchParams, type REST, type RawFile, type RequestData } from '@discordjs/rest'; import { Routes } from 'discord-api-types/v10'; import type { RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody, @@ -97,9 +96,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild} * @param guildId - The id of the guild + * @param options - The options for fetching the guild */ - public async get(guildId: string) { - return this.rest.get(Routes.guild(guildId)) as Promise; + public async get(guildId: string, { signal }: Pick) { + return this.rest.get(Routes.guild(guildId), { signal }) as Promise; } /** @@ -107,19 +107,23 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-preview} * @param guildId - The id of the guild to fetch the preview from + * @param options - The options for fetching the guild preview */ - public async getPreview(guildId: Snowflake) { - return this.rest.get(Routes.guildPreview(guildId)) as Promise; + public async getPreview(guildId: Snowflake, { signal }: Pick) { + return this.rest.get(Routes.guildPreview(guildId), { + signal, + }) as Promise; } /** * Creates a guild * * @see {@link https://discord.com/developers/docs/resources/guild#create-guild} - * @param data - The guild to create + * @param body - The guild to create + * @param options - The options for creating the guild */ - public async create(data: RESTPostAPIGuildsJSONBody) { - return this.rest.post(Routes.guilds(), { body: data }) as Promise; + public async create(body: RESTPostAPIGuildsJSONBody, { signal }: Pick) { + return this.rest.post(Routes.guilds(), { body, signal }) as Promise; } /** @@ -127,11 +131,19 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#modify-guild} * @param guildId - The id of the guild to edit - * @param data - The new guild data - * @param reason - The reason for editing this guild + * @param body - The new guild data + * @param options - The options for editing the guild */ - public async edit(guildId: Snowflake, data: RESTPatchAPIGuildJSONBody, reason?: string) { - return this.rest.patch(Routes.guild(guildId), { reason, body: data }) as Promise; + public async edit( + guildId: Snowflake, + body: RESTPatchAPIGuildJSONBody, + { reason, signal }: Pick = {}, + ) { + return this.rest.patch(Routes.guild(guildId), { + reason, + body, + signal, + }) as Promise; } /** @@ -139,10 +151,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#delete-guild} * @param guildId - The id of the guild to delete - * @param reason - The reason for deleting this guild + * @param options - The options for deleting this guild */ - public async delete(guildId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guild(guildId), { reason }); + public async delete(guildId: Snowflake, { signal, reason }: Pick = {}) { + await this.rest.delete(Routes.guild(guildId), { reason, signal }); } /** @@ -150,11 +162,17 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#list-guild-members} * @param guildId - The id of the guild - * @param options - The options to use when fetching the guild members + * @param query - The query to use when fetching the guild members + * @param options - The options for fetching the guild members */ - public async getMembers(guildId: Snowflake, options: RESTGetAPIGuildMembersQuery = {}) { + public async getMembers( + guildId: Snowflake, + query: RESTGetAPIGuildMembersQuery = {}, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.guildMembers(guildId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -163,9 +181,12 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-channels} * @param guildId - The id of the guild to fetch the channels from + * @param options - The options for fetching the guild channels */ - public async getChannels(guildId: Snowflake) { - return this.rest.get(Routes.guildChannels(guildId)) as Promise; + public async getChannels(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildChannels(guildId), { + signal, + }) as Promise; } /** @@ -173,13 +194,18 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#create-guild-channel} * @param guildId - The id of the guild to create the channel in - * @param data - The data to create the new channel - * @param reason - The reason for creating this channel + * @param body - The data to create the new channel + * @param options - The options for creating the guild channel */ - public async createChannel(guildId: Snowflake, data: RESTPostAPIGuildChannelJSONBody, reason?: string) { + public async createChannel( + guildId: Snowflake, + body: RESTPostAPIGuildChannelJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.post(Routes.guildChannels(guildId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -188,15 +214,15 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#modify-guild-channel-positions} * @param guildId - The id of the guild to edit the channel positions from - * @param data - The data to edit the channel positions with - * @param reason - The reason for editing the channel positions + * @param body - The data to edit the channel positions with + * @param options - The options for editing the guild channel positions */ public async setChannelPositions( guildId: Snowflake, - data: RESTPatchAPIGuildChannelPositionsJSONBody, - reason?: string, + body: RESTPatchAPIGuildChannelPositionsJSONBody, + { reason, signal }: Pick = {}, ) { - await this.rest.patch(Routes.guildChannels(guildId), { reason, body: data }); + await this.rest.patch(Routes.guildChannels(guildId), { reason, body, signal }); } /** @@ -204,9 +230,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#list-active-guild-threads} * @param guildId - The id of the guild to fetch the active threads from + * @param options - The options for fetching the active threads */ - public async getActiveThreads(guildId: Snowflake) { - return this.rest.get(Routes.guildActiveThreads(guildId)) as Promise; + public async getActiveThreads(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildActiveThreads(guildId), { signal }) as Promise; } /** @@ -214,9 +241,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-bans} * @param guildId - The id of the guild to fetch the ban from + * @param options - The options for fetching the guild member ban */ - public async getMemberBans(guildId: Snowflake) { - return this.rest.get(Routes.guildBans(guildId)) as Promise; + public async getMemberBans(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildBans(guildId), { signal }) as Promise; } /** @@ -225,16 +253,16 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#create-guild-ban} * @param guildId - The id of the guild to ban the member in * @param userId - The id of the user to ban - * @param options - Options for banning the user - * @param reason - The reason for banning the user + * @param body - The payload for banning the user + * @param options - The options for banning the user */ public async banUser( guildId: Snowflake, userId: Snowflake, - options: RESTPutAPIGuildBanJSONBody = {}, - reason?: string, + body: RESTPutAPIGuildBanJSONBody = {}, + { reason, signal }: Pick = {}, ) { - await this.rest.put(Routes.guildBan(guildId, userId), { reason, body: options }); + await this.rest.put(Routes.guildBan(guildId, userId), { reason, body, signal }); } /** @@ -243,10 +271,14 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#remove-guild-ban} * @param guildId - The id of the guild to unban the member in * @param userId - The id of the user to unban - * @param reason - The reason for unbanning the user + * @param options - The options for unbanning the user */ - public async unbanUser(guildId: Snowflake, userId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guildBan(guildId, userId), { reason }); + public async unbanUser( + guildId: Snowflake, + userId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.delete(Routes.guildBan(guildId, userId), { reason, signal }); } /** @@ -254,9 +286,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-roles} * @param guildId - The id of the guild to fetch the roles from + * @param options - The options for fetching the guild roles */ - public async getRoles(guildId: Snowflake) { - return this.rest.get(Routes.guildRoles(guildId)) as Promise; + public async getRoles(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildRoles(guildId), { signal }) as Promise; } /** @@ -264,11 +297,15 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#create-guild-role} * @param guildId - The id of the guild to create the role in - * @param data - The data to create the role with - * @param reason - The reason for creating the role + * @param body - The data to create the role with + * @param options - The options for creating the guild role */ - public async createRole(guildId: Snowflake, data: RESTPostAPIGuildRoleJSONBody, reason?: string) { - return this.rest.post(Routes.guildRoles(guildId), { reason, body: data }) as Promise; + public async createRole( + guildId: Snowflake, + body: RESTPostAPIGuildRoleJSONBody, + { reason, signal }: Pick = {}, + ) { + return this.rest.post(Routes.guildRoles(guildId), { reason, body, signal }) as Promise; } /** @@ -276,13 +313,18 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#modify-guild-role-positions} * @param guildId - The id of the guild to set role positions for - * @param data - The data for setting a role position - * @param reason - The reason for setting the role position + * @param body - The data for setting a role position + * @param options - The options for setting role positions */ - public async setRolePositions(guildId: Snowflake, data: RESTPatchAPIGuildRolePositionsJSONBody, reason?: string) { + public async setRolePositions( + guildId: Snowflake, + body: RESTPatchAPIGuildRolePositionsJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.patch(Routes.guildRoles(guildId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -292,13 +334,19 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#modify-guild-role} * @param guildId - The id of the guild to edit the role in * @param roleId - The id of the role to edit - * @param data - data for editing the role - * @param reason - The reason for editing the role + * @param body - data for editing the role + * @param options - The options for editing the guild role */ - public async editRole(guildId: Snowflake, roleId: Snowflake, data: RESTPatchAPIGuildRoleJSONBody, reason?: string) { + public async editRole( + guildId: Snowflake, + roleId: Snowflake, + body: RESTPatchAPIGuildRoleJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.patch(Routes.guildRole(guildId, roleId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -308,10 +356,14 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#delete-guild-role} * @param guildId - The id of the guild to delete the role in * @param roleId - The id of the role to delete - * @param reason - The reason for deleting the role + * @param options - The options for deleting the guild role */ - public async deleteRole(guildId: Snowflake, roleId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guildRole(guildId, roleId), { reason }); + public async deleteRole( + guildId: Snowflake, + roleId: Snowflake, + { reason, signal }: Pick, + ) { + await this.rest.delete(Routes.guildRole(guildId, roleId), { reason, signal }); } /** @@ -320,11 +372,16 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#modify-guild-mfa-level} * @param guildId - The id of the guild to edit the MFA level for * @param level - The new MFA level - * @param reason - The reason for editing the MFA level + * @param options - The options for editing the MFA level */ - public async editMFALevel(guildId: Snowflake, level: GuildMFALevel, reason?: string) { + public async editMFALevel( + guildId: Snowflake, + level: GuildMFALevel, + { reason, signal }: Pick = {}, + ) { return this.rest.post(Routes.guildMFA(guildId), { reason, + signal, body: { mfa_level: level }, }) as Promise; } @@ -334,11 +391,17 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-prune-count} * @param guildId - The id of the guild to fetch the number of pruned members from + * @param query - The query options for fetching the number of pruned members * @param options - The options for fetching the number of pruned members */ - public async getPruneCount(guildId: Snowflake, options: RESTGetAPIGuildPruneCountQuery = {}) { + public async getPruneCount( + guildId: Snowflake, + query: RESTGetAPIGuildPruneCountQuery = {}, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.guildPrune(guildId), { - query: makeURLSearchParams(options), + signal, + query: makeURLSearchParams(query), }) as Promise; } @@ -347,13 +410,18 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#begin-guild-prune} * @param guildId - The id of the guild to prune members in - * @param options - The options for pruning members - * @param reason - The reason for pruning members + * @param body - The options for pruning members + * @param options - The options for initiating the prune */ - public async beginPrune(guildId: Snowflake, options: RESTPostAPIGuildPruneJSONBody = {}, reason?: string) { + public async beginPrune( + guildId: Snowflake, + body: RESTPostAPIGuildPruneJSONBody = {}, + { reason, signal }: Pick = {}, + ) { return this.rest.post(Routes.guildPrune(guildId), { - body: options, + body, reason, + signal, }) as Promise; } @@ -362,9 +430,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-voice-regions} * @param guildId - The id of the guild to fetch the voice regions from + * @param options - The options for fetching the voice regions */ - public async getVoiceRegions(guildId: Snowflake) { - return this.rest.get(Routes.guildVoiceRegions(guildId)) as Promise; + public async getVoiceRegions(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildVoiceRegions(guildId), { signal }) as Promise; } /** @@ -372,9 +441,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-invites} * @param guildId - The id of the guild to fetch the invites from + * @param options - The options for fetching the invites */ - public async getInvites(guildId: Snowflake) { - return this.rest.get(Routes.guildInvites(guildId)) as Promise; + public async getInvites(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildInvites(guildId), { signal }) as Promise; } /** @@ -382,9 +452,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-integrations} * @param guildId - The id of the guild to fetch the integrations from + * @param options - The options for fetching the integrations */ - public async getIntegrations(guildId: Snowflake) { - return this.rest.get(Routes.guildIntegrations(guildId)) as Promise; + public async getIntegrations(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildIntegrations(guildId), { signal }) as Promise; } /** @@ -393,10 +464,14 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#delete-guild-integration} * @param guildId - The id of the guild to delete the integration from * @param integrationId - The id of the integration to delete - * @param reason - The reason for deleting the integration + * @param options - The options for deleting the integration */ - public async deleteIntegration(guildId: Snowflake, integrationId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guildIntegration(guildId, integrationId), { reason }); + public async deleteIntegration( + guildId: Snowflake, + integrationId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.delete(Routes.guildIntegration(guildId, integrationId), { reason, signal }); } /** @@ -404,9 +479,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-widget-settings} * @param guildId - The id of the guild to fetch the widget settings from + * @param options - The options for fetching the widget settings */ - public async getWidgetSettings(guildId: Snowflake) { - return this.rest.get(Routes.guildWidgetSettings(guildId)) as Promise; + public async getWidgetSettings(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildWidgetSettings(guildId), { signal }) as Promise; } /** @@ -414,13 +490,18 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#modify-guild-widget} * @param guildId - The id of the guild to edit the widget settings from - * @param data - The new widget settings data - * @param reason - The reason for editing the widget settings + * @param body - The new widget settings data + * @param options - The options for editing the widget settings */ - public async editWidgetSettings(guildId: Snowflake, data: RESTPatchAPIGuildWidgetSettingsJSONBody, reason?: string) { + public async editWidgetSettings( + guildId: Snowflake, + body: RESTPatchAPIGuildWidgetSettingsJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.patch(Routes.guildWidgetSettings(guildId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -429,9 +510,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-widget} * @param guildId - The id of the guild to fetch the widget from + * @param options - The options for fetching the widget */ - public async getWidget(guildId: Snowflake) { - return this.rest.get(Routes.guildWidgetJSON(guildId)) as Promise; + public async getWidget(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildWidgetJSON(guildId), { signal }) as Promise; } /** @@ -439,9 +521,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-vanity-url} * @param guildId - The id of the guild to fetch the vanity url from + * @param options - The options for fetching the vanity url */ - public async getVanityURL(guildId: Snowflake) { - return this.rest.get(Routes.guildVanityUrl(guildId)) as Promise; + public async getVanityURL(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildVanityUrl(guildId), { signal }) as Promise; } /** @@ -450,10 +533,16 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-widget-image} * @param guildId - The id of the guild to fetch the widget image from * @param style - The style of the widget image + * @param options - The options for fetching the widget image */ - public async getWidgetImage(guildId: Snowflake, style?: GuildWidgetStyle) { + public async getWidgetImage( + guildId: Snowflake, + style?: GuildWidgetStyle, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.guildWidgetImage(guildId), { query: makeURLSearchParams({ style }), + signal, }) as Promise; } @@ -462,9 +551,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-welcome-screen} * @param guildId - The id of the guild to fetch the welcome screen from + * @param options - The options for fetching the welcome screen */ - public async getWelcomeScreen(guildId: Snowflake) { - return this.rest.get(Routes.guildWelcomeScreen(guildId)) as Promise; + public async getWelcomeScreen(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildWelcomeScreen(guildId), { signal }) as Promise; } /** @@ -472,13 +562,18 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#modify-guild-welcome-screen} * @param guildId - The id of the guild to edit the welcome screen for - * @param data - The new welcome screen data - * @param reason - The reason for editing the welcome screen + * @param body - The new welcome screen data + * @param options - The options for editing the welcome screen */ - public async editWelcomeScreen(guildId: Snowflake, data?: RESTPatchAPIGuildWelcomeScreenJSONBody, reason?: string) { + public async editWelcomeScreen( + guildId: Snowflake, + body?: RESTPatchAPIGuildWelcomeScreenJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.patch(Routes.guildWelcomeScreen(guildId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -488,16 +583,16 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#modify-user-voice-state} * @param guildId - The id of the guild to edit the current user's voice state in * @param userId - The id of the user to edit the voice state for - * @param data - The data for editing the voice state - * @param reason - The reason for editing the voice state + * @param body - The data for editing the voice state + * @param options - The options for editing the voice state */ public async editUserVoiceState( guildId: Snowflake, userId: Snowflake, - data: RESTPatchAPIGuildVoiceStateUserJSONBody, - reason?: string, + body: RESTPatchAPIGuildVoiceStateUserJSONBody, + { reason, signal }: Pick = {}, ) { - await this.rest.patch(Routes.guildVoiceState(guildId, userId), { reason, body: data }); + await this.rest.patch(Routes.guildVoiceState(guildId, userId), { reason, body, signal }); } /** @@ -505,9 +600,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/emoji#list-guild-emojis} * @param guildId - The id of the guild to fetch the emojis from + * @param options - The options for fetching the emojis */ - public async getEmojis(guildId: Snowflake) { - return this.rest.get(Routes.guildEmojis(guildId)) as Promise; + public async getEmojis(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildEmojis(guildId), { signal }) as Promise; } /** @@ -516,9 +612,10 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/emoji#get-guild-emoji} * @param guildId - The id of the guild to fetch the emoji from * @param emojiId - The id of the emoji to fetch + * @param options - The options for fetching the emoji */ - public async getEmoji(guildId: Snowflake, emojiId: Snowflake) { - return this.rest.get(Routes.guildEmoji(guildId, emojiId)) as Promise; + public async getEmoji(guildId: Snowflake, emojiId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildEmoji(guildId, emojiId), { signal }) as Promise; } /** @@ -526,13 +623,18 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/emoji#create-guild-emoji} * @param guildId - The id of the guild to create the emoji from - * @param data - The data for creating the emoji - * @param reason - The reason for creating the emoji + * @param body - The data for creating the emoji + * @param options - The options for creating the emoji */ - public async createEmoji(guildId: Snowflake, data: RESTPostAPIGuildEmojiJSONBody, reason?: string) { + public async createEmoji( + guildId: Snowflake, + body: RESTPostAPIGuildEmojiJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.post(Routes.guildEmojis(guildId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -542,18 +644,19 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/emoji#modify-guild-emoji} * @param guildId - The id of the guild to edit the emoji from * @param emojiId - The id of the emoji to edit - * @param data - The data for editing the emoji - * @param reason - The reason for editing the emoji + * @param body - The data for editing the emoji + * @param options - The options for editing the emoji */ public async editEmoji( guildId: Snowflake, emojiId: Snowflake, - data: RESTPatchAPIGuildEmojiJSONBody, - reason?: string, + body: RESTPatchAPIGuildEmojiJSONBody, + { reason, signal }: Pick = {}, ) { return this.rest.patch(Routes.guildEmoji(guildId, emojiId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -563,10 +666,14 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/emoji#delete-guild-emoji} * @param guildId - The id of the guild to delete the emoji from * @param emojiId - The id of the emoji to delete - * @param reason - The reason for deleting the emoji + * @param options - The options for deleting the emoji */ - public async deleteEmoji(guildId: Snowflake, emojiId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guildEmoji(guildId, emojiId), { reason }); + public async deleteEmoji( + guildId: Snowflake, + emojiId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.delete(Routes.guildEmoji(guildId, emojiId), { reason, signal }); } /** @@ -574,11 +681,17 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild-scheduled-event#list-scheduled-events-for-guild} * @param guildId - The id of the guild to fetch the scheduled events from + * @param query - The query options for fetching the scheduled events * @param options - The options for fetching the scheduled events */ - public async getScheduledEvents(guildId: Snowflake, options: RESTGetAPIGuildScheduledEventsQuery = {}) { + public async getScheduledEvents( + guildId: Snowflake, + query: RESTGetAPIGuildScheduledEventsQuery = {}, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.guildScheduledEvents(guildId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -587,13 +700,18 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild-scheduled-event#create-guild-scheduled-event} * @param guildId - The id of the guild to create the scheduled event from - * @param data - The data to create the event with - * @param reason - The reason for creating the scheduled event + * @param body - The data to create the event with + * @param options - The options for creating the scheduled event */ - public async createScheduledEvent(guildId: Snowflake, data: RESTPostAPIGuildScheduledEventJSONBody, reason?: string) { + public async createScheduledEvent( + guildId: Snowflake, + body: RESTPostAPIGuildScheduledEventJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.post(Routes.guildScheduledEvents(guildId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -603,15 +721,18 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild-scheduled-event#get-guild-scheduled-event} * @param guildId - The id of the guild to fetch the scheduled event from * @param eventId - The id of the scheduled event to fetch + * @param query - The options for fetching the scheduled event * @param options - The options for fetching the scheduled event */ public async getScheduledEvent( guildId: Snowflake, eventId: Snowflake, - options: RESTGetAPIGuildScheduledEventQuery = {}, + query: RESTGetAPIGuildScheduledEventQuery = {}, + { signal }: Pick = {}, ) { return this.rest.get(Routes.guildScheduledEvent(guildId, eventId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -621,18 +742,19 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild-scheduled-event#modify-guild-scheduled-event} * @param guildId - The id of the guild to edit the scheduled event from * @param eventId - The id of the scheduled event to edit - * @param data - The new event data - * @param reason - The reason for editing the scheduled event + * @param body - The new event data + * @param options - The options for editing the scheduled event */ public async editScheduledEvent( guildId: Snowflake, eventId: Snowflake, - data: RESTPatchAPIGuildScheduledEventJSONBody, - reason?: string, + body: RESTPatchAPIGuildScheduledEventJSONBody, + { reason, signal }: Pick = {}, ) { return this.rest.patch(Routes.guildScheduledEvent(guildId, eventId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -642,10 +764,14 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild-scheduled-event#delete-guild-scheduled-event} * @param guildId - The id of the guild to delete the scheduled event from * @param eventId - The id of the scheduled event to delete - * @param reason - The reason for deleting the scheduled event + * @param options - The options for deleting the scheduled event */ - public async deleteScheduledEvent(guildId: Snowflake, eventId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guildScheduledEvent(guildId, eventId), { reason }); + public async deleteScheduledEvent( + guildId: Snowflake, + eventId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.delete(Routes.guildScheduledEvent(guildId, eventId), { reason, signal }); } /** @@ -654,15 +780,18 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild-scheduled-event#get-guild-scheduled-event-users} * @param guildId - The id of the guild to fetch the scheduled event users from * @param eventId - The id of the scheduled event to fetch the users for + * @param query - The options for fetching the scheduled event users * @param options - The options for fetching the scheduled event users */ public async getScheduledEventUsers( guildId: Snowflake, eventId: Snowflake, - options: RESTGetAPIGuildScheduledEventUsersQuery = {}, + query: RESTGetAPIGuildScheduledEventUsersQuery = {}, + { signal }: Pick = {}, ) { return this.rest.get(Routes.guildScheduledEventUsers(guildId, eventId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -671,9 +800,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild-template#get-guild-templates} * @param guildId - The id of the guild to fetch the templates from + * @param options - The options for fetching the templates */ - public async getTemplates(guildId: Snowflake) { - return this.rest.get(Routes.guildTemplates(guildId)) as Promise; + public async getTemplates(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildTemplates(guildId), { signal }) as Promise; } /** @@ -682,9 +812,12 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild-template#sync-guild-template} * @param guildId - The id of the guild to sync the template from * @param templateCode - The code of the template to sync + * @param options - The options for syncing the template */ - public async syncTemplate(guildId: Snowflake, templateCode: string) { - return this.rest.put(Routes.guildTemplate(guildId, templateCode)) as Promise; + public async syncTemplate(guildId: Snowflake, templateCode: string, { signal }: Pick = {}) { + return this.rest.put(Routes.guildTemplate(guildId, templateCode), { + signal, + }) as Promise; } /** @@ -693,11 +826,18 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild-template#modify-guild-template} * @param guildId - The id of the guild to edit the template from * @param templateCode - The code of the template to edit - * @param data - The data for editing the template + * @param body - The data for editing the template + * @param options - The options for editing the template */ - public async editTemplate(guildId: Snowflake, templateCode: string, data: RESTPatchAPIGuildTemplateJSONBody) { + public async editTemplate( + guildId: Snowflake, + templateCode: string, + body: RESTPatchAPIGuildTemplateJSONBody, + { signal }: Pick = {}, + ) { return this.rest.patch(Routes.guildTemplate(guildId, templateCode), { - body: data, + body, + signal, }) as Promise; } @@ -707,9 +847,10 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild-template#delete-guild-template} * @param guildId - The id of the guild to delete the template from * @param templateCode - The code of the template to delete + * @param options - The options for deleting the template */ - public async deleteTemplate(guildId: Snowflake, templateCode: string) { - await this.rest.delete(Routes.guildTemplate(guildId, templateCode)); + public async deleteTemplate(guildId: Snowflake, templateCode: string, { signal }: Pick = {}) { + await this.rest.delete(Routes.guildTemplate(guildId, templateCode), { signal }); } /** @@ -717,9 +858,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/sticker#list-guild-stickers} * @param guildId - The id of the guild to fetch the stickers from + * @param options - The options for fetching the stickers */ - public async getStickers(guildId: Snowflake) { - return this.rest.get(Routes.guildStickers(guildId)) as Promise; + public async getStickers(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildStickers(guildId), { signal }) as Promise; } /** @@ -728,9 +870,10 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/sticker#get-guild-sticker} * @param guildId - The id of the guild to fetch the sticker from * @param stickerId - The id of the sticker to fetch + * @param options - The options for fetching the sticker */ - public async getSticker(guildId: Snowflake, stickerId: Snowflake) { - return this.rest.get(Routes.guildSticker(guildId, stickerId)) as Promise; + public async getSticker(guildId: Snowflake, stickerId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildSticker(guildId, stickerId), { signal }) as Promise; } /** @@ -738,13 +881,13 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/sticker#create-guild-sticker} * @param guildId - The id of the guild to create the sticker for - * @param data - The data for creating the sticker - * @param reason - The reason for creating the sticker + * @param body - The data for creating the sticker + * @param options - The options for creating the sticker */ public async createSticker( guildId: Snowflake, { file, ...body }: Omit & { file: RawFile }, - reason?: string, + { reason, signal }: Pick = {}, ) { const fileData = { ...file, key: 'file' }; @@ -753,6 +896,7 @@ export class GuildsAPI { body, files: [fileData], reason, + signal, }) as Promise; } @@ -762,18 +906,19 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/sticker#modify-guild-sticker} * @param guildId - The id of the guild to edit the sticker from * @param stickerId - The id of the sticker to edit - * @param data - The data for editing the sticker - * @param reason - The reason for editing the sticker + * @param body - The data for editing the sticker + * @param options - The options for editing the sticker */ public async editSticker( guildId: Snowflake, stickerId: Snowflake, - data: RESTPatchAPIGuildStickerJSONBody, - reason?: string, + body: RESTPatchAPIGuildStickerJSONBody, + { reason, signal }: Pick = {}, ) { return this.rest.patch(Routes.guildSticker(guildId, stickerId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -783,10 +928,14 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/sticker#delete-guild-sticker} * @param guildId - The id of the guild to delete the sticker from * @param stickerId - The id of the sticker to delete - * @param reason - The reason for deleting the sticker + * @param options - The options for deleting the sticker */ - public async deleteSticker(guildId: Snowflake, stickerId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guildSticker(guildId, stickerId), { reason }); + public async deleteSticker( + guildId: Snowflake, + stickerId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.delete(Routes.guildSticker(guildId, stickerId), { reason, signal }); } /** @@ -794,11 +943,17 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log} * @param guildId - The id of the guild to fetch the audit logs from + * @param query - The query options for fetching the audit logs * @param options - The options for fetching the audit logs */ - public async getAuditLogs(guildId: Snowflake, options: RESTGetAPIAuditLogQuery = {}) { + public async getAuditLogs( + guildId: Snowflake, + query: RESTGetAPIAuditLogQuery = {}, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.guildAuditLog(guildId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -807,9 +962,12 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/auto-moderation#list-auto-moderation-rules-for-guild} * @param guildId - The id of the guild to fetch the auto moderation rules from + * @param options - The options for fetching the auto moderation rules */ - public async getAutoModerationRules(guildId: Snowflake) { - return this.rest.get(Routes.guildAutoModerationRules(guildId)) as Promise; + public async getAutoModerationRules(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildAutoModerationRules(guildId), { + signal, + }) as Promise; } /** @@ -818,11 +976,16 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/auto-moderation#get-auto-moderation-rule} * @param guildId - The id of the guild to fetch the auto moderation rule from * @param ruleId - The id of the auto moderation rule to fetch + * @param options - The options for fetching the auto moderation rule */ - public async getAutoModerationRule(guildId: Snowflake, ruleId: Snowflake) { - return this.rest.get( - Routes.guildAutoModerationRule(guildId, ruleId), - ) as Promise; + public async getAutoModerationRule( + guildId: Snowflake, + ruleId: Snowflake, + { signal }: Pick = {}, + ) { + return this.rest.get(Routes.guildAutoModerationRule(guildId, ruleId), { + signal, + }) as Promise; } /** @@ -830,16 +993,18 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/auto-moderation#create-auto-moderation-rule} * @param guildId - The id of the guild to create the auto moderation rule from - * @param data - The data for creating the auto moderation rule + * @param body - The data for creating the auto moderation rule + * @param options - The options for creating the auto moderation rule */ public async createAutoModerationRule( guildId: Snowflake, - data: RESTPostAPIAutoModerationRuleJSONBody, - reason?: string, + body: RESTPostAPIAutoModerationRuleJSONBody, + { reason, signal }: Pick = {}, ) { return this.rest.post(Routes.guildAutoModerationRules(guildId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -849,18 +1014,19 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/auto-moderation#modify-auto-moderation-rule} * @param guildId - The id of the guild to edit the auto moderation rule from * @param ruleId - The id of the auto moderation rule to edit - * @param data - The data for editing the auto moderation rule - * @param reason - The reason for editing the auto moderation rule + * @param body - The data for editing the auto moderation rule + * @param options - The options for editing the auto moderation rule */ public async editAutoModerationRule( guildId: Snowflake, ruleId: Snowflake, - data: RESTPatchAPIAutoModerationRuleJSONBody, - reason?: string, + body: RESTPatchAPIAutoModerationRuleJSONBody, + { reason, signal }: Pick = {}, ) { return this.rest.patch(Routes.guildAutoModerationRule(guildId, ruleId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -869,11 +1035,14 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/auto-moderation#delete-auto-moderation-rule} * @param guildId - The id of the guild to delete the auto moderation rule from - * @param ruleId - The id of the auto moderation rule to delete - * @param reason - The reason for deleting the auto moderation rule + * @param options - The options for deleting the auto moderation rule */ - public async deleteAutoModerationRule(guildId: Snowflake, ruleId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guildAutoModerationRule(guildId, ruleId), { reason }); + public async deleteAutoModerationRule( + guildId: Snowflake, + ruleId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.delete(Routes.guildAutoModerationRule(guildId, ruleId), { reason, signal }); } /** @@ -882,9 +1051,10 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#get-guild-member} * @param guildId - The id of the guild * @param userId - The id of the user + * @param options - The options for fetching the guild member */ - public async getMember(guildId: Snowflake, userId: Snowflake) { - return this.rest.get(Routes.guildMember(guildId, userId)) as Promise; + public async getMember(guildId: Snowflake, userId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.guildMember(guildId, userId), { signal }) as Promise; } /** @@ -893,11 +1063,16 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#search-guild-members} * @param guildId - The id of the guild to search in * @param query - The query to search for - * @param limit - The maximum number of members to return + * @param options - The options for searching for guild members */ - public async searchForMembers(guildId: Snowflake, options: RESTGetAPIGuildMembersSearchQuery) { + public async searchForMembers( + guildId: Snowflake, + query: RESTGetAPIGuildMembersSearchQuery, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.guildMembersSearch(guildId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -907,18 +1082,19 @@ export class GuildsAPI { * @see {@link https://discord.com/developers/docs/resources/guild#modify-guild-member} * @param guildId - The id of the guild * @param userId - The id of the user - * @param data - The data to use when editing the guild member - * @param reason - The reason for editing this guild member + * @param body - The data to use when editing the guild member + * @param options - The options for editing the guild member */ public async editMember( guildId: Snowflake, userId: Snowflake, - data: RESTPatchAPIGuildMemberJSONBody = {}, - reason?: string, + body: RESTPatchAPIGuildMemberJSONBody = {}, + { reason, signal }: Pick = {}, ) { return this.rest.patch(Routes.guildMember(guildId, userId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -929,10 +1105,15 @@ export class GuildsAPI { * @param guildId - The id of the guild * @param userId - The id of the user * @param roleId - The id of the role - * @param reason - The reason for adding this role to the guild member + * @param options - The options for adding a role to a guild member */ - public async addRoleToMember(guildId: Snowflake, userId: Snowflake, roleId: Snowflake, reason?: string) { - await this.rest.put(Routes.guildMemberRole(guildId, userId, roleId), { reason }); + public async addRoleToMember( + guildId: Snowflake, + userId: Snowflake, + roleId: Snowflake, + { reason, signal }: Pick = {}, + ) { + await this.rest.put(Routes.guildMemberRole(guildId, userId, roleId), { reason, signal }); } /** @@ -942,10 +1123,15 @@ export class GuildsAPI { * @param guildId - The id of the guild * @param userId - The id of the user * @param roleId - The id of the role - * @param reason - The reason for removing this role from the guild member + * @param options - The options for removing a role from a guild member */ - public async removeRoleFromMember(guildId: Snowflake, userId: Snowflake, roleId: Snowflake, reason?: string) { - await this.rest.delete(Routes.guildMemberRole(guildId, userId, roleId), { reason }); + public async removeRoleFromMember( + guildId: Snowflake, + userId: Snowflake, + roleId: Snowflake, + { reason, signal }: Pick, + ) { + await this.rest.delete(Routes.guildMemberRole(guildId, userId, roleId), { reason, signal }); } /** @@ -953,9 +1139,10 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild-template#get-guild-template} * @param templateCode - The code of the template + * @param options - The options for fetching the guild template */ - public async getTemplate(templateCode: string) { - return this.rest.get(Routes.template(templateCode)) as Promise; + public async getTemplate(templateCode: string, { signal }: Pick = {}) { + return this.rest.get(Routes.template(templateCode), { signal }) as Promise; } /** @@ -963,10 +1150,15 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild-template#create-guild-template} * @param templateCode - The code of the template - * @param data - The data to use when creating the template + * @param body - The data to use when creating the template + * @param options - The options for creating the template */ - public async createTemplate(templateCode: string, data: RESTPostAPITemplateCreateGuildJSONBody) { - return this.rest.post(Routes.template(templateCode), { body: data }) as Promise; + public async createTemplate( + templateCode: string, + body: RESTPostAPITemplateCreateGuildJSONBody, + { signal }: Pick = {}, + ) { + return this.rest.post(Routes.template(templateCode), { body, signal }) as Promise; } /** @@ -984,11 +1176,11 @@ export class GuildsAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#modify-current-user-voice-state} * @param guildId - The id of the guild - * @param options - The options to use when setting the voice state + * @param body - The options to use when setting the voice state */ - public async setVoiceState(guildId: Snowflake, options: RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody = {}) { + public async setVoiceState(guildId: Snowflake, body: RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody = {}) { return this.rest.patch(Routes.guildVoiceState(guildId, '@me'), { - body: options, + body, }) as Promise; } } diff --git a/packages/core/src/api/interactions.ts b/packages/core/src/api/interactions.ts index c28ba13c6..5b6205163 100644 --- a/packages/core/src/api/interactions.ts +++ b/packages/core/src/api/interactions.ts @@ -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 = {}, ) { 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 = {}) { 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 = {}, + ) { 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 = {}, ) { - 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 = {}, ) { - 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, + ) { return this.webhooks.getMessage( applicationId, interactionToken, '@original', + {}, + { signal }, ) as Promise; } @@ -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 = {}, + ) { + 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 = {}, ) { 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 = {}, ) { 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 = {}, ) { await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), { auth: false, body: { type: InteractionResponseType.Modal, - data, + data: callbackData, }, + signal, }); } } diff --git a/packages/core/src/api/invite.ts b/packages/core/src/api/invite.ts index c7b856d41..14438286e 100644 --- a/packages/core/src/api/invite.ts +++ b/packages/core/src/api/invite.ts @@ -1,4 +1,4 @@ -import { makeURLSearchParams, type REST } from '@discordjs/rest'; +import { makeURLSearchParams, type RequestData, type REST } from '@discordjs/rest'; import { Routes, type RESTGetAPIInviteQuery, type RESTGetAPIInviteResult } from 'discord-api-types/v10'; export class InvitesAPI { @@ -9,10 +9,13 @@ export class InvitesAPI { * * @see {@link https://discord.com/developers/docs/resources/invite#get-invite} * @param code - The invite code + * @param query - The options to use when fetching the invite + * @param options - The options to use when fetching the invite */ - public async get(code: string, options: RESTGetAPIInviteQuery = {}) { + public async get(code: string, query: RESTGetAPIInviteQuery = {}, { signal }: Pick = {}) { return this.rest.get(Routes.invite(code), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -21,9 +24,9 @@ export class InvitesAPI { * * @see {@link https://discord.com/developers/docs/resources/invite#delete-invite} * @param code - The invite code - * @param reason - The reason for deleting the invite + * @param options - The options to use when deleting the invite */ - public async delete(code: string, reason?: string) { - await this.rest.delete(Routes.invite(code), { reason }); + public async delete(code: string, { reason, signal }: Pick = {}) { + await this.rest.delete(Routes.invite(code), { reason, signal }); } } diff --git a/packages/core/src/api/oauth2.ts b/packages/core/src/api/oauth2.ts index 17a8e9b94..4fcd311d9 100644 --- a/packages/core/src/api/oauth2.ts +++ b/packages/core/src/api/oauth2.ts @@ -1,6 +1,5 @@ import { URL } from 'node:url'; -import type { REST } from '@discordjs/rest'; -import { makeURLSearchParams } from '@discordjs/rest'; +import { type RequestData, type REST, makeURLSearchParams } from '@discordjs/rest'; import { Routes, RouteBases, @@ -34,15 +33,20 @@ export class OAuth2API { * Performs an OAuth2 token exchange, giving you an access token * * @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-access-token-exchange-example} + * @param body - The body of the token exchange request * @param options - The options for the token exchange request */ - public async tokenExchange(options: RESTPostOAuth2AccessTokenURLEncodedData) { + public async tokenExchange( + body: RESTPostOAuth2AccessTokenURLEncodedData, + { signal }: Pick = {}, + ) { return this.rest.post(Routes.oauth2TokenExchange(), { - body: makeURLSearchParams(options), + body: makeURLSearchParams(body), passThroughBody: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, + signal, }) as Promise; } @@ -50,15 +54,20 @@ export class OAuth2API { * Refreshes an OAuth2 access token, giving you a new one * * @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-refresh-token-exchange-example} + * @param body - The options for the refresh token request * @param options - The options for the refresh token request */ - public async refreshToken(options: RESTPostOAuth2RefreshTokenURLEncodedData) { + public async refreshToken( + body: RESTPostOAuth2RefreshTokenURLEncodedData, + { signal }: Pick = {}, + ) { return this.rest.post(Routes.oauth2TokenExchange(), { - body: makeURLSearchParams(options), + body: makeURLSearchParams(body), passThroughBody: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, + signal, }) as Promise; } @@ -68,15 +77,20 @@ export class OAuth2API { * @remarks * This is primarily used for testing purposes * @see {@link https://discord.com/developers/docs/topics/oauth2#client-credentials-grant} + * @param body - The options for the client credentials grant request * @param options - The options for the client credentials grant request */ - public async getToken(options: RESTPostOAuth2ClientCredentialsURLEncodedData) { + public async getToken( + body: RESTPostOAuth2ClientCredentialsURLEncodedData, + { signal }: Pick = {}, + ) { return this.rest.post(Routes.oauth2TokenExchange(), { - body: makeURLSearchParams(options), + body: makeURLSearchParams(body), passThroughBody: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, + signal, }) as Promise; } @@ -84,17 +98,23 @@ export class OAuth2API { * Fetches the current bot's application information * * @see {@link https://discord.com/developers/docs/topics/oauth2#get-current-bot-application-information} + * @param options - The options for the current bot application information request */ - public async getCurrentBotApplicationInformation() { - return this.rest.get(Routes.oauth2CurrentApplication()) as Promise; + public async getCurrentBotApplicationInformation({ signal }: Pick = {}) { + return this.rest.get(Routes.oauth2CurrentApplication(), { + signal, + }) as Promise; } /** * Fetches the current authorization information * * @see {@link https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information} + * @param options - The options for the current authorization information request */ - public async getCurrentAuthorizationInformation() { - return this.rest.get(Routes.oauth2CurrentAuthorization()) as Promise; + public async getCurrentAuthorizationInformation({ signal }: Pick = {}) { + return this.rest.get(Routes.oauth2CurrentAuthorization(), { + signal, + }) as Promise; } } diff --git a/packages/core/src/api/roleConnections.ts b/packages/core/src/api/roleConnections.ts index e81981da4..d9a4c76ab 100644 --- a/packages/core/src/api/roleConnections.ts +++ b/packages/core/src/api/roleConnections.ts @@ -1,4 +1,4 @@ -import type { REST } from '@discordjs/rest'; +import type { RequestData, REST } from '@discordjs/rest'; import { Routes, type RESTGetAPIApplicationRoleConnectionMetadataResult, @@ -15,11 +15,12 @@ export class RoleConnectionsAPI { * * @see {@link https://discord.com/developers/docs/resources/application-role-connection-metadata#get-application-role-connection-metadata-records} * @param applicationId - The id of the application to get role connection metadata records for + * @param options - The options to use when fetching the role connection metadata records */ - public async getMetadataRecords(applicationId: Snowflake) { - return this.rest.get( - Routes.applicationRoleConnectionMetadata(applicationId), - ) as Promise; + public async getMetadataRecords(applicationId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.applicationRoleConnectionMetadata(applicationId), { + signal, + }) as Promise; } /** @@ -27,14 +28,17 @@ export class RoleConnectionsAPI { * * @see {@link https://discord.com/developers/docs/resources/application-role-connection-metadata#update-application-role-connection-metadata-records} * @param applicationId - The id of the application to update role connection metadata records for - * @param options - The new role connection metadata records + * @param body - The new role connection metadata records + * @param options - The options to use when updating the role connection metadata records */ public async updateMetadataRecords( applicationId: Snowflake, - options: RESTPutAPIApplicationCommandPermissionsJSONBody, + body: RESTPutAPIApplicationCommandPermissionsJSONBody, + { signal }: Pick = {}, ) { return this.rest.put(Routes.applicationRoleConnectionMetadata(applicationId), { - body: options, + body, + signal, }) as Promise; } } diff --git a/packages/core/src/api/sticker.ts b/packages/core/src/api/sticker.ts index 94e580a67..4d56a5a21 100644 --- a/packages/core/src/api/sticker.ts +++ b/packages/core/src/api/sticker.ts @@ -1,4 +1,4 @@ -import type { REST } from '@discordjs/rest'; +import type { RequestData, REST } from '@discordjs/rest'; import { Routes, type RESTGetAPIStickerResult, @@ -13,9 +13,10 @@ export class StickersAPI { * Fetches all of the nitro sticker packs * * @see {@link https://discord.com/developers/docs/resources/sticker#list-nitro-sticker-packs} + * @param options - The options to use when fetching the sticker packs */ - public async getNitroStickers() { - return this.rest.get(Routes.nitroStickerPacks()) as Promise; + public async getNitroStickers({ signal }: Pick = {}) { + return this.rest.get(Routes.nitroStickerPacks(), { signal }) as Promise; } /** @@ -23,8 +24,9 @@ export class StickersAPI { * * @see {@link https://discord.com/developers/docs/resources/sticker#get-sticker} * @param stickerId - The id of the sticker + * @param options - The options to use when fetching the sticker */ - public async get(stickerId: Snowflake) { - return this.rest.get(Routes.sticker(stickerId)) as Promise; + public async get(stickerId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.sticker(stickerId), { signal }) as Promise; } } diff --git a/packages/core/src/api/thread.ts b/packages/core/src/api/thread.ts index 78a2e591c..2bcf73978 100644 --- a/packages/core/src/api/thread.ts +++ b/packages/core/src/api/thread.ts @@ -1,4 +1,4 @@ -import type { RawFile, REST } from '@discordjs/rest'; +import type { RawFile, RequestData, REST } from '@discordjs/rest'; import { Routes, type APIThreadChannel, @@ -10,10 +10,6 @@ import { type Snowflake, } from 'discord-api-types/v10'; -export interface StartThreadOptions extends RESTPostAPIChannelThreadsJSONBody { - message_id?: string; -} - export interface StartForumThreadOptions extends RESTPostAPIGuildForumThreadsJSONBody { message: RESTPostAPIGuildForumThreadsJSONBody['message'] & { files?: RawFile[] }; } @@ -26,9 +22,10 @@ export class ThreadsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#get-channel} * @param threadId - The id of the thread + * @param options - The options to use when fetching the thread */ - public async get(threadId: Snowflake) { - return this.rest.get(Routes.channel(threadId)) as Promise; + public async get(threadId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.channel(threadId), { signal }) as Promise; } /** @@ -37,10 +34,20 @@ export class ThreadsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#start-thread-from-message} * @see {@link https://discord.com/developers/docs/resources/channel#start-thread-without-message} * @param channelId - The id of the channel to start the thread in - * @param data - The data to use when starting the thread + * @param body - The data to use when starting the thread + * @param messageId - The id of the message to start the thread from + * @param options - The options to use when starting the thread */ - public async create(channelId: Snowflake, { message_id, ...body }: StartThreadOptions) { - return this.rest.post(Routes.threads(channelId, message_id), { body }) as Promise; + public async create( + channelId: Snowflake, + body: RESTPostAPIChannelThreadsJSONBody, + messageId?: Snowflake, + { signal }: Pick = {}, + ) { + return this.rest.post(Routes.threads(channelId, messageId), { + body, + signal, + }) as Promise; } /** @@ -48,9 +55,14 @@ export class ThreadsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#start-thread-in-forum-channel} * @param channelId - The id of the forum channel to start the thread in - * @param data - The data to use when starting the thread + * @param body - The data to use when starting the thread + * @param options - The options to use when starting the thread */ - public async createForumThread(channelId: Snowflake, { message, ...optionsBody }: StartForumThreadOptions) { + public async createForumThread( + channelId: Snowflake, + { message, ...optionsBody }: StartForumThreadOptions, + { signal }: Pick = {}, + ) { const { files, ...messageBody } = message; const body = { @@ -58,7 +70,7 @@ export class ThreadsAPI { message: messageBody, }; - return this.rest.post(Routes.threads(channelId), { files, body }) as Promise; + return this.rest.post(Routes.threads(channelId), { files, body, signal }) as Promise; } /** @@ -66,9 +78,10 @@ export class ThreadsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#join-thread} * @param threadId - The id of the thread to join + * @param options - The options to use when joining the thread */ - public async join(threadId: Snowflake) { - await this.rest.put(Routes.threadMembers(threadId, '@me')); + public async join(threadId: Snowflake, { signal }: Pick = {}) { + await this.rest.put(Routes.threadMembers(threadId, '@me'), { signal }); } /** @@ -77,9 +90,10 @@ export class ThreadsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#add-thread-member} * @param threadId - The id of the thread to add the member to * @param userId - The id of the user to add to the thread + * @param options - The options to use when adding the member to the thread */ - public async addMember(threadId: Snowflake, userId: Snowflake) { - await this.rest.put(Routes.threadMembers(threadId, userId)); + public async addMember(threadId: Snowflake, userId: Snowflake, { signal }: Pick = {}) { + await this.rest.put(Routes.threadMembers(threadId, userId), { signal }); } /** @@ -87,9 +101,10 @@ export class ThreadsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#leave-thread} * @param threadId - The id of the thread to leave + * @param options - The options to use when leaving the thread */ - public async leave(threadId: Snowflake) { - await this.rest.delete(Routes.threadMembers(threadId, '@me')); + public async leave(threadId: Snowflake, { signal }: Pick = {}) { + await this.rest.delete(Routes.threadMembers(threadId, '@me'), { signal }); } /** @@ -98,9 +113,10 @@ export class ThreadsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#remove-thread-member} * @param threadId - The id of the thread to remove the member from * @param userId - The id of the user to remove from the thread + * @param options - The options to use when removing the member from the thread */ - public async removeMember(threadId: Snowflake, userId: Snowflake) { - await this.rest.delete(Routes.threadMembers(threadId, userId)); + public async removeMember(threadId: Snowflake, userId: Snowflake, { signal }: Pick = {}) { + await this.rest.delete(Routes.threadMembers(threadId, userId), { signal }); } /** @@ -109,9 +125,10 @@ export class ThreadsAPI { * @see {@link https://discord.com/developers/docs/resources/channel#get-thread-member} * @param threadId - The id of the thread to fetch the member from * @param userId - The id of the user + * @param options - The options to use when fetching the member */ - public async getMember(threadId: Snowflake, userId: Snowflake) { - return this.rest.get(Routes.threadMembers(threadId, userId)) as Promise; + public async getMember(threadId: Snowflake, userId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.threadMembers(threadId, userId), { signal }) as Promise; } /** @@ -119,8 +136,9 @@ export class ThreadsAPI { * * @see {@link https://discord.com/developers/docs/resources/channel#list-thread-members} * @param threadId - The id of the thread to fetch the members from + * @param options - The options to use when fetching the members */ - public async getAllMembers(threadId: Snowflake) { - return this.rest.get(Routes.threadMembers(threadId)) as Promise; + public async getAllMembers(threadId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.threadMembers(threadId), { signal }) as Promise; } } diff --git a/packages/core/src/api/user.ts b/packages/core/src/api/user.ts index 8280b51e3..b76837f39 100644 --- a/packages/core/src/api/user.ts +++ b/packages/core/src/api/user.ts @@ -1,4 +1,4 @@ -import { makeURLSearchParams, type REST } from '@discordjs/rest'; +import { makeURLSearchParams, type RequestData, type REST } from '@discordjs/rest'; import { Routes, type RESTGetAPICurrentUserApplicationRoleConnectionResult, @@ -26,29 +26,33 @@ export class UsersAPI { * * @see {@link https://discord.com/developers/docs/resources/user#get-user} * @param userId - The id of the user to fetch + * @param options - The options to use when fetching the user */ - public async get(userId: Snowflake) { - return this.rest.get(Routes.user(userId)) as Promise; + public async get(userId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.user(userId), { signal }) as Promise; } /** * Returns the user object of the requester's account * * @see {@link https://discord.com/developers/docs/resources/user#get-current-user} + * @param options - The options to use when fetching the current user */ - public async getCurrent() { - return this.rest.get(Routes.user('@me')) as Promise; + public async getCurrent({ signal }: Pick = {}) { + return this.rest.get(Routes.user('@me'), { signal }) as Promise; } /** * Returns a list of partial guild objects the current user is a member of * * @see {@link https://discord.com/developers/docs/resources/user#get-current-user-guilds} - * @param options - The options to use when fetching the current user's guilds + * @param query - The query options to use when fetching the current user's guilds + * @param options - The options to use when fetching the guilds */ - public async getGuilds(options: RESTGetAPICurrentUserGuildsQuery = {}) { + public async getGuilds(query: RESTGetAPICurrentUserGuildsQuery = {}, { signal }: Pick = {}) { return this.rest.get(Routes.userGuilds(), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), + signal, }) as Promise; } @@ -57,19 +61,21 @@ export class UsersAPI { * * @see {@link https://discord.com/developers/docs/resources/user#leave-guild} * @param guildId - The id of the guild + * @param options - The options for leaving the guild */ - public async leaveGuild(guildId: Snowflake) { - await this.rest.delete(Routes.userGuild(guildId)); + public async leaveGuild(guildId: Snowflake, { signal }: Pick = {}) { + await this.rest.delete(Routes.userGuild(guildId), { signal }); } /** * Edits the current user * * @see {@link https://discord.com/developers/docs/resources/user#modify-current-user} - * @param user - The new data for the current user + * @param body - The new data for the current user + * @param options - The options for editing the user */ - public async edit(user: RESTPatchAPICurrentUserJSONBody) { - return this.rest.patch(Routes.user('@me'), { body: user }) as Promise; + public async edit(body: RESTPatchAPICurrentUserJSONBody, { signal }: Pick = {}) { + return this.rest.patch(Routes.user('@me'), { body, signal }) as Promise; } /** @@ -77,9 +83,10 @@ export class UsersAPI { * * @see {@link https://discord.com/developers/docs/resources/user#get-current-user-guild-member} * @param guildId - The id of the guild + * @param options - The options for fetching the guild member */ - public async getGuildMember(guildId: Snowflake) { - return this.rest.get(Routes.userGuildMember(guildId)) as Promise; + public async getGuildMember(guildId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.userGuildMember(guildId), { signal }) as Promise; } /** @@ -87,13 +94,18 @@ export class UsersAPI { * * @see {@link https://discord.com/developers/docs/resources/guild#modify-current-member} * @param guildId - The id of the guild - * @param member - The new data for the guild member - * @param reason - The reason for editing this guild member + * @param body - The new data for the guild member + * @param options - The options for editing the guild member */ - public async editGuildMember(guildId: Snowflake, member: RESTPatchAPIGuildMemberJSONBody = {}, reason?: string) { + public async editCurrentGuildMember( + guildId: Snowflake, + body: RESTPatchAPIGuildMemberJSONBody = {}, + { reason, signal }: Pick = {}, + ) { return this.rest.patch(Routes.guildMember(guildId, '@me'), { reason, - body: member, + body, + signal, }) as Promise; } @@ -102,10 +114,12 @@ export class UsersAPI { * * @see {@link https://discord.com/developers/docs/resources/user#create-dm} * @param userId - The id of the user to open a DM channel with + * @param options - The options for opening the DM */ - public async createDM(userId: Snowflake) { + public async createDM(userId: Snowflake, { signal }: Pick = {}) { return this.rest.post(Routes.userChannels(), { body: { recipient_id: userId }, + signal, }) as Promise; } @@ -113,9 +127,10 @@ export class UsersAPI { * Gets the current user's connections * * @see {@link https://discord.com/developers/docs/resources/user#get-user-connections} + * @param options - The options for fetching the user's connections */ - public async getConnections() { - return this.rest.get(Routes.userConnections()) as Promise; + public async getConnections({ signal }: Pick = {}) { + return this.rest.get(Routes.userConnections(), { signal }) as Promise; } /** @@ -123,11 +138,12 @@ export class UsersAPI { * * @see {@link https://discord.com/developers/docs/resources/user#get-user-application-role-connection} * @param applicationId - The id of the application + * @param options - The options for fetching the role connections */ - public async getApplicationRoleConnection(applicationId: Snowflake) { - return this.rest.get( - Routes.userApplicationRoleConnection(applicationId), - ) as Promise; + public async getApplicationRoleConnection(applicationId: Snowflake, { signal }: Pick = {}) { + return this.rest.get(Routes.userApplicationRoleConnection(applicationId), { + signal, + }) as Promise; } /** @@ -135,14 +151,17 @@ export class UsersAPI { * * @see {@link https://discord.com/developers/docs/resources/user#update-user-application-role-connection} * @param applicationId - The id of the application + * @param body - The data to use when updating the application role connection * @param options - The options to use when updating the application role connection */ public async updateApplicationRoleConnection( applicationId: Snowflake, - options: RESTPutAPICurrentUserApplicationRoleConnectionJSONBody, + body: RESTPutAPICurrentUserApplicationRoleConnectionJSONBody, + { signal }: Pick = {}, ) { return this.rest.put(Routes.userApplicationRoleConnection(applicationId), { - body: options, + body, + signal, }) as Promise; } } diff --git a/packages/core/src/api/voice.ts b/packages/core/src/api/voice.ts index 4d8df9433..60d9c89a6 100644 --- a/packages/core/src/api/voice.ts +++ b/packages/core/src/api/voice.ts @@ -1,5 +1,5 @@ -import type { REST } from '@discordjs/rest'; -import { Routes, type GetAPIVoiceRegionsResult } from 'discord-api-types/v10'; +import type { RequestData, REST } from '@discordjs/rest'; +import { Routes, type RESTGetAPIVoiceRegionsResult } from 'discord-api-types/v10'; export class VoiceAPI { public constructor(private readonly rest: REST) {} @@ -8,8 +8,9 @@ export class VoiceAPI { * Fetches all voice regions * * @see {@link https://discord.com/developers/docs/resources/voice#list-voice-regions} + * @param options - The options to use when fetching the voice regions */ - public async getVoiceRegions() { - return this.rest.get(Routes.voiceRegions()) as Promise; + public async getVoiceRegions({ signal }: Pick = {}) { + return this.rest.get(Routes.voiceRegions(), { signal }) as Promise; } } diff --git a/packages/core/src/api/webhook.ts b/packages/core/src/api/webhook.ts index fd462e083..e307b3f4f 100644 --- a/packages/core/src/api/webhook.ts +++ b/packages/core/src/api/webhook.ts @@ -1,6 +1,7 @@ -import { makeURLSearchParams, type RawFile, type REST } from '@discordjs/rest'; +import { makeURLSearchParams, type RequestData, type RawFile, type REST } from '@discordjs/rest'; +import { Routes } from 'discord-api-types/v10'; import { - Routes, + type RESTGetAPIWebhookWithTokenMessageQuery, type RESTGetAPIChannelMessageResult, type RESTGetAPIWebhookResult, type RESTPatchAPIWebhookJSONBody, @@ -27,9 +28,10 @@ export class WebhooksAPI { * @see {@link https://discord.com/developers/docs/resources/webhook#get-webhook-with-token} * @param id - The id of the webhook * @param token - The token of the webhook + * @param options - The options to use when fetching the webhook */ - public async get(id: Snowflake, token?: string) { - return this.rest.get(Routes.webhook(id, token)) as Promise; + public async get(id: Snowflake, token?: string, { signal }: Pick = {}) { + return this.rest.get(Routes.webhook(id, token), { signal }) as Promise; } /** @@ -37,13 +39,18 @@ export class WebhooksAPI { * * @see {@link https://discord.com/developers/docs/resources/webhook#create-webhook} * @param channelId - The id of the channel to create the webhook in - * @param data - The data to use when creating the webhook - * @param reason - The reason for creating the webhook + * @param body - The data to use when creating the webhook + * @param options - The options to use when creating the webhook */ - public async create(channelId: Snowflake, data: RESTPostAPIChannelWebhookJSONBody, reason?: string) { + public async create( + channelId: Snowflake, + body: RESTPostAPIChannelWebhookJSONBody, + { reason, signal }: Pick = {}, + ) { return this.rest.post(Routes.channelWebhooks(channelId), { reason, - body: data, + body, + signal, }) as Promise; } @@ -53,15 +60,19 @@ export class WebhooksAPI { * @see {@link https://discord.com/developers/docs/resources/webhook#modify-webhook} * @see {@link https://discord.com/developers/docs/resources/webhook#modify-webhook-with-token} * @param id - The id of the webhook to edit - * @param webhook - The new webhook data + * @param body - The new webhook data * @param options - The options to use when editing the webhook */ public async edit( id: Snowflake, - webhook: RESTPatchAPIWebhookJSONBody, - { token, reason }: { reason?: string; token?: string } = {}, + body: RESTPatchAPIWebhookJSONBody, + { token, reason, signal }: Pick & { token?: string | undefined } = {}, ) { - return this.rest.patch(Routes.webhook(id, token), { reason, body: webhook }) as Promise; + return this.rest.patch(Routes.webhook(id, token), { + reason, + body, + signal, + }) as Promise; } /** @@ -72,8 +83,11 @@ export class WebhooksAPI { * @param id - The id of the webhook to delete * @param options - The options to use when deleting the webhook */ - public async delete(id: Snowflake, { token, reason }: { reason?: string; token?: string } = {}) { - await this.rest.delete(Routes.webhook(id, token), { reason }); + public async delete( + id: Snowflake, + { token, reason, signal }: Pick & { token?: string | undefined } = {}, + ) { + await this.rest.delete(Routes.webhook(id, token), { reason, signal }); } /** @@ -82,12 +96,14 @@ export class WebhooksAPI { * @see {@link https://discord.com/developers/docs/resources/webhook#execute-webhook} * @param id - The id of the webhook * @param token - The token of the webhook - * @param data - The data to use when executing the webhook + * @param body - The data to use when executing the webhook + * @param options - The options to use when executing the webhook */ public async execute( id: Snowflake, token: string, - data: RESTPostAPIWebhookWithTokenJSONBody & RESTPostAPIWebhookWithTokenQuery & { files?: RawFile[]; wait: true }, + body: RESTPostAPIWebhookWithTokenJSONBody & RESTPostAPIWebhookWithTokenQuery & { files?: RawFile[]; wait: true }, + { signal }: Pick, ): Promise; /** @@ -96,12 +112,14 @@ export class WebhooksAPI { * @see {@link https://discord.com/developers/docs/resources/webhook#execute-webhook} * @param id - The id of the webhook * @param token - The token of the webhook - * @param data - The data to use when executing the webhook + * @param body - The data to use when executing the webhook + * @param options - The options to use when executing the webhook */ public async execute( id: Snowflake, token: string, - data: RESTPostAPIWebhookWithTokenJSONBody & RESTPostAPIWebhookWithTokenQuery & { files?: RawFile[]; wait?: false }, + body: RESTPostAPIWebhookWithTokenJSONBody & RESTPostAPIWebhookWithTokenQuery & { files?: RawFile[]; wait?: false }, + { signal }: Pick, ): Promise; /** @@ -110,7 +128,8 @@ export class WebhooksAPI { * @see {@link https://discord.com/developers/docs/resources/webhook#execute-webhook} * @param id - The id of the webhook * @param token - The token of the webhook - * @param data - The data to use when executing the webhook + * @param body - The data to use when executing the webhook + * @param options - The options to use when executing the webhook */ public async execute( id: Snowflake, @@ -121,12 +140,14 @@ export class WebhooksAPI { files, ...body }: RESTPostAPIWebhookWithTokenJSONBody & RESTPostAPIWebhookWithTokenQuery & { files?: RawFile[] }, + { signal }: Pick = {}, ) { return this.rest.post(Routes.webhook(id, token), { query: makeURLSearchParams({ wait, thread_id }), files, body, auth: false, + signal, // eslint-disable-next-line @typescript-eslint/no-invalid-void-type }) as Promise; } @@ -137,18 +158,21 @@ export class WebhooksAPI { * @see {@link https://discord.com/developers/docs/resources/webhook#execute-slackcompatible-webhook} * @param id - The id of the webhook * @param token - The token of the webhook + * @param query - The query options to use when executing the webhook * @param options - The options to use when executing the webhook */ public async executeSlack( id: Snowflake, token: string, body: unknown, - options: RESTPostAPIWebhookWithTokenSlackQuery = {}, + query: RESTPostAPIWebhookWithTokenSlackQuery = {}, + { signal }: Pick = {}, ) { await this.rest.post(Routes.webhookPlatform(id, token, 'slack'), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), body, auth: false, + signal, }); } @@ -158,17 +182,20 @@ export class WebhooksAPI { * @see {@link https://discord.com/developers/docs/resources/webhook#execute-githubcompatible-webhook} * @param id - The id of the webhook * @param token - The token of the webhook + * @param query - The options to use when executing the webhook * @param options - The options to use when executing the webhook */ public async executeGitHub( id: Snowflake, token: string, body: unknown, - options: RESTPostAPIWebhookWithTokenGitHubQuery = {}, + query: RESTPostAPIWebhookWithTokenGitHubQuery = {}, + { signal }: Pick = {}, ) { await this.rest.post(Routes.webhookPlatform(id, token, 'github'), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), body, + signal, auth: false, }); } @@ -180,12 +207,20 @@ export class WebhooksAPI { * @param id - The id of the webhook * @param token - The token of the webhook * @param messageId - The id of the message to fetch + * @param query - The query options to use when fetching the message * @param options - The options to use when fetching the message */ - public async getMessage(id: Snowflake, token: string, messageId: Snowflake, options: { thread_id?: string } = {}) { + public async getMessage( + id: Snowflake, + token: string, + messageId: Snowflake, + query: RESTGetAPIWebhookWithTokenMessageQuery = {}, + { signal }: Pick = {}, + ) { return this.rest.get(Routes.webhookMessage(id, token, messageId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), auth: false, + signal, }) as Promise; } @@ -196,18 +231,21 @@ export class WebhooksAPI { * @param id - The id of the webhook * @param token - The token of the webhook * @param messageId - The id of the message to edit - * @param data - The data to use when editing the message + * @param body - The data to use when editing the message + * @param options - The options to use when editing the message */ public async editMessage( id: Snowflake, token: string, messageId: Snowflake, { thread_id, ...body }: RESTPatchAPIWebhookWithTokenMessageJSONBody & { thread_id?: string }, + { signal }: Pick = {}, ) { return this.rest.patch(Routes.webhookMessage(id, token, messageId), { query: makeURLSearchParams({ thread_id }), auth: false, body, + signal, }) as Promise; } @@ -218,12 +256,20 @@ export class WebhooksAPI { * @param id - The id of the webhook * @param token - The token of the webhook * @param messageId - The id of the message to delete + * @param query - The options to use when deleting the message * @param options - The options to use when deleting the message */ - public async deleteMessage(id: Snowflake, token: string, messageId: Snowflake, options: { thread_id?: string } = {}) { + public async deleteMessage( + id: Snowflake, + token: string, + messageId: Snowflake, + query: { thread_id?: string } = {}, + { signal }: Pick = {}, + ) { await this.rest.delete(Routes.webhookMessage(id, token, messageId), { - query: makeURLSearchParams(options), + query: makeURLSearchParams(query), auth: false, + signal, }); } }