-
-
Notifications
You must be signed in to change notification settings - Fork 4k
feat: add support for guild templates #4907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9c2edfb
feat(Constants): add 'template' to DefaultOptions.http
izexi f6e4c42
feat(DataResolver): add 'resolveCode' method
izexi 38272d9
refactor(DataResolver): use 'resolveCode' method
izexi 7d3d049
docs(DataResolver): add 'GuildTemplateResolvable' typedef
izexi 5859b25
feat(DataResolver): add 'resolveGuildTemplateCode' method
izexi 6b7fa3f
feat(Structures): create 'GuildTemplate' class
izexi 92f5777
feat(Client): add 'fetchGuildTemplate' method
izexi 07f8a21
chore: add 'GuildTemplate' to exports
izexi e3d3009
feat(GuildTemplate): add 'createGuild' method
izexi 32bdf8d
chore: add 'GuildTemplate' to ESM exports
izexi 8435a16
fix(GuildTemplate): casing on 'usageCount'
izexi ed88644
typings: add GuildTemplate class & Client#fetchGuildTemplate()
izexi cb77eb1
feat(GuildTemplate): add 'edit/delete/sync' methods
izexi d24829a
feat(Guild): add 'createTemplate/fetchTemplates' methods
izexi 39062b3
refactor(GuildTemplate): name param can now be optional on 'edit'
izexi ecc36df
typings(GuildTemplate): remove '_patch' method
izexi cfa9e81
Merge branch 'master' into unmerged-guild-template
izexi 4817df1
Merge branch 'master' into unmerged-guild-template
iCrawl 4841917
fix: implement requested changes from @SpaceEEC
izexi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ export const { | |
| GuildEmoji, | ||
| GuildMember, | ||
| GuildPreview, | ||
| GuildTemplate, | ||
| Integration, | ||
| Invite, | ||
| Message, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| 'use strict'; | ||
|
|
||
| const Base = require('./Base'); | ||
| const { Events } = require('../util/Constants'); | ||
| const DataResolver = require('../util/DataResolver'); | ||
|
|
||
| /** | ||
| * Represents the template for a guild. | ||
| * @extends {Base} | ||
| */ | ||
| class GuildTemplate extends Base { | ||
| /** | ||
| * @param {Client} client The instantiating client | ||
| * @param {Object} data The raw data for the template | ||
| */ | ||
| constructor(client, data) { | ||
| super(client); | ||
| this._patch(data); | ||
| } | ||
|
|
||
| /** | ||
| * Builds or updates the template with the provided data. | ||
| * @param {Object} data The raw data for the template | ||
| * @returns {GuildTemplate} | ||
| * @private | ||
| */ | ||
| _patch(data) { | ||
| /** | ||
| * The unique code of this template | ||
| * @type {string} | ||
| */ | ||
| this.code = data.code; | ||
|
|
||
| /** | ||
| * The name of this template | ||
| * @type {string} | ||
| */ | ||
| this.name = data.name; | ||
|
|
||
| /** | ||
| * The description of this template | ||
| * @type {?string} | ||
| */ | ||
| this.description = data.description; | ||
|
|
||
| /** | ||
| * The amount of times this template has been used | ||
| * @type {number} | ||
| */ | ||
| this.usageCount = data.usage_count; | ||
|
|
||
| /** | ||
| * The ID of the user that created this template | ||
| * @type {Snowflake} | ||
| */ | ||
| this.creatorID = data.creator_id; | ||
|
|
||
| /** | ||
| * The user that created this template | ||
| * @type {User} | ||
| */ | ||
| this.creator = this.client.users.add(data.creator); | ||
|
|
||
| /** | ||
| * The time of when this template was created at | ||
| * @type {Date} | ||
| */ | ||
| this.createdAt = new Date(data.created_at); | ||
|
|
||
| /** | ||
| * The time of when this template was last synced to the guild | ||
| * @type {Date} | ||
| */ | ||
| this.updatedAt = new Date(data.updated_at); | ||
izexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * The ID of the guild that this template belongs to | ||
| * @type {Snowflake} | ||
| */ | ||
| this.guildID = data.source_guild_id; | ||
izexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * The data of the guild that this template would create | ||
| * @type {Object} | ||
| * @see {@link https://discord.com/developers/docs/resources/guild#guild-resource} | ||
| */ | ||
| this.serializedGuild = data.serialized_source_guild; | ||
|
|
||
| /** | ||
| * Whether this template has unsynced changes | ||
| * @type {?boolean} | ||
| */ | ||
| this.unSynced = 'is_dirty' in data ? Boolean(data.is_dirty) : null; | ||
|
|
||
| return this; | ||
izexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Creates a guild based from this template. | ||
| * <warn>This is only available to bots in fewer than 10 guilds.</warn> | ||
| * @param {string} name The name of the guild | ||
| * @param {BufferResolvable|Base64Resolvable} [icon] The icon for the guild | ||
| * @returns {Promise<Guild>} | ||
| */ | ||
| async createGuild(name, icon) { | ||
| const { client } = this; | ||
| const data = await client.api.guilds.templates(this.code).post({ | ||
| data: { | ||
| name, | ||
| icon: await DataResolver.resolveImage(icon), | ||
| }, | ||
| }); | ||
| // eslint-disable-next-line consistent-return | ||
| return new Promise(resolve => { | ||
| const createdGuild = client.guilds.cache.get(data.id); | ||
| if (createdGuild) return resolve(createdGuild); | ||
|
|
||
| const resolveGuild = guild => { | ||
| client.off(Events.GUILD_CREATE, handleGuild); | ||
| client.decrementMaxListeners(); | ||
| resolve(guild); | ||
| }; | ||
|
|
||
| const handleGuild = guild => { | ||
| if (guild.id === data.id) { | ||
| client.clearTimeout(timeout); | ||
| resolveGuild(guild); | ||
| } | ||
| }; | ||
|
|
||
| client.incrementMaxListeners(); | ||
| client.on(Events.GUILD_CREATE, handleGuild); | ||
|
|
||
| const timeout = client.setTimeout(() => resolveGuild(client.guilds.add(data)), 10000); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Updates the metadata on this template. | ||
| * @param {Object} options Options for the template | ||
| * @param {string} [options.name] The name of this template | ||
| * @param {string} [options.description] The description of this template | ||
| * @returns {Promise<GuildTemplate>} | ||
| */ | ||
| edit({ name, description } = {}) { | ||
| return this.client.api | ||
| .guilds(this.guildID) | ||
| .templates(this.code) | ||
| .patch({ data: { name, description } }) | ||
| .then(data => this._patch(data)); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes this template. | ||
| * @returns {Promise<GuildTemplate>} | ||
| */ | ||
| delete() { | ||
| return this.client.api | ||
| .guilds(this.guildID) | ||
| .templates(this.code) | ||
| .delete() | ||
| .then(() => this); | ||
| } | ||
|
|
||
| /** | ||
| * Syncs this template to the current state of the guild. | ||
| * @returns {Promise<GuildTemplate>} | ||
| */ | ||
| sync() { | ||
| return this.client.api | ||
| .guilds(this.guildID) | ||
| .templates(this.code) | ||
| .put() | ||
| .then(data => this._patch(data)); | ||
| } | ||
|
|
||
| /** | ||
| * The timestamp of when this template was created at | ||
| * @type {number} | ||
| * @readonly | ||
| */ | ||
| get createdTimestamp() { | ||
| return this.createdAt.getTime(); | ||
| } | ||
|
|
||
| /** | ||
| * The timestamp of when this template was last synced to the guild | ||
| * @type {number} | ||
| * @readonly | ||
| */ | ||
| get updatedTimestamp() { | ||
| return this.updatedAt.getTime(); | ||
| } | ||
|
|
||
| /** | ||
| * The guild that this template belongs to | ||
| * @type {?Guild} | ||
| * @readonly | ||
| */ | ||
| get guild() { | ||
| return this.client.guilds.get(this.guildID) || null; | ||
| } | ||
| /** | ||
| * The URL of this template | ||
| * @type {string} | ||
| * @readonly | ||
| */ | ||
| get url() { | ||
| return `${this.client.options.http.template}/${this.code}`; | ||
| } | ||
|
|
||
| /** | ||
| * When concatenated with a string, this automatically returns the templates's code instead of the template object. | ||
| * @returns {string} | ||
| * @example | ||
| * // Logs: Template: FKvmczH2HyUf | ||
| * console.log(`Template: ${guildTemplate}!`); | ||
| */ | ||
| toString() { | ||
| return this.code; | ||
| } | ||
| } | ||
|
|
||
| module.exports = GuildTemplate; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.