mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 18:43:31 +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;
|
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:
|
* Data that resolves to give a Base64 string, typically for image uploading. This can be:
|
||||||
* * A Buffer
|
* * A Buffer
|
||||||
|
|||||||
@@ -171,13 +171,8 @@ class ClientUser extends User {
|
|||||||
* .then(user => console.log(`New avatar set!`))
|
* .then(user => console.log(`New avatar set!`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
setAvatar(avatar) {
|
async setAvatar(avatar) {
|
||||||
if (typeof avatar === 'string' && avatar.startsWith('data:')) {
|
return this.edit({ avatar: await this.client.resolver.resolveImage(avatar) });
|
||||||
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 }));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -347,7 +342,7 @@ class ClientUser extends User {
|
|||||||
}, reject)
|
}, reject)
|
||||||
);
|
);
|
||||||
} else {
|
} 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 }));
|
.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.
|
* 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>}
|
* @returns {Promise<GroupDMChannel>}
|
||||||
*/
|
*/
|
||||||
setIcon(icon) {
|
async setIcon(icon) {
|
||||||
if (typeof icon === 'string' && icon.startsWith('data:')) {
|
return this.edit({ icon: await this.client.resolver.resolveImage(icon) });
|
||||||
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) }));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -661,9 +661,9 @@ class Guild extends Base {
|
|||||||
if (data.afkChannel) _data.afk_channel_id = this.client.resolver.resolveChannel(data.afkChannel).id;
|
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.systemChannel) _data.system_channel_id = this.client.resolver.resolveChannel(data.systemChannel).id;
|
||||||
if (data.afkTimeout) _data.afk_timeout = Number(data.afkTimeout);
|
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.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') {
|
if (typeof data.explicitContentFilter !== 'undefined') {
|
||||||
_data.explicit_content_filter = Number(data.explicitContentFilter);
|
_data.explicit_content_filter = Number(data.explicitContentFilter);
|
||||||
}
|
}
|
||||||
@@ -768,17 +768,17 @@ class Guild extends Base {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a new guild icon.
|
* 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
|
* @param {string} [reason] Reason for changing the guild's icon
|
||||||
* @returns {Promise<Guild>}
|
* @returns {Promise<Guild>}
|
||||||
* @example
|
* @example
|
||||||
* // Edit the guild icon
|
* // Edit the guild icon
|
||||||
* guild.setIcon(fs.readFileSync('./icon.png'))
|
* guild.setIcon('./icon.png')
|
||||||
* .then(updated => console.log('Updated the guild icon'))
|
* .then(updated => console.log('Updated the guild icon'))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
setIcon(icon, reason) {
|
async setIcon(icon, reason) {
|
||||||
return this.edit({ 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.
|
* 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
|
* @param {string} [reason] Reason for changing the guild's splash screen
|
||||||
* @returns {Promise<Guild>}
|
* @returns {Promise<Guild>}
|
||||||
* @example
|
* @example
|
||||||
* // Edit the guild splash
|
* // Edit the guild splash
|
||||||
* guild.setIcon(fs.readFileSync('./splash.png'))
|
* guild.setSplash('./splash.png')
|
||||||
* .then(updated => console.log('Updated the guild splash'))
|
* .then(updated => console.log('Updated the guild splash'))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
setSplash(splash, reason) {
|
async setSplash(splash, reason) {
|
||||||
return this.edit({ 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);
|
.then(emoji => this.client.actions.GuildEmojiCreate.handle(this, emoji).emoji);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.client.resolver.resolveBuffer(attachment)
|
return this.client.resolver.resolveFile(attachment)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
const dataURI = this.client.resolver.resolveBase64(data);
|
const dataURI = this.client.resolver.resolveBase64(data);
|
||||||
return this.createEmoji(dataURI, name, { roles, reason });
|
return this.createEmoji(dataURI, name, { roles, reason });
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ class TextChannel extends GuildChannel {
|
|||||||
name, avatar,
|
name, avatar,
|
||||||
}, reason }).then(data => new Webhook(this.client, data));
|
}, reason }).then(data => new Webhook(this.client, data));
|
||||||
} else {
|
} 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));
|
this.createWebhook(name, this.client.resolver.resolveBase64(data) || null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ class Webhook {
|
|||||||
*/
|
*/
|
||||||
edit({ name = this.name, avatar }, reason) {
|
edit({ name = this.name, avatar }, reason) {
|
||||||
if (avatar && (typeof avatar === 'string' && !avatar.startsWith('data:'))) {
|
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);
|
const dataURI = this.client.resolver.resolveBase64(file);
|
||||||
return this.edit({ name, avatar: dataURI }, reason);
|
return this.edit({ name, avatar: dataURI }, reason);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user