mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 16:57:22 +00:00
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
This commit is contained in:
@@ -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<DiscordGuildApplicationCommandPermissions[]>(
|
||||
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;
|
||||
}
|
||||
@@ -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<DiscordGuildApplicationCommandPermissions>(
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
export * from "./batchEditApplicationCommandPermissions.ts";
|
||||
export * from "./createApplicationCommand.ts";
|
||||
export * from "./deleteApplicationCommand.ts";
|
||||
export * from "./deleteInteractionResponse.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<string, string> = {
|
||||
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;
|
||||
|
||||
@@ -77,6 +77,7 @@ export interface RestPayload {
|
||||
bucketId?: string;
|
||||
body?: Record<string, unknown>;
|
||||
retryCount: number;
|
||||
headers?: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface RestRateLimitedPath {
|
||||
|
||||
+11
-16
@@ -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<T = any>(
|
||||
rest: RestManager,
|
||||
method: "get",
|
||||
url: string,
|
||||
): Promise<T>;
|
||||
export async function runMethod<T = any>(
|
||||
rest: RestManager,
|
||||
method: "post" | "put" | "delete" | "patch",
|
||||
url: string,
|
||||
body?: unknown,
|
||||
): Promise<T>;
|
||||
export async function runMethod<T = any>(
|
||||
rest: RestManager,
|
||||
method: "get" | "post" | "put" | "delete" | "patch",
|
||||
url: string,
|
||||
body?: unknown,
|
||||
retryCount = 0,
|
||||
bucketId?: string,
|
||||
options?: {
|
||||
retryCount?: number;
|
||||
bucketId?: string;
|
||||
headers?: Record<string, string>;
|
||||
},
|
||||
): Promise<T> {
|
||||
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<T = any>(
|
||||
resolve(data.status !== 204 ? JSON.parse(data.body ?? "{}") : (undefined as unknown as T)),
|
||||
},
|
||||
{
|
||||
bucketId,
|
||||
bucketId: options?.bucketId,
|
||||
body: body as Record<string, unknown> | undefined,
|
||||
retryCount,
|
||||
retryCount: options?.retryCount ?? 0,
|
||||
headers: options?.headers,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user