From 091b4fc21445c7415a250d48de11ae098299e6c7 Mon Sep 17 00:00:00 2001 From: Kyra Date: Tue, 28 Aug 2018 17:33:51 +0200 Subject: [PATCH] backport: Guild#{fetchEmbed,setEmbed} (#2778) * backport: Guild Embeds * fix: Added missing return * docs: Updated typings --- src/client/rest/RESTMethods.js | 14 ++++++++++++++ src/structures/Guild.js | 31 +++++++++++++++++++++++++++++++ typings/index.d.ts | 7 +++++++ 3 files changed, 52 insertions(+) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 0d2f7fcdb1d0..0919d1b40c11 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -59,6 +59,13 @@ class RESTMethods { }); } + fetchEmbed(guildID) { + return this.rest.makeRequest('get', Endpoints.Guild(guildID).embed, true).then(data => ({ + enabled: data.enabled, + channel: data.channel_id ? this.client.channels.get(data.channel_id) : null, + })); + } + sendMessage(channel, content, { tts, nonce, embed, disableEveryone, split, code, reply } = {}, files = null) { return new Promise((resolve, reject) => { // eslint-disable-line complexity if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content); @@ -853,6 +860,13 @@ class RESTMethods { ); } + updateEmbed(guildID, embed, reason) { + return this.rest.makeRequest('patch', Endpoints.Guild(guildID).embed, true, { + enabled: embed.enabled, + channel_id: this.client.resolver.resolveChannelID(embed.channel), + }, undefined, reason); + } + setRolePositions(guildID, roles) { return this.rest.makeRequest('patch', Endpoints.Guild(guildID).roles, true, roles).then(() => this.client.actions.GuildRolesPositionUpdate.handle({ diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 1be2fb3cb249..f983027792b7 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -530,6 +530,26 @@ class Guild { return this.client.rest.methods.fetchVoiceRegions(this.id); } + /** + * The Guild Embed object + * @typedef {Object} GuildEmbedData + * @property {boolean} enabled Whether the embed is enabled + * @property {?ChannelResolvable} channel The embed channel + */ + + /** + * Fetches the guild embed. + * @returns {Promise} + * @example + * // Fetches the guild embed + * guild.fetchEmbed() + * .then(embed => console.log(`The embed is ${embed.enabled ? 'enabled' : 'disabled'}`)) + * .catch(console.error); + */ + fetchEmbed() { + return this.client.rest.methods.fetchEmbed(this.id); + } + /** * Fetch audit logs for this guild. * @param {Object} [options={}] Options for fetching audit logs @@ -1025,6 +1045,17 @@ class Guild { return this.client.rest.methods.updateChannelPositions(this.id, channelPositions); } + /** + * Edits the guild's embed. + * @param {GuildEmbedData} embed The embed for the guild + * @param {string} [reason] Reason for changing the guild's embed + * @returns {Promise} + */ + setEmbed(embed, reason) { + return this.client.rest.methods.updateEmbed(this.id, embed, reason) + .then(() => this); + } + /** * Creates a new role in the guild with given information. * @param {RoleData} [data] The data to update the role with diff --git a/typings/index.d.ts b/typings/index.d.ts index 65151e36bd49..4e13377a1bf6 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -533,6 +533,7 @@ declare module 'discord.js' { public equals(guild: Guild): boolean; public fetchAuditLogs(options?: GuildAuditLogsFetchOptions): Promise; public fetchBans(): Promise>; + public fetchEmbed(): Promise; public fetchInvites(): Promise>; public fetchMember(user: UserResolvable, cache?: boolean): Promise; public fetchMembers(query?: string, limit?: number): Promise; @@ -547,6 +548,7 @@ declare module 'discord.js' { public setChannelPosition(channel: string | GuildChannel, position: number, relative?: boolean): Promise; public setChannelPositions(channelPositions: ChannelPosition[]): Promise; public setDefaultMessageNotification(defaultMessageNotifications: DefaultMessageNotifications, reason: string): Promise; + public setEmbed(embed: GuildEmbedData, reason?: string): Promise; public setExcplicitContentFilter(explicitContentFilter: number, reason?: string): Promise; public setIcon(icon: Base64Resolvable, reason?: string): Promise; public setName(name: string, reason?: string): Promise; @@ -1787,6 +1789,11 @@ declare module 'discord.js' { splash?: Base64Resolvable; }; + type GuildEmbedData = { + enabled: boolean; + channel: ChannelResolvable; + }; + type GuildMemberEditData = { nick?: string; roles?: Collection | Role[] | Snowflake[];