From be5bcb5bbc55a4b74090a15c9d591aed5e850c3b Mon Sep 17 00:00:00 2001 From: ITOH Date: Wed, 25 May 2022 15:53:53 +0200 Subject: [PATCH] fix(rest)!: `editApplicationCommandPermissions` (#2238) * fix(rest)!: `editApplicationCommandPermissions` - add `bearerToken` option - add headers to `RestPayload` - remove `batchEditApplicationCommandPermissions` since the related endpoint has been removed * fix header prefix --- .../batchEditApplicationCommandPermissions.ts | 29 ------------------- .../editApplicationCommandPermissions.ts | 17 ++++++++++- helpers/interactions/commands/mod.ts | 1 - rest/createRequestBody.ts | 13 +++++++-- rest/rest.ts | 1 + rest/runMethod.ts | 27 +++++++---------- 6 files changed, 38 insertions(+), 50 deletions(-) delete mode 100644 helpers/interactions/commands/batchEditApplicationCommandPermissions.ts diff --git a/helpers/interactions/commands/batchEditApplicationCommandPermissions.ts b/helpers/interactions/commands/batchEditApplicationCommandPermissions.ts deleted file mode 100644 index 1e388db96..000000000 --- a/helpers/interactions/commands/batchEditApplicationCommandPermissions.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { Bot } from "../../../bot.ts"; -import { DiscordGuildApplicationCommandPermissions } from "../../../types/discord.ts"; -import { ApplicationCommandPermissionTypes } from "../../../types/shared.ts"; - -/** Batch edits permissions for all commands in a guild. Takes an array of partial GuildApplicationCommandPermissions objects including `id` and `permissions`. */ -export async function batchEditApplicationCommandPermissions( - bot: Bot, - guildId: bigint, - options: { id: string; permissions: ApplicationCommandPermissions[] }[], -) { - const result = await bot.rest.runMethod( - bot.rest, - "put", - bot.constants.endpoints.COMMANDS_PERMISSIONS(bot.applicationId, guildId), - options, - ); - - return result.map((res) => bot.transformers.applicationCommandPermission(bot, res)); -} - -/** https://discord.com/developers/docs/interactions/slash-commands#applicationcommandpermissions */ -export interface ApplicationCommandPermissions { - /** The id of the role or user */ - id: string; - /** Role or User */ - type: ApplicationCommandPermissionTypes; - /** `true` to allow, `false`, to disallow */ - permission: boolean; -} diff --git a/helpers/interactions/commands/editApplicationCommandPermissions.ts b/helpers/interactions/commands/editApplicationCommandPermissions.ts index 1f9893e8a..a06af3e1b 100644 --- a/helpers/interactions/commands/editApplicationCommandPermissions.ts +++ b/helpers/interactions/commands/editApplicationCommandPermissions.ts @@ -1,12 +1,14 @@ import type { Bot } from "../../../bot.ts"; import { DiscordGuildApplicationCommandPermissions } from "../../../types/discord.ts"; -import { ApplicationCommandPermissions } from "./batchEditApplicationCommandPermissions.ts"; +import { ApplicationCommandPermissionTypes } from "../../../types/shared.ts"; /** Edits command permissions for a specific command for your application in a guild. */ export async function editApplicationCommandPermissions( bot: Bot, guildId: bigint, commandId: bigint, + /** Bearer token which has the `applications.commands.permissions.update` scope and also access to this guild. */ + bearerToken: string, options: ApplicationCommandPermissions[], ) { const result = await bot.rest.runMethod( @@ -16,7 +18,20 @@ export async function editApplicationCommandPermissions( { permissions: options, }, + { + headers: { authorization: `Bearer ${bearerToken}` }, + }, ); return bot.transformers.applicationCommandPermission(bot, result); } + +/** https://discord.com/developers/docs/interactions/application-commands#edit-application-command-permissions */ +export interface ApplicationCommandPermissions { + /** The id of the role or user */ + id: string; + /** Role or User */ + type: ApplicationCommandPermissionTypes; + /** `true` to allow, `false`, to disallow */ + permission: boolean; +} diff --git a/helpers/interactions/commands/mod.ts b/helpers/interactions/commands/mod.ts index 2bf6aa764..7d69c7e94 100644 --- a/helpers/interactions/commands/mod.ts +++ b/helpers/interactions/commands/mod.ts @@ -1,4 +1,3 @@ -export * from "./batchEditApplicationCommandPermissions.ts"; export * from "./createApplicationCommand.ts"; export * from "./deleteApplicationCommand.ts"; export * from "./deleteInteractionResponse.ts"; diff --git a/rest/createRequestBody.ts b/rest/createRequestBody.ts index b207d8e7a..c4c34fe48 100644 --- a/rest/createRequestBody.ts +++ b/rest/createRequestBody.ts @@ -5,11 +5,18 @@ import { RestPayload, RestRequest } from "./rest.ts"; /** Creates the request body and headers that are necessary to send a request. Will handle different types of methods and everything necessary for discord. */ export function createRequestBody(rest: RestManager, queuedRequest: { request: RestRequest; payload: RestPayload }) { - const headers: { [key: string]: string } = { - Authorization: `Bot ${rest.token}`, - "User-Agent": USER_AGENT, + const headers: Record = { + authorization: `Bot ${rest.token}`, + "user-agent": USER_AGENT, }; + // SOMETIMES SPECIAL HEADERS (E.G. CUSTOM AUTHORIZATION) NEED TO BE USED + if (queuedRequest.payload.headers) { + for (const key in queuedRequest.payload.headers) { + headers[key] = queuedRequest.payload.headers[key]; + } + } + // GET METHODS SHOULD NOT HAVE A BODY if (queuedRequest.request.method.toUpperCase() === "GET") { queuedRequest.payload.body = undefined; diff --git a/rest/rest.ts b/rest/rest.ts index 408673837..a8d32e0ad 100644 --- a/rest/rest.ts +++ b/rest/rest.ts @@ -77,6 +77,7 @@ export interface RestPayload { bucketId?: string; body?: Record; retryCount: number; + headers?: Record; } export interface RestRateLimitedPath { diff --git a/rest/runMethod.ts b/rest/runMethod.ts index f5ca39e5c..c5cab72fd 100644 --- a/rest/runMethod.ts +++ b/rest/runMethod.ts @@ -2,27 +2,21 @@ import { RestManager } from "../bot.ts"; import { API_VERSION, BASE_URL, IMAGE_BASE_URL } from "../util/constants.ts"; import { RestRequestRejection, RestRequestResponse } from "./rest.ts"; -export async function runMethod( - rest: RestManager, - method: "get", - url: string, -): Promise; -export async function runMethod( - rest: RestManager, - method: "post" | "put" | "delete" | "patch", - url: string, - body?: unknown, -): Promise; export async function runMethod( rest: RestManager, method: "get" | "post" | "put" | "delete" | "patch", url: string, body?: unknown, - retryCount = 0, - bucketId?: string, + options?: { + retryCount?: number; + bucketId?: string; + headers?: Record; + }, ): Promise { rest.debug( - `[REST - RequestCreate] Method: ${method} | URL: ${url} | Retry Count: ${retryCount} | Bucket ID: ${bucketId} | Body: ${ + `[REST - RequestCreate] Method: ${method} | URL: ${url} | Retry Count: ${ + options?.retryCount ?? 0 + } | Bucket ID: ${options?.bucketId} | Body: ${ JSON.stringify( body, ) @@ -72,9 +66,10 @@ export async function runMethod( resolve(data.status !== 204 ? JSON.parse(data.body ?? "{}") : (undefined as unknown as T)), }, { - bucketId, + bucketId: options?.bucketId, body: body as Record | undefined, - retryCount, + retryCount: options?.retryCount ?? 0, + headers: options?.headers, }, ); });