mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
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
This commit is contained in:
@@ -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<string>}
|
||||
*/
|
||||
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
|
||||
|
||||
@@ -146,7 +146,7 @@ class ClientUser extends User {
|
||||
* Changes the password for the client user's account.
|
||||
* <warn>This is only available when using a user account.</warn>
|
||||
* @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 }));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<GroupDMChannel>}
|
||||
*/
|
||||
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) });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<Guild>}
|
||||
* @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<Guild>}
|
||||
* @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 });
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user