From fbb1253b3f714063d4244ab5ba5a5e2e06cbfc71 Mon Sep 17 00:00:00 2001 From: Isabella Date: Fri, 25 Aug 2017 15:17:38 -0500 Subject: [PATCH] Added resolveImage to reduce code duplication (#1820) * add client#resolveImage * oops * resolveFile fix * async is the future * async * update doc example * build fix * doc fix * fix docs * thx hydar --- src/client/ClientDataResolver.js | 14 ++++++++++++++ src/structures/ClientUser.js | 13 ++++--------- src/structures/GroupDMChannel.js | 13 +++---------- src/structures/Guild.js | 22 +++++++++++----------- src/structures/TextChannel.js | 2 +- src/structures/Webhook.js | 2 +- 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index 687e0afe0..3241ad2c2 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -177,6 +177,20 @@ class ClientDataResolver { return data; } + /** + * Resolves a Base64Resolvable, a string, or a BufferResolvable to a Base 64 image. + * @param {BufferResolvable|Base64Resolvable} image The image to be resolved + * @returns {Promise} + */ + async resolveImage(image) { + if (!image) return null; + if (typeof image === 'string' && image.startsWith('data:')) { + return image; + } + const file = await this.resolveFile(image); + return this.resolveBase64(file); + } + /** * Data that resolves to give a Base64 string, typically for image uploading. This can be: * * A Buffer diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index b6a4a7912..4ea3dcee3 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -146,7 +146,7 @@ class ClientUser extends User { * Changes the password for the client user's account. * This is only available when using a user account. * @param {string} newPassword New password to change to - * @param {Object|string} options Object containing an MFA code, password or both. + * @param {Object|string} options Object containing an MFA code, password or both. * Can be just a string for the password. * @param {string} [options.oldPassword] Current password * @param {string} [options.mfaCode] Timed MFA Code @@ -171,13 +171,8 @@ class ClientUser extends User { * .then(user => console.log(`New avatar set!`)) * .catch(console.error); */ - setAvatar(avatar) { - if (typeof avatar === 'string' && avatar.startsWith('data:')) { - return this.edit({ avatar }); - } else { - return this.client.resolver.resolveBuffer(avatar || Buffer.alloc(0)) - .then(data => this.edit({ avatar: this.client.resolver.resolveBase64(data) || null })); - } + async setAvatar(avatar) { + return this.edit({ avatar: await this.client.resolver.resolveImage(avatar) }); } /** @@ -347,7 +342,7 @@ class ClientUser extends User { }, reject) ); } else { - return this.client.resolver.resolveBuffer(icon) + return this.client.resolver.resolveFile(icon) .then(data => this.createGuild(name, { region, icon: this.client.resolver.resolveBase64(data) || null })); } } diff --git a/src/structures/GroupDMChannel.js b/src/structures/GroupDMChannel.js index 6c05ac09b..b7d12e6d6 100644 --- a/src/structures/GroupDMChannel.js +++ b/src/structures/GroupDMChannel.js @@ -157,18 +157,11 @@ class GroupDMChannel extends Channel { /** * Sets a new icon for this Group DM. - * @param {Base64Resolvable} icon The new icon of this Group DM + * @param {Base64Resolvable|BufferResolvable} icon The new icon of this Group DM * @returns {Promise} */ - setIcon(icon) { - if (typeof icon === 'string' && icon.startsWith('data:')) { - return this.edit({ icon }); - } else if (!icon) { - return this.edit({ icon: null }); - } else { - return this.client.resolver.resolveBuffer(icon) - .then(data => this.edit({ icon: this.client.resolver.resolveBase64(data) })); - } + async setIcon(icon) { + return this.edit({ icon: await this.client.resolver.resolveImage(icon) }); } /** diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 145a0422c..a4631632b 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -661,9 +661,9 @@ class Guild extends Base { if (data.afkChannel) _data.afk_channel_id = this.client.resolver.resolveChannel(data.afkChannel).id; if (data.systemChannel) _data.system_channel_id = this.client.resolver.resolveChannel(data.systemChannel).id; if (data.afkTimeout) _data.afk_timeout = Number(data.afkTimeout); - if (data.icon) _data.icon = this.client.resolver.resolveBase64(data.icon); + if (data.icon) _data.icon = data.icon; if (data.owner) _data.owner_id = this.client.resolver.resolveUser(data.owner).id; - if (data.splash) _data.splash = this.client.resolver.resolveBase64(data.splash); + if (data.splash) _data.splash = data.splash; if (typeof data.explicitContentFilter !== 'undefined') { _data.explicit_content_filter = Number(data.explicitContentFilter); } @@ -768,17 +768,17 @@ class Guild extends Base { /** * Set a new guild icon. - * @param {Base64Resolvable} icon The new icon of the guild + * @param {Base64Resolvable|BufferResolvable} icon The new icon of the guild * @param {string} [reason] Reason for changing the guild's icon * @returns {Promise} * @example * // Edit the guild icon - * guild.setIcon(fs.readFileSync('./icon.png')) + * guild.setIcon('./icon.png') * .then(updated => console.log('Updated the guild icon')) * .catch(console.error); */ - setIcon(icon, reason) { - return this.edit({ icon }, reason); + async setIcon(icon, reason) { + return this.edit({ icon: await this.client.resolver.resolveImage(icon), reason }); } /** @@ -798,17 +798,17 @@ class Guild extends Base { /** * Set a new guild splash screen. - * @param {Base64Resolvable} splash The new splash screen of the guild + * @param {Base64Resolvable|BufferResolvable} splash The new splash screen of the guild * @param {string} [reason] Reason for changing the guild's splash screen * @returns {Promise} * @example * // Edit the guild splash - * guild.setIcon(fs.readFileSync('./splash.png')) + * guild.setSplash('./splash.png') * .then(updated => console.log('Updated the guild splash')) * .catch(console.error); */ - setSplash(splash, reason) { - return this.edit({ splash }, reason); + async setSplash(splash, reason) { + return this.edit({ splash: await this.client.resolver.resolveImage(splash), reason }); } /** @@ -1102,7 +1102,7 @@ class Guild extends Base { .then(emoji => this.client.actions.GuildEmojiCreate.handle(this, emoji).emoji); } - return this.client.resolver.resolveBuffer(attachment) + return this.client.resolver.resolveFile(attachment) .then(data => { const dataURI = this.client.resolver.resolveBase64(data); return this.createEmoji(dataURI, name, { roles, reason }); diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index 693bd35eb..6e6b44511 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -65,7 +65,7 @@ class TextChannel extends GuildChannel { name, avatar, }, reason }).then(data => new Webhook(this.client, data)); } else { - return this.client.resolver.resolveBuffer(avatar).then(data => + return this.client.resolver.resolveFile(avatar).then(data => this.createWebhook(name, this.client.resolver.resolveBase64(data) || null)); } } diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index ae50a3ff7..770ec0dbd 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -254,7 +254,7 @@ class Webhook { */ edit({ name = this.name, avatar }, reason) { if (avatar && (typeof avatar === 'string' && !avatar.startsWith('data:'))) { - return this.client.resolver.resolveBuffer(avatar).then(file => { + return this.client.resolver.resolveFile(avatar).then(file => { const dataURI = this.client.resolver.resolveBase64(file); return this.edit({ name, avatar: dataURI }, reason); });