2020-12-25 10:21:01 +00:00
|
|
|
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 {
|
2020-12-25 11:20:48 +00:00
|
|
|
/** The template code (unique ID) */
|
2020-12-25 10:21:01 +00:00
|
|
|
code: string
|
2020-12-25 11:20:48 +00:00
|
|
|
/** The template name */
|
2020-12-25 10:21:01 +00:00
|
|
|
name: string
|
2020-12-25 11:20:48 +00:00
|
|
|
/** The description for the template */
|
2020-12-25 10:21:01 +00:00
|
|
|
description: string | null
|
2020-12-25 11:20:48 +00:00
|
|
|
/** Number of times this template has been used */
|
2020-12-25 10:21:01 +00:00
|
|
|
usageCount: number
|
2020-12-25 11:20:48 +00:00
|
|
|
/** The ID of the user who created the template */
|
2020-12-25 10:21:01 +00:00
|
|
|
creatorID: string
|
2020-12-25 11:20:48 +00:00
|
|
|
/** The user who created the template */
|
2020-12-25 10:21:01 +00:00
|
|
|
creator: User
|
2020-12-25 11:20:48 +00:00
|
|
|
/** When this template was created (in ms) */
|
2020-12-25 10:21:01 +00:00
|
|
|
createdAt: number
|
2020-12-25 11:20:48 +00:00
|
|
|
/** When this template was last synced to the source guild (in ms) */
|
2020-12-25 10:21:01 +00:00
|
|
|
updatedAt: number
|
2020-12-25 11:20:48 +00:00
|
|
|
/** The ID of the guild this template is based on */
|
2020-12-25 10:21:01 +00:00
|
|
|
sourceGuildID: string
|
2020-12-25 11:20:48 +00:00
|
|
|
/** The guild snapshot this template contains */
|
2020-12-25 10:21:01 +00:00
|
|
|
serializedSourceGuild: Guild
|
2020-12-25 11:20:48 +00:00
|
|
|
/** Whether the template has unsynced changes */
|
2020-12-25 10:21:01 +00:00
|
|
|
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. */
|
2020-12-25 10:59:24 +00:00
|
|
|
async edit(data: ModifyGuildTemplateParams): Promise<Template> {
|
2020-12-25 10:21:01 +00:00
|
|
|
const res = await this.client.rest.patch(TEMPLATE(this.code), data)
|
2020-12-25 10:59:24 +00:00
|
|
|
return new Template(this.client, res)
|
2020-12-25 10:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Deletes the template. Requires the MANAGE_GUILD permission. Returns the deleted template object on success. */
|
2020-12-25 10:59:24 +00:00
|
|
|
async delete(): Promise<Template> {
|
2020-12-25 10:21:01 +00:00
|
|
|
const res = await this.client.rest.delete(TEMPLATE(this.code))
|
2020-12-25 10:59:24 +00:00
|
|
|
return new Template(this.client, res)
|
2020-12-25 10:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Syncs the template to the guild's current state. Requires the MANAGE_GUILD permission. Returns the template object on success. */
|
2020-12-25 10:59:24 +00:00
|
|
|
async sync(): Promise<Template> {
|
2020-12-25 10:21:01 +00:00
|
|
|
const res = await this.client.rest.put(TEMPLATE(this.code))
|
2020-12-25 10:59:24 +00:00
|
|
|
return new Template(this.client, res)
|
2020-12-25 10:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** https://discord.com/developers/docs/resources/template#modify-guild-template-json-params */
|
|
|
|
export interface ModifyGuildTemplateParams {
|
|
|
|
name?: string
|
|
|
|
description?: string | null
|
|
|
|
}
|