|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Base = require('./Base'); |
| 4 | +const { Events } = require('../util/Constants'); |
| 5 | +const DataResolver = require('../util/DataResolver'); |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents the template for a guild. |
| 9 | + * @extends {Base} |
| 10 | + */ |
| 11 | +class GuildTemplate extends Base { |
| 12 | + /** |
| 13 | + * @param {Client} client The instantiating client |
| 14 | + * @param {Object} data The raw data for the template |
| 15 | + */ |
| 16 | + constructor(client, data) { |
| 17 | + super(client); |
| 18 | + this._patch(data); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Builds or updates the template with the provided data. |
| 23 | + * @param {Object} data The raw data for the template |
| 24 | + * @returns {GuildTemplate} |
| 25 | + * @private |
| 26 | + */ |
| 27 | + _patch(data) { |
| 28 | + /** |
| 29 | + * The unique code of this template |
| 30 | + * @type {string} |
| 31 | + */ |
| 32 | + this.code = data.code; |
| 33 | + |
| 34 | + /** |
| 35 | + * The name of this template |
| 36 | + * @type {string} |
| 37 | + */ |
| 38 | + this.name = data.name; |
| 39 | + |
| 40 | + /** |
| 41 | + * The description of this template |
| 42 | + * @type {?string} |
| 43 | + */ |
| 44 | + this.description = data.description; |
| 45 | + |
| 46 | + /** |
| 47 | + * The amount of times this template has been used |
| 48 | + * @type {number} |
| 49 | + */ |
| 50 | + this.usageCount = data.usage_count; |
| 51 | + |
| 52 | + /** |
| 53 | + * The ID of the user that created this template |
| 54 | + * @type {Snowflake} |
| 55 | + */ |
| 56 | + this.creatorID = data.creator_id; |
| 57 | + |
| 58 | + /** |
| 59 | + * The user that created this template |
| 60 | + * @type {User} |
| 61 | + */ |
| 62 | + this.creator = this.client.users.add(data.creator); |
| 63 | + |
| 64 | + /** |
| 65 | + * The time of when this template was created at |
| 66 | + * @type {Date} |
| 67 | + */ |
| 68 | + this.createdAt = new Date(data.created_at); |
| 69 | + |
| 70 | + /** |
| 71 | + * The time of when this template was last synced to the guild |
| 72 | + * @type {Date} |
| 73 | + */ |
| 74 | + this.updatedAt = new Date(data.updated_at); |
| 75 | + |
| 76 | + /** |
| 77 | + * The ID of the guild that this template belongs to |
| 78 | + * @type {Snowflake} |
| 79 | + */ |
| 80 | + this.guildID = data.source_guild_id; |
| 81 | + |
| 82 | + /** |
| 83 | + * The data of the guild that this template would create |
| 84 | + * @type {Object} |
| 85 | + * @see {@link https://discord.com/developers/docs/resources/guild#guild-resource} |
| 86 | + */ |
| 87 | + this.serializedGuild = data.serialized_source_guild; |
| 88 | + |
| 89 | + /** |
| 90 | + * Whether this template has unsynced changes |
| 91 | + * @type {?boolean} |
| 92 | + */ |
| 93 | + this.unSynced = 'is_dirty' in data ? Boolean(data.is_dirty) : null; |
| 94 | + |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates a guild based from this template. |
| 100 | + * <warn>This is only available to bots in fewer than 10 guilds.</warn> |
| 101 | + * @param {string} name The name of the guild |
| 102 | + * @param {BufferResolvable|Base64Resolvable} [icon] The icon for the guild |
| 103 | + * @returns {Promise<Guild>} |
| 104 | + */ |
| 105 | + async createGuild(name, icon) { |
| 106 | + const { client } = this; |
| 107 | + const data = await client.api.guilds.templates(this.code).post({ |
| 108 | + data: { |
| 109 | + name, |
| 110 | + icon: await DataResolver.resolveImage(icon), |
| 111 | + }, |
| 112 | + }); |
| 113 | + // eslint-disable-next-line consistent-return |
| 114 | + return new Promise(resolve => { |
| 115 | + const createdGuild = client.guilds.cache.get(data.id); |
| 116 | + if (createdGuild) return resolve(createdGuild); |
| 117 | + |
| 118 | + const resolveGuild = guild => { |
| 119 | + client.off(Events.GUILD_CREATE, handleGuild); |
| 120 | + client.decrementMaxListeners(); |
| 121 | + resolve(guild); |
| 122 | + }; |
| 123 | + |
| 124 | + const handleGuild = guild => { |
| 125 | + if (guild.id === data.id) { |
| 126 | + client.clearTimeout(timeout); |
| 127 | + resolveGuild(guild); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + client.incrementMaxListeners(); |
| 132 | + client.on(Events.GUILD_CREATE, handleGuild); |
| 133 | + |
| 134 | + const timeout = client.setTimeout(() => resolveGuild(client.guilds.add(data)), 10000); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Updates the metadata on this template. |
| 140 | + * @param {Object} options Options for the template |
| 141 | + * @param {string} [options.name] The name of this template |
| 142 | + * @param {string} [options.description] The description of this template |
| 143 | + * @returns {Promise<GuildTemplate>} |
| 144 | + */ |
| 145 | + edit({ name, description } = {}) { |
| 146 | + return this.client.api |
| 147 | + .guilds(this.guildID) |
| 148 | + .templates(this.code) |
| 149 | + .patch({ data: { name, description } }) |
| 150 | + .then(data => this._patch(data)); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Deletes this template. |
| 155 | + * @returns {Promise<GuildTemplate>} |
| 156 | + */ |
| 157 | + delete() { |
| 158 | + return this.client.api |
| 159 | + .guilds(this.guildID) |
| 160 | + .templates(this.code) |
| 161 | + .delete() |
| 162 | + .then(() => this); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Syncs this template to the current state of the guild. |
| 167 | + * @returns {Promise<GuildTemplate>} |
| 168 | + */ |
| 169 | + sync() { |
| 170 | + return this.client.api |
| 171 | + .guilds(this.guildID) |
| 172 | + .templates(this.code) |
| 173 | + .put() |
| 174 | + .then(data => this._patch(data)); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * The timestamp of when this template was created at |
| 179 | + * @type {number} |
| 180 | + * @readonly |
| 181 | + */ |
| 182 | + get createdTimestamp() { |
| 183 | + return this.createdAt.getTime(); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * The timestamp of when this template was last synced to the guild |
| 188 | + * @type {number} |
| 189 | + * @readonly |
| 190 | + */ |
| 191 | + get updatedTimestamp() { |
| 192 | + return this.updatedAt.getTime(); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * The guild that this template belongs to |
| 197 | + * @type {?Guild} |
| 198 | + * @readonly |
| 199 | + */ |
| 200 | + get guild() { |
| 201 | + return this.client.guilds.get(this.guildID) || null; |
| 202 | + } |
| 203 | + /** |
| 204 | + * The URL of this template |
| 205 | + * @type {string} |
| 206 | + * @readonly |
| 207 | + */ |
| 208 | + get url() { |
| 209 | + return `${this.client.options.http.template}/${this.code}`; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * When concatenated with a string, this automatically returns the templates's code instead of the template object. |
| 214 | + * @returns {string} |
| 215 | + * @example |
| 216 | + * // Logs: Template: FKvmczH2HyUf |
| 217 | + * console.log(`Template: ${guildTemplate}!`); |
| 218 | + */ |
| 219 | + toString() { |
| 220 | + return this.code; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +module.exports = GuildTemplate; |
0 commit comments