From d1fcce7b839246fba89484538cf187dc72f25812 Mon Sep 17 00:00:00 2001 From: ayntee Date: Fri, 25 Dec 2020 14:21:01 +0400 Subject: [PATCH 1/3] feat: add template structure and methods --- src/structures/template.ts | 70 ++++++++++++++++++++++++++++++++++++++ src/types/template.ts | 4 +-- 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/structures/template.ts diff --git a/src/structures/template.ts b/src/structures/template.ts new file mode 100644 index 0000000..22224b0 --- /dev/null +++ b/src/structures/template.ts @@ -0,0 +1,70 @@ +import { Client } from '../models/client.ts' +import { TEMPLATE } from '../types/endpoint.ts' +import { TemplatePayload } from '../types/template.ts' +import { Base } from './base.ts' +import { Guild } from './guild.ts' +import { User } from './user.ts' + +export class Template extends Base { + /** 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 */ + usageCount: number + /** the ID of the user who created the template */ + creatorID: string + /** the user who created the template */ + creator: User + /** when this template was created (in ms) */ + createdAt: number + /** when this template was last synced to the source guild (in ms) */ + updatedAt: number + /** the ID of the guild this template is based on */ + sourceGuildID: string + /** the guild snapshot this template contains */ + serializedSourceGuild: Guild + /** whether the template has unsynced changes */ + isDirty: boolean | null + + constructor(client: Client, data: TemplatePayload) { + super(client, data) + this.code = data.code + this.name = data.name + this.description = data.description + this.usageCount = data.usage_count + this.creatorID = data.creator_id + this.creator = new User(client, data.creator) + this.createdAt = Date.parse(data.created_at) + this.updatedAt = Date.parse(data.updated_at) + this.sourceGuildID = data.source_guild_id + this.serializedSourceGuild = new Guild(client, data.serialized_source_guild) + this.isDirty = Boolean(data.is_dirty) + } + + /** Modifies the template's metadata. Requires the MANAGE_GUILD permission. Returns the template object on success. */ + async edit(data: ModifyGuildTemplateParams) { + const res = await this.client.rest.patch(TEMPLATE(this.code), data) + return res + } + + /** Deletes the template. Requires the MANAGE_GUILD permission. Returns the deleted template object on success. */ + async delete() { + const res = await this.client.rest.delete(TEMPLATE(this.code)) + return res + } + + /** Syncs the template to the guild's current state. Requires the MANAGE_GUILD permission. Returns the template object on success. */ + async sync() { + const res = await this.client.rest.put(TEMPLATE(this.code)) + return res + } +} + +/** https://discord.com/developers/docs/resources/template#modify-guild-template-json-params */ +export interface ModifyGuildTemplateParams { + name?: string + description?: string | null +} diff --git a/src/types/template.ts b/src/types/template.ts index 8128515..05fb0a8 100644 --- a/src/types/template.ts +++ b/src/types/template.ts @@ -4,7 +4,7 @@ import { UserPayload } from './user.ts' export interface TemplatePayload { code: string name: string - description: string | undefined + description: string | null usage_count: number creator_id: string creator: UserPayload @@ -12,5 +12,5 @@ export interface TemplatePayload { updated_at: string source_guild_id: string serialized_source_guild: GuildPayload - is_dirty: boolean | undefined + is_dirty: boolean | null } From be662a230b98ad0ae56308f32ebdcf5f2e833665 Mon Sep 17 00:00:00 2001 From: ayntee Date: Fri, 25 Dec 2020 14:59:24 +0400 Subject: [PATCH 2/3] chore: add return type and handle raw API payload --- src/structures/template.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/structures/template.ts b/src/structures/template.ts index 22224b0..b59d7c9 100644 --- a/src/structures/template.ts +++ b/src/structures/template.ts @@ -45,21 +45,21 @@ export class Template extends Base { } /** Modifies the template's metadata. Requires the MANAGE_GUILD permission. Returns the template object on success. */ - async edit(data: ModifyGuildTemplateParams) { + async edit(data: ModifyGuildTemplateParams): Promise