Add permission-checking

This commit is contained in:
ayntee
2020-11-14 11:06:44 +04:00
parent d31cf09c64
commit 087f1f1df7
2 changed files with 90 additions and 28 deletions
+69 -28
View File
@@ -14,9 +14,12 @@ import {
BanOptions, BanOptions,
ChannelCreateOptions, ChannelCreateOptions,
CreateEmojisOptions, CreateEmojisOptions,
CreateGuildFromTemplate,
CreateGuildTemplate,
CreateRoleOptions, CreateRoleOptions,
CreateServerOptions, CreateServerOptions,
EditEmojisOptions, EditEmojisOptions,
EditGuildTemplate,
EditIntegrationOptions, EditIntegrationOptions,
FetchMembersOptions, FetchMembersOptions,
GetAuditLogsOptions, GetAuditLogsOptions,
@@ -32,6 +35,7 @@ import { MemberCreatePayload } from "../types/member.ts";
import { Intents } from "../types/options.ts"; import { Intents } from "../types/options.ts";
import { Permissions } from "../types/permission.ts"; import { Permissions } from "../types/permission.ts";
import { RoleData } from "../types/role.ts"; import { RoleData } from "../types/role.ts";
import { cache } from "../utils/cache.ts";
import { formatImageURL } from "../utils/cdn.ts"; import { formatImageURL } from "../utils/cdn.ts";
import { Collection } from "../utils/collection.ts"; import { Collection } from "../utils/collection.ts";
import { botHasPermission, calculateBits } from "../utils/permissions.ts"; import { botHasPermission, calculateBits } from "../utils/permissions.ts";
@@ -638,27 +642,47 @@ export function getGuildTemplate(
* Create a new guild based on a template * Create a new guild based on a template
* NOTE: This endpoint can be used only by bots in less than 10 guilds. * NOTE: This endpoint can be used only by bots in less than 10 guilds.
*/ */
export function createGuildFromTemplate( export async function createGuildFromTemplate(
templateCode: string, templateCode: string,
data: CreateGuildFromTemplate, data: CreateGuildFromTemplate,
) { ): Promise<Guild> {
return RequestManager.post(endpoints.GUILD_TEMPLATE(templateCode), data); if (cache.guilds.size >= 10) {
} throw new Error(
"This function can only be used by bots in less than 10 guilds.",
);
}
export interface CreateGuildFromTemplate { if (data.icon) {
/** name of the guild (2-100 characters) */ data.icon = await urlToBase64(data.icon);
name: string; }
/** base64 128x128 image for the guild icon */
icon?: string; try {
const guild = RequestManager.post(
endpoints.GUILD_TEMPLATE(templateCode),
data,
) as any;
return guild;
} catch (error) {
throw error;
}
} }
/** Returns an array of guild templates */ /** Returns an array of guild templates */
export function getGuildTemplates(guildID: string) { export async function getGuildTemplates(guildID: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
return RequestManager.get(endpoints.GUILD_TEMPLATES(guildID)); return RequestManager.get(endpoints.GUILD_TEMPLATES(guildID));
} }
/** Deletes a template from a guild */ /** Deletes a template from a guild */
export function deleteGuildTemplate(guildID: string, templateCode: string) { export async function deleteGuildTemplate(
guildID: string,
templateCode: string,
) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
return RequestManager.delete( return RequestManager.delete(
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
); );
@@ -670,42 +694,59 @@ export function deleteGuildTemplate(guildID: string, templateCode: string) {
* @param name name of the template (1-100 characters) * @param name name of the template (1-100 characters)
* @param description description for the template (0-120 characters * @param description description for the template (0-120 characters
*/ */
export function createGuildTemplate( export async function createGuildTemplate(
guildID: string, guildID: string,
data: CreateGuildTemplate, data: CreateGuildTemplate,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
if (data.name.length < 1 || data.name.length > 100) {
throw new Error("The name can only be in between 1-100 characters.");
}
if (
data.description?.length &&
data.description.length > 120
) {
throw new Error("The description can only be in between 0-120 characters.");
}
return RequestManager.post(endpoints.GUILD_TEMPLATES(guildID), data); return RequestManager.post(endpoints.GUILD_TEMPLATES(guildID), data);
} }
export interface CreateGuildTemplate {
/** name of the template (1-100 characters) */
name: string;
/** description for the template (0-120 characters) */
description?: string | null;
}
/** Syncs the template to the guild's current state */ /** Syncs the template to the guild's current state */
export function syncGuildTemplate(guildID: string, templateCode: string) { export async function syncGuildTemplate(guildID: string, templateCode: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
return RequestManager.put( return RequestManager.put(
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
); );
} }
/** Edit a template's metadata */ /** Edit a template's metadata */
export function editGuildTemplate( export async function editGuildTemplate(
guildID: string, guildID: string,
templateCode: string, templateCode: string,
data: EditGuildTemplate, data: EditGuildTemplate,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
if (data.name?.length && (data.name.length < 1 || data.name.length > 100)) {
throw new Error("The name can only be in between 1-100 characters.");
}
if (
data.description?.length &&
data.description.length > 120
) {
throw new Error("The description can only be in between 0-120 characters.");
}
return RequestManager.patch( return RequestManager.patch(
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
data, data,
); );
} }
export interface EditGuildTemplate {
/** name of the template (1-100 characters) */
name?: string;
/** description for the template (0-120 characters) */
description?: string | null;
}
+21
View File
@@ -633,3 +633,24 @@ export interface Template {
/** whether the template has unsynced changes */ /** whether the template has unsynced changes */
is_dirty: boolean | null; is_dirty: boolean | null;
} }
export interface CreateGuildFromTemplate {
/** name of the guild (2-100 characters) */
name: string;
/** base64 128x128 image for the guild icon */
icon?: string;
}
export interface CreateGuildTemplate {
/** name of the template (1-100 characters) */
name: string;
/** description for the template (0-120 characters) */
description?: string;
}
export interface EditGuildTemplate {
/** name of the template (1-100 characters) */
name?: string;
/** description for the template (0-120 characters) */
description?: string;
}