From 9520741de7d81948d423c88f904ddf1d3a51205e Mon Sep 17 00:00:00 2001 From: deepsarda <92147339+deepsarda@users.noreply.github.com> Date: Mon, 16 Jan 2023 22:22:24 +0530 Subject: [PATCH] Add rest helpers for templates (#2727) * Add rest helpers for templates * Fix code style issues with ESLint Co-authored-by: Lint Action --- .../client/src/Structures/channels/Guild.ts | 2 +- packages/rest/src/manager.ts | 1498 +++++++++-------- 2 files changed, 843 insertions(+), 657 deletions(-) diff --git a/packages/client/src/Structures/channels/Guild.ts b/packages/client/src/Structures/channels/Guild.ts index 78264e821..d41351c8e 100644 --- a/packages/client/src/Structures/channels/Guild.ts +++ b/packages/client/src/Structures/channels/Guild.ts @@ -84,7 +84,7 @@ export class GuildChannel extends Channel { if (permission & BigInt(BitwisePermissionFlags.ADMINISTRATOR)) { return new Permission(BitwisePermissionFlags.ADMINISTRATOR) } - const channel = this instanceof ThreadChannel ? this.guild.channels.get(this.parentID!) : this + const channel = this instanceof ThreadChannel ? this.guild.channels.get(this.parentID) : this let overwrite = channel?.permissionOverwrites.get(this.guild.id) if (overwrite) { permission = (permission & ~overwrite.deny) | overwrite.allow diff --git a/packages/rest/src/manager.ts b/packages/rest/src/manager.ts index 4db8c6b50..ca1a96408 100644 --- a/packages/rest/src/manager.ts +++ b/packages/rest/src/manager.ts @@ -347,68 +347,68 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage // Interaction Endpoints interactions: { commands: { -// Application Endpoints -commands: (applicationId) => { - return `/applications/${applicationId}/commands` -}, + // Application Endpoints + commands: (applicationId) => { + return `/applications/${applicationId}/commands` + }, -guilds: { - all(applicationId, guildId) { - return `/applications/${applicationId}/guilds/${guildId}/commands` - }, + guilds: { + all(applicationId, guildId) { + return `/applications/${applicationId}/guilds/${guildId}/commands` + }, - one(applicationId, guildId, commandId, withLocalizations) { - let url = `/applications/${applicationId}/guilds/${guildId}/commands/${commandId}?` + one(applicationId, guildId, commandId, withLocalizations) { + let url = `/applications/${applicationId}/guilds/${guildId}/commands/${commandId}?` - if (withLocalizations !== undefined) { - url += `with_localizations=${withLocalizations.toString()}` - } + if (withLocalizations !== undefined) { + url += `with_localizations=${withLocalizations.toString()}` + } - return url - }, -}, -permissions: (applicationId, guildId) => { - return `/applications/${applicationId}/guilds/${guildId}/commands/permissions` -}, -permission: ( - applicationId, - guildId, - commandId -) => { - return `/applications/${applicationId}/guilds/${guildId}/commands/${commandId}/permissions` -}, -command: ( - applicationId, - commandId, - withLocalizations -) => { - let url = `/applications/${applicationId}/commands/${commandId}?` + return url + }, + }, + permissions: (applicationId, guildId) => { + return `/applications/${applicationId}/guilds/${guildId}/commands/permissions` + }, + permission: ( + applicationId, + guildId, + commandId + ) => { + return `/applications/${applicationId}/guilds/${guildId}/commands/${commandId}/permissions` + }, + command: ( + applicationId, + commandId, + withLocalizations + ) => { + let url = `/applications/${applicationId}/commands/${commandId}?` - if (withLocalizations !== undefined) { - url += `withLocalizations=${withLocalizations.toString()}` - } + if (withLocalizations !== undefined) { + url += `withLocalizations=${withLocalizations.toString()}` + } - return url -}, + return url + }, }, responses: { -// Interaction Endpoints -callback: (interactionId, token) => { - return `/interactions/${interactionId}/${token}/callback` -}, -original: (interactionId, token) => { - return `/webhooks/${interactionId}/${token}/messages/@original` -}, -message: ( - applicationId, - token, - messageId -) => { - return `/webhooks/${applicationId}/${token}/messages/${messageId}` -}, + // Interaction Endpoints + callback: (interactionId, token) => { + return `/interactions/${interactionId}/${token}/callback` + }, + original: (interactionId, token) => { + return `/webhooks/${interactionId}/${token}/messages/@original` + }, + message: ( + applicationId, + token, + messageId + ) => { + return `/webhooks/${applicationId}/${token}/messages/${messageId}` + }, }, }, @@ -718,7 +718,7 @@ message: ( return await rest.deleteMessage(channelId, messageId, reason) }, async edit(channelId, messageId, options) { - return await rest.editMessage(channelId, messageId, options) + return await rest.editMessage(channelId, messageId, options) }, async send(channelId, options) { @@ -1181,19 +1181,19 @@ message: ( return await rest.post(rest.routes.guilds.emojis(guildId), options) }, -async createGlobalApplicationCommand (command) { - return await rest.post( - rest.routes.interactions.commands.commands(rest.applicationId), - command - ) -}, + async createGlobalApplicationCommand(command) { + return await rest.post( + rest.routes.interactions.commands.commands(rest.applicationId), + command + ) + }, -async createGuildApplicationCommand (command,guildId) { - return await rest.post( - rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), - command - ) -}, + async createGuildApplicationCommand(command, guildId) { + return await rest.post( + rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), + command + ) + }, async createForumThread(channelId, options) { return await rest.post(rest.routes.channels.forum(channelId), options) @@ -1235,39 +1235,39 @@ async createGuildApplicationCommand (command,guildId) { return await rest.delete(rest.routes.guilds.emoji(guildId, id), { reason }) }, -async deleteFollowupMessage ( - token, - messageId -) { - return await rest.delete( - rest.routes.interactions.responses.message( - rest.applicationId, + async deleteFollowupMessage( token, messageId - ) - ) -}, + ) { + return await rest.delete( + rest.routes.interactions.responses.message( + rest.applicationId, + token, + messageId + ) + ) + }, -async deleteGlobalApplicationCommand ( - commandId -) { - return await rest.delete( - rest.routes.interactions.commands.command(rest.applicationId, commandId) - ) -}, - - async deleteGuildApplicationCommand ( - commandId, - guildId -) { - return await rest.delete( - rest.routes.interactions.commands.guilds.one( - rest.applicationId, - guildId, + async deleteGlobalApplicationCommand( commandId - ) - ) -}, + ) { + return await rest.delete( + rest.routes.interactions.commands.command(rest.applicationId, commandId) + ) + }, + + async deleteGuildApplicationCommand( + commandId, + guildId + ) { + return await rest.delete( + rest.routes.interactions.commands.guilds.one( + rest.applicationId, + guildId, + commandId + ) + ) + }, async deleteIntegration(guildId, integrationId) { return await rest.delete(rest.routes.guilds.integration(guildId, integrationId)) @@ -1277,24 +1277,24 @@ async deleteGlobalApplicationCommand ( return await rest.delete(rest.routes.guilds.invite(inviteCode), reason ? { reason } : undefined) }, -async deleteMessage (channelId,messageId,reason) { - return await rest.delete( - rest.routes.channels.message(channelId, messageId), - { reason } - ) -}, + async deleteMessage(channelId, messageId, reason) { + return await rest.delete( + rest.routes.channels.message(channelId, messageId), + { reason } + ) + }, - async deleteOriginalInteractionResponse ( - token -) { - return await rest.delete( - rest.routes.interactions.responses.original( - rest.applicationId, + async deleteOriginalInteractionResponse( token - ) - ) -}, + ) { + return await rest.delete( + rest.routes.interactions.responses.original( + rest.applicationId, + token + ) + ) + }, async deleteScheduledEvent(guildId, eventId) { return await rest.delete(rest.routes.guilds.events.event(guildId, eventId)) @@ -1316,27 +1316,27 @@ async deleteMessage (channelId,messageId,reason) { return await rest.delete(rest.routes.webhooks.webhook(webhookId, token)) }, - -async editApplicationCommandPermissions ( - guildId, - commandId, - bearerToken, - options -) { - return await rest.put( - rest.routes.interactions.commands.permission( - rest.applicationId, - guildId, - commandId - ), - { - permissions: options - }, - { - headers: { authorization: `Bearer ${bearerToken}` } - } - ) -}, + + async editApplicationCommandPermissions( + guildId, + commandId, + bearerToken, + options + ) { + return await rest.put( + rest.routes.interactions.commands.permission( + rest.applicationId, + guildId, + commandId + ), + { + permissions: options + }, + { + headers: { authorization: `Bearer ${bearerToken}` } + } + ) + }, async editAutomodRule(guildId, ruleId, options) { return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), options) @@ -1384,63 +1384,63 @@ async editApplicationCommandPermissions ( * * @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#edit-followup-message} */ -async editFollowupMessage ( - token, - messageId, - options -) { - return await rest.patch( - rest.routes.interactions.responses.message(rest.applicationId, token, messageId), - options - ) -}, + async editFollowupMessage( + token, + messageId, + options + ) { + return await rest.patch( + rest.routes.interactions.responses.message(rest.applicationId, token, messageId), + options + ) + }, - async editGlobalApplicationCommand ( - commandId, - options -) { - return await rest.patch( - rest.routes.interactions.commands.command(rest.applicationId, commandId), - options - ) -}, + async editGlobalApplicationCommand( + commandId, + options + ) { + return await rest.patch( + rest.routes.interactions.commands.command(rest.applicationId, commandId), + options + ) + }, - async editGuildApplicationCommand ( - commandId, - guildId, - options -) { - return await rest.patch( - rest.routes.interactions.commands.guilds.one( - rest.applicationId, + async editGuildApplicationCommand( + commandId, guildId, - commandId - ), -options - ) -}, + options + ) { + return await rest.patch( + rest.routes.interactions.commands.guilds.one( + rest.applicationId, + guildId, + commandId + ), + options + ) + }, - async editMessage (channelId,messageId,options) { + async editMessage(channelId, messageId, options) { return await rest.patch( rest.routes.channels.message(channelId, messageId), options ) }, -async editOriginalInteractionResponse ( - token, - options -) { - return await rest.patch( - rest.routes.interactions.responses.original( - rest.applicationId, - token - ), - options - ) -}, + async editOriginalInteractionResponse( + token, + options + ) { + return await rest.patch( + rest.routes.interactions.responses.original( + rest.applicationId, + token + ), + options + ) + }, async editOriginalWebhookMessage(webhookId, token, options) { return await rest.patch(rest.routes.webhooks.original(webhookId, token, options), { @@ -1486,29 +1486,29 @@ async editOriginalInteractionResponse ( return await rest.get(rest.routes.channels.threads.active(guildId)) }, - -async getApplicationCommandPermission ( - guildId, - commandId -) { - return await rest.get( - rest.routes.interactions.commands.permission( - rest.applicationId, - guildId, - commandId - ) - ) -}, -async getApplicationCommandPermissions ( - guildId -) { - return await rest.get< - DiscordApplicationCommandPermissions[] - >( - rest.routes.interactions.commands.permissions(rest.applicationId, guildId) - ) -}, + async getApplicationCommandPermission( + guildId, + commandId + ) { + return await rest.get( + rest.routes.interactions.commands.permission( + rest.applicationId, + guildId, + commandId + ) + ) + }, + + async getApplicationCommandPermissions( + guildId + ) { + return await rest.get< + DiscordApplicationCommandPermissions[] + >( + rest.routes.interactions.commands.permissions(rest.applicationId, guildId) + ) + }, async getApplicationInfo() { return await rest.get(rest.routes.oauth2Application()) @@ -1538,14 +1538,14 @@ async getApplicationCommandPermissions ( return await rest.get(rest.routes.channels.webhooks(channelId)) }, -async getDmChannel (userId) { - return await rest.post( - rest.routes.channels.dm(), - { - recipient_id: userId.toString() - } - ) -}, + async getDmChannel(userId) { + return await rest.post( + rest.routes.channels.dm(), + { + recipient_id: userId.toString() + } + ) + }, async getEmoji(guildId, emojiId) { return await rest.get(rest.routes.guilds.emoji(guildId, emojiId)) @@ -1555,57 +1555,57 @@ async getDmChannel (userId) { return await rest.get(rest.routes.guilds.emojis(guildId)) }, -async getFollowupMessage ( - token, - messageId -) { - return await rest.get( - rest.routes.interactions.responses.message( - rest.applicationId, + async getFollowupMessage( token, messageId - ) - ) -}, + ) { + return await rest.get( + rest.routes.interactions.responses.message( + rest.applicationId, + token, + messageId + ) + ) + }, async getGatewayBot() { return await rest.get(rest.routes.gatewayBot()) }, -async getGlobalApplicationCommand ( - commandId -) { - return await rest.get( - rest.routes.interactions.commands.command(rest.applicationId, commandId) - ) -}, - - async getGlobalApplicationCommands() { - return await rest.get( - rest.routes.interactions.commands.commands(rest.applicationId) - ) -}, - - async getGuildApplicationCommand ( - commandId, - guildId -) { - return await rest.get( - rest.routes.interactions.commands.guilds.one( - rest.applicationId, - guildId, + async getGlobalApplicationCommand( commandId - ) - ) -}, + ) { + return await rest.get( + rest.routes.interactions.commands.command(rest.applicationId, commandId) + ) + }, -async getGuildApplicationCommands ( - guildId -) { - return await rest.get( - rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId) - ) -}, + async getGlobalApplicationCommands() { + return await rest.get( + rest.routes.interactions.commands.commands(rest.applicationId) + ) + }, + + async getGuildApplicationCommand( + commandId, + guildId + ) { + return await rest.get( + rest.routes.interactions.commands.guilds.one( + rest.applicationId, + guildId, + commandId + ) + ) + }, + + async getGuildApplicationCommands( + guildId + ) { + return await rest.get( + rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId) + ) + }, async getGuildWebhooks(guildId) { return await rest.get(rest.routes.guilds.webhooks(guildId)) @@ -1627,16 +1627,16 @@ async getGuildApplicationCommands ( return await rest.get(rest.routes.nitroStickerPacks()) }, -async getOriginalInteractionResponse ( - token -) { - return await rest.get( - rest.routes.interactions.responses.original( - rest.applicationId, + async getOriginalInteractionResponse( token - ) - ) -}, + ) { + return await rest.get( + rest.routes.interactions.responses.original( + rest.applicationId, + token + ) + ) + }, async getPrivateArchivedThreads(channelId, options) { return await rest.get(rest.routes.channels.threads.private(channelId, options)) @@ -1702,66 +1702,66 @@ async getOriginalInteractionResponse ( return await rest.delete(rest.routes.channels.threads.me(channelId)) }, -async publishMessage (channelId,messageId) { - return await rest.post( - rest.routes.channels.crosspost(channelId, messageId) - ) -}, + async publishMessage(channelId, messageId) { + return await rest.post( + rest.routes.channels.crosspost(channelId, messageId) + ) + }, async removeThreadMember(channelId, userId) { return await rest.delete(rest.routes.channels.threads.user(channelId, userId)) }, -async sendFollowupMessage ( - token, - options -) { - return await new Promise((resolve, reject) => { - rest.sendRequest({ - url: rest.routes.webhooks.webhook(rest.applicationId, token), - method: 'POST', - body: rest.createRequest({ - method: 'POST', - body: {...options}, - // remove authorization header - headers: { Authorization: '' } - }), - retryCount: 0, - retryRequest: async function (options: SendRequestOptions) { - // TODO: should change to reprocess queue item - await rest.sendRequest(options) - }, - resolve, - reject, - }) - }) -}, + async sendFollowupMessage( + token, + options + ) { + return await new Promise((resolve, reject) => { + rest.sendRequest({ + url: rest.routes.webhooks.webhook(rest.applicationId, token), + method: 'POST', + body: rest.createRequest({ + method: 'POST', + body: { ...options }, + // remove authorization header + headers: { Authorization: '' } + }), + retryCount: 0, + retryRequest: async function (options: SendRequestOptions) { + // TODO: should change to reprocess queue item + await rest.sendRequest(options) + }, + resolve, + reject, + }) + }) + }, -async sendInteractionResponse ( - interactionId, - token, - options -) { - return await new Promise((resolve, reject) => { - rest.sendRequest({ - url: rest.routes.interactions.responses.callback(interactionId, token), - method: 'POST', - body: rest.createRequest({ - method: 'POST', - body: {...options}, - // remove authorization header - headers: { Authorization: '' } - }), - retryCount: 0, - retryRequest: async function (options: SendRequestOptions) { - // TODO: should change to reprocess queue item - await rest.sendRequest(options) - }, - resolve, - reject, - }) - }) -}, + async sendInteractionResponse( + interactionId, + token, + options + ) { + return await new Promise((resolve, reject) => { + rest.sendRequest({ + url: rest.routes.interactions.responses.callback(interactionId, token), + method: 'POST', + body: rest.createRequest({ + method: 'POST', + body: { ...options }, + // remove authorization header + headers: { Authorization: '' } + }), + retryCount: 0, + retryRequest: async function (options: SendRequestOptions) { + // TODO: should change to reprocess queue item + await rest.sendRequest(options) + }, + resolve, + reject, + }) + }) + }, async sendMessage(channelId, options) { return await rest.post(rest.routes.channels.messages(channelId), options) @@ -1779,27 +1779,200 @@ async sendInteractionResponse ( return await rest.post(rest.routes.channels.typing(channelId)) }, - -async upsertGlobalApplicationCommands ( - commands -) { - return await rest.put( - rest.routes.interactions.commands.commands(rest.applicationId), - commands - ) -}, -async upsertGuildApplicationCommands ( - guildId, - commands -) { - return await rest.put( - rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), - commands - ) + async upsertGlobalApplicationCommands( + commands + ) { + return await rest.put( + rest.routes.interactions.commands.commands(rest.applicationId), + commands + ) + }, -} - } + async upsertGuildApplicationCommands( + guildId, + commands + ) { + return await rest.put( + rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), + commands + ) + }, + + /** + * Creates a template from a guild. + * + * @param rest - The rest manager to use to make the request. + * @param guildId - The ID of the guild to create the template from. + * @param options - The parameters for the creation of the template. + * @returns An instance of the created {@link Template}. + * + * @remarks + * Requires the `MANAGE_GUILD` permission. + * + * Fires a _Guild Update_ gateway event. + * + * @see {@link https://discord.com/developers/docs/resources/guild-template#create-guild-template} + */ + async createGuildTemplate( + rest: RestManager, + guildId: BigString, + options: CreateTemplate + ): Promise