From f9fddf7621a619860ddc681aa41604b535878561 Mon Sep 17 00:00:00 2001 From: ayntee Date: Fri, 13 Nov 2020 22:53:54 +0400 Subject: [PATCH 01/27] =?UTF-8?q?Server=20Templates=20=F0=9F=8E=89?= =?UTF-8?q?=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handlers/guild.ts | 28 ++++++++++++++++++++++++++++ src/types/guild.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 47385b112..8db3f76ea 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -622,3 +622,31 @@ export function getGuild(guildID: string, counts = true) { { with_counts: counts }, ) as Promise; } + +/** + * Get a template by its code + * @param code The code of the template + */ +export function getTemplate(code: string) { + const endpoint = `${endpoints.GUILDS}/templates/${code}`; + return RequestManager.get(endpoint); +} + +/** + * Create a new guild based on a template. + * NOTE: This endpoint can be used only by bots in less than 10 guilds. + * @param code the code of the template to create a new guild from + * @param name name of the guild (2-100 characters) + * @param icon base64 128x128 image for the guild icon + */ +export function createGuildFromTemplate( + code: string, + name: string, + icon?: any, +) { + const endpoint = `${endpoints.GUILDS}/templates/${code}`; + return RequestManager.post(endpoint, { + name, + icon, + }); +} diff --git a/src/types/guild.ts b/src/types/guild.ts index 7bcd4991e..e1a903fe7 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -1,3 +1,4 @@ +import { Guild } from "../structures/guild.ts"; import { ChannelCreatePayload, ChannelTypes } from "./channel.ts"; import { Emoji, StatusType } from "./discord.ts"; import { MemberCreatePayload } from "./member.ts"; @@ -606,3 +607,29 @@ export interface CreateServerOptions { /** the id of the channel where guild notices such as welcome messages and boost events are posted */ system_channel_id?: string; } + +// https://discord.com/developers/docs/resources/template#template-object +export interface GuildTemplate { + /** the template code (unique ID) */ + code: string; + /** template name */ + name: string; + /** the description for the template */ + description: string | null; + /** number of times this template has been used */ + usage_count: number; + /** the ID of the user who created the template */ + creator_id: string; + /** the user who created the template */ + user: UserPayload; + /** when this template was created */ + created_at: string; + /** when this template was last synced to the source guild */ + updated_at: string; + /** the ID of the guild this template is based on */ + source_guild_id: string; + /** the guild snapshot this template contains */ + serialized_source_guild: Guild; + /** whether the template has unsynced changes */ + is_dirty: boolean | null; +} From 985a4358f91071ba3e116e39edb303202c78c78d Mon Sep 17 00:00:00 2001 From: ayntee Date: Fri, 13 Nov 2020 23:41:41 +0400 Subject: [PATCH 02/27] Rename GuildTemplate interface to Template --- src/types/guild.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/guild.ts b/src/types/guild.ts index e1a903fe7..b3d64044a 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -609,7 +609,7 @@ export interface CreateServerOptions { } // https://discord.com/developers/docs/resources/template#template-object -export interface GuildTemplate { +export interface Template { /** the template code (unique ID) */ code: string; /** template name */ From 1f2c5853de273d2599f7fda24833800eaa06e265 Mon Sep 17 00:00:00 2001 From: ayntee Date: Sat, 14 Nov 2020 00:10:13 +0400 Subject: [PATCH 03/27] =?UTF-8?q?=F0=9F=8E=89=20Server=20Templates=20?= =?UTF-8?q?=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/discord.ts | 3 ++ src/handlers/guild.ts | 101 ++++++++++++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 16 deletions(-) diff --git a/src/constants/discord.ts b/src/constants/discord.ts index fa334574b..3cf526388 100644 --- a/src/constants/discord.ts +++ b/src/constants/discord.ts @@ -89,6 +89,9 @@ export const endpoints = { `${baseEndpoints.CDN_URL}/splashes/${id}/${icon}`, GUILD_VANITY_URL: (id: string) => `${GUILDS_BASE(id)}/vanity-url`, GUILD_WEBHOOKS: (id: string) => `${GUILDS_BASE(id)}/webhooks`, + GUILD_TEMPLATE: (code: string) => + `${baseEndpoints.BASE_URL}/guilds/templates/${code}`, + GUILD_TEMPLATES: (id: string) => `${GUILDS_BASE(id)}/templates`, WEBHOOK: (id: string, token: string) => `${baseEndpoints.BASE_URL}/webhooks/${id}/${token}`, diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 8db3f76ea..e3919cd22 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -623,30 +623,99 @@ export function getGuild(guildID: string, counts = true) { ) as Promise; } -/** - * Get a template by its code - * @param code The code of the template +/** + * Returns the guild template if it exists + * @param guildID The ID of the guild + * @param code The code of the template to get */ -export function getTemplate(code: string) { - const endpoint = `${endpoints.GUILDS}/templates/${code}`; +export function getGuildTemplate(guildID: string, code: string) { + const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${code}`; return RequestManager.get(endpoint); } /** - * 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. - * @param code the code of the template to create a new guild from - * @param name name of the guild (2-100 characters) - * @param icon base64 128x128 image for the guild icon + * @param code The code of the template to create a guild from */ export function createGuildFromTemplate( code: string, - name: string, - icon?: any, + data: CreateGuildFromTemplate, ) { - const endpoint = `${endpoints.GUILDS}/templates/${code}`; - return RequestManager.post(endpoint, { - name, - icon, - }); + return RequestManager.post(endpoints.GUILD_TEMPLATE(code), data); +} + +export interface CreateGuildFromTemplate { + /** name of the guild (2-100 characters) */ + name: string; + /** base64 128x128 image for the guild icon */ + icon?: string; +} + +/** + * Returns an array of guild templates + * @param guildID The ID of the guild + */ +export function getGuildTemplates(guildID: string) { + return RequestManager.get(endpoints.GUILD_TEMPLATES(guildID)); +} + +/** + * Deletes a template from a guild + * @param guildID The guild ID to delete the template from + */ +export function deleteGuildTemplate(guildID: string, code: string) { + const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${code}`; + return RequestManager.delete(endpoint); +} + +/** + * Creates a template for the guild + * @param guildID The ID of the guild to create a template in + * @param name name of the template (1-100 characters) + * @param description description for the template (0-120 characters + */ +export function createGuildTemplate( + guildID: string, + data: CreateGuildTemplate, +) { + 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 + * @param guildID The ID of the guild + * @param code The code of the template to sync + */ +export function syncGuildTemplate(guildID: string, code: string) { + const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${code}`; + return RequestManager.put(endpoint); +} + +/** + * Edit a template's metadata + * @param guildID The ID of the guild + * @param code The code of the template to edit + */ +export function editGuildTemplate( + guildID: string, + code: string, + data: EditGuildTemplate, +) { + const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${code}`; + return RequestManager.patch(endpoint, data); +} + +export interface EditGuildTemplate { + /** name of the template (1-100 characters) */ + name?: string; + /** description for the template (0-120 characters) */ + description?: string | null; } From dd60de628c011c295453d8dfa1d32267148d593d Mon Sep 17 00:00:00 2001 From: ayntee Date: Sat, 14 Nov 2020 00:41:25 +0400 Subject: [PATCH 04/27] Rename code parameter to templateCode to reduce ambiguity --- src/handlers/guild.ts | 49 +++++++++++++------------------------------ 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index e3919cd22..979492b63 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -623,26 +623,21 @@ export function getGuild(guildID: string, counts = true) { ) as Promise; } -/** - * Returns the guild template if it exists - * @param guildID The ID of the guild - * @param code The code of the template to get - */ -export function getGuildTemplate(guildID: string, code: string) { - const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${code}`; +/** Returns the guild template if it exists */ +export function getGuildTemplate(guildID: string, templateCode: string) { + const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`; return RequestManager.get(endpoint); } /** * Create a new guild based on a template * NOTE: This endpoint can be used only by bots in less than 10 guilds. - * @param code The code of the template to create a guild from */ export function createGuildFromTemplate( - code: string, + templateCode: string, data: CreateGuildFromTemplate, ) { - return RequestManager.post(endpoints.GUILD_TEMPLATE(code), data); + return RequestManager.post(endpoints.GUILD_TEMPLATE(templateCode), data); } export interface CreateGuildFromTemplate { @@ -652,20 +647,14 @@ export interface CreateGuildFromTemplate { icon?: string; } -/** - * Returns an array of guild templates - * @param guildID The ID of the guild - */ +/** Returns an array of guild templates */ export function getGuildTemplates(guildID: string) { return RequestManager.get(endpoints.GUILD_TEMPLATES(guildID)); } -/** - * Deletes a template from a guild - * @param guildID The guild ID to delete the template from - */ -export function deleteGuildTemplate(guildID: string, code: string) { - const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${code}`; +/** Deletes a template from a guild */ +export function deleteGuildTemplate(guildID: string, templateCode: string) { + const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`; return RequestManager.delete(endpoint); } @@ -689,27 +678,19 @@ export interface CreateGuildTemplate { description?: string | null; } -/** - * Syncs the template to the guild's current state - * @param guildID The ID of the guild - * @param code The code of the template to sync - */ -export function syncGuildTemplate(guildID: string, code: string) { - const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${code}`; +/** Syncs the template to the guild's current state */ +export function syncGuildTemplate(guildID: string, templateCode: string) { + const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`; return RequestManager.put(endpoint); } -/** - * Edit a template's metadata - * @param guildID The ID of the guild - * @param code The code of the template to edit - */ +/** Edit a template's metadata */ export function editGuildTemplate( guildID: string, - code: string, + templateCode: string, data: EditGuildTemplate, ) { - const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${code}`; + const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`; return RequestManager.patch(endpoint, data); } From 9c5031259f7bf89b496e4190579389f5e8f86d7a Mon Sep 17 00:00:00 2001 From: ayntee Date: Sat, 14 Nov 2020 10:43:08 +0400 Subject: [PATCH 05/27] Embed endpoint into function instead of assigning to var --- src/handlers/guild.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 979492b63..df17cff35 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -625,8 +625,9 @@ export function getGuild(guildID: string, counts = true) { /** Returns the guild template if it exists */ export function getGuildTemplate(guildID: string, templateCode: string) { - const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`; - return RequestManager.get(endpoint); + return RequestManager.get( + `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, + ); } /** @@ -654,8 +655,9 @@ export function getGuildTemplates(guildID: string) { /** Deletes a template from a guild */ export function deleteGuildTemplate(guildID: string, templateCode: string) { - const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`; - return RequestManager.delete(endpoint); + return RequestManager.delete( + `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, + ); } /** @@ -680,8 +682,9 @@ export interface CreateGuildTemplate { /** Syncs the template to the guild's current state */ export function syncGuildTemplate(guildID: string, templateCode: string) { - const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`; - return RequestManager.put(endpoint); + return RequestManager.put( + `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, + ); } /** Edit a template's metadata */ @@ -690,8 +693,10 @@ export function editGuildTemplate( templateCode: string, data: EditGuildTemplate, ) { - const endpoint = `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`; - return RequestManager.patch(endpoint, data); + return RequestManager.patch( + `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, + data, + ); } export interface EditGuildTemplate { From d31cf09c64a2927c22e2979b7b128026a1c43c12 Mon Sep 17 00:00:00 2001 From: ayntee Date: Sat, 14 Nov 2020 10:44:58 +0400 Subject: [PATCH 06/27] Better types for getGuildTemplate() --- src/handlers/guild.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index df17cff35..1d227f79d 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -24,6 +24,7 @@ import { PositionSwap, PruneOptions, PrunePayload, + Template, UpdateGuildPayload, UserPayload, } from "../types/guild.ts"; @@ -624,10 +625,13 @@ export function getGuild(guildID: string, counts = true) { } /** Returns the guild template if it exists */ -export function getGuildTemplate(guildID: string, templateCode: string) { +export function getGuildTemplate( + guildID: string, + templateCode: string, +): Promise