Merge branch 'main' of github.com:discordeno/discordeno into main

This commit is contained in:
ITOH
2022-05-25 18:53:46 +02:00
8 changed files with 41 additions and 53 deletions
@@ -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
View File
@@ -1,4 +1,3 @@
export * from "./batchEditApplicationCommandPermissions.ts";
export * from "./createApplicationCommand.ts";
export * from "./deleteApplicationCommand.ts";
export * from "./deleteInteractionResponse.ts";
@@ -32,7 +32,6 @@ export async function sendInteractionResponse(
users: options.data.allowedMentions!.users?.map((id) => id.toString()),
roles: options.data.allowedMentions!.roles?.map((id) => id.toString()),
},
file: options.data.file,
custom_id: options.data.customId,
title: options.data.title,
components: options.data.components?.map((component) => ({
@@ -105,6 +104,7 @@ export async function sendInteractionResponse(
{
type: options.type,
data,
file: options.data.file,
},
);
}
@@ -114,7 +114,7 @@ export async function sendInteractionResponse(
bot.rest,
"post",
bot.constants.endpoints.WEBHOOK(bot.applicationId, token),
data,
{ ...data, file: options.data.file },
);
return bot.transformers.message(bot, result);
+1 -1
View File
@@ -15,7 +15,7 @@ export async function createInvite(bot: Bot, channelId: bigint, options: CreateC
unique: options.unique,
target_type: options.targetType,
target_user_id: options.targetUserId,
target_application_id: options.targetUserId,
target_application_id: options.targetApplicationId,
},
);
+10 -3
View File
@@ -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;
+1
View File
@@ -77,6 +77,7 @@ export interface RestPayload {
bucketId?: string;
body?: Record<string, unknown>;
retryCount: number;
headers?: Record<string, string>;
}
export interface RestRateLimitedPath {
+11 -16
View File
@@ -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,
},
);
});