feat: dynamic property for ImageURLOptions (#3530)

* Added dynamic property to ImageURLOptions

* fixes

* order

* typings fix

* made dynamic false by default

* add curly spaces
This commit is contained in:
Tenpi
2020-01-13 09:32:29 -05:00
committed by Amish Shah
parent 400cb56358
commit 8014ddcd1c
4 changed files with 22 additions and 20 deletions

View File

@@ -433,9 +433,9 @@ class Guild extends Base {
* @param {ImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
iconURL({ format, size } = {}) {
iconURL({ format, size, dynamic } = {}) {
if (!this.icon) return null;
return this.client.rest.cdn.Icon(this.id, this.icon, format, size);
return this.client.rest.cdn.Icon(this.id, this.icon, format, size, dynamic);
}
/**

View File

@@ -140,9 +140,9 @@ class User extends Base {
* @param {ImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
avatarURL({ format, size } = {}) {
avatarURL({ format, size, dynamic } = {}) {
if (!this.avatar) return null;
return this.client.rest.cdn.Avatar(this.id, this.avatar, format, size);
return this.client.rest.cdn.Avatar(this.id, this.avatar, format, size, dynamic);
}
/**

View File

@@ -119,7 +119,9 @@ function makeImageUrl(root, { format = 'webp', size } = {}) {
* Options for Image URLs.
* @typedef {Object} ImageURLOptions
* @property {string} [format] One of `webp`, `png`, `jpg`, `gif`. If no format is provided,
* it will be `gif` for animated avatars or otherwise `webp`
* defaults to `webp`.
* @property {boolean} [dynamic] If true, the format will dynamically change to `gif` for
* animated avatars; the default is false.
* @property {number} [size] One of `16`, `32`, `64`, `128`, `256`, `512`, `1024`, `2048`
*/
@@ -129,14 +131,14 @@ exports.Endpoints = {
Emoji: (emojiID, format = 'png') => `${root}/emojis/${emojiID}.${format}`,
Asset: name => `${root}/assets/${name}`,
DefaultAvatar: discriminator => `${root}/embed/avatars/${discriminator}.png`,
Avatar: (userID, hash, format = 'default', size) => {
if (format === 'default') format = hash.startsWith('a_') ? 'gif' : 'webp';
Avatar: (userID, hash, format = 'webp', size, dynamic = false) => {
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
return makeImageUrl(`${root}/avatars/${userID}/${hash}`, { format, size });
},
Banner: (guildID, hash, format = 'webp', size) =>
makeImageUrl(`${root}/banners/${guildID}/${hash}`, { format, size }),
Icon: (guildID, hash, format = 'default', size) => {
if (format === 'default') format = hash.startsWith('a_') ? 'gif' : 'webp';
Icon: (guildID, hash, format = 'webp', size, dynamic = false) => {
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
return makeImageUrl(`${root}/icons/${guildID}/${hash}`, { format, size });
},
AppIcon: (clientID, hash, { format = 'webp', size } = {}) =>

22
typings/index.d.ts vendored
View File

@@ -272,9 +272,9 @@ declare module 'discord.js' {
public name: string;
public owner: User | Team | null;
public rpcOrigins: string[];
public coverImage(options?: AvatarOptions): string;
public coverImage(options?: ImageURLOptions): string;
public fetchAssets(): Promise<ClientApplicationAsset>;
public iconURL(options?: AvatarOptions): string;
public iconURL(options?: ImageURLOptions): string;
public toJSON(): object;
public toString(): string;
}
@@ -291,7 +291,7 @@ declare module 'discord.js' {
public readonly createdAt: Date;
public readonly createdTimestamp: number;
public iconURL(options?: AvatarOptions): string;
public iconURL(options?: ImageURLOptions): string;
public toJSON(): object;
public toString(): string;
}
@@ -726,7 +726,7 @@ declare module 'discord.js' {
public widgetChannelID: Snowflake | null;
public widgetEnabled: boolean | null;
public addMember(user: UserResolvable, options: AddGuildMemberOptions): Promise<GuildMember>;
public bannerURL(options?: AvatarOptions): string | null;
public bannerURL(options?: ImageURLOptions): string | null;
public createIntegration(data: IntegrationData, reason?: string): Promise<Guild>;
public delete(): Promise<Guild>;
public edit(data: GuildEditData, reason?: string): Promise<Guild>;
@@ -740,7 +740,7 @@ declare module 'discord.js' {
public fetchVanityCode(): Promise<string>;
public fetchVoiceRegions(): Promise<Collection<string, VoiceRegion>>;
public fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
public iconURL(options?: AvatarOptions): string | null;
public iconURL(options?: ImageURLOptions & { dynamic?: boolean }): string | null;
public leave(): Promise<Guild>;
public member(user: UserResolvable): GuildMember | null;
public setAFKChannel(afkChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
@@ -759,7 +759,7 @@ declare module 'discord.js' {
public setSystemChannel(systemChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise<Guild>;
public setVerificationLevel(verificationLevel: number, reason?: string): Promise<Guild>;
public splashURL(options?: AvatarOptions): string | null;
public splashURL(options?: ImageURLOptions): string | null;
public toJSON(): object;
public toString(): string;
}
@@ -1189,8 +1189,8 @@ declare module 'discord.js' {
public largeText: string | null;
public smallImage: Snowflake | null;
public smallText: string | null;
public largeImageURL(options: AvatarOptions): string | null;
public smallImageURL(options: AvatarOptions): string | null;
public largeImageURL(options: ImageURLOptions): string | null;
public smallImageURL(options: ImageURLOptions): string | null;
}
export class Role extends Base {
@@ -1421,10 +1421,10 @@ declare module 'discord.js' {
public system?: boolean;
public readonly tag: string;
public username: string;
public avatarURL(options?: AvatarOptions): string | null;
public avatarURL(options?: ImageURLOptions & { dynamic?: boolean }): string | null;
public createDM(): Promise<DMChannel>;
public deleteDM(): Promise<DMChannel>;
public displayAvatarURL(options?: AvatarOptions): string;
public displayAvatarURL(options?: ImageURLOptions & { dynamic?: boolean }): string;
public equals(user: User): boolean;
public fetch(): Promise<User>;
public toString(): string;
@@ -2011,7 +2011,7 @@ declare module 'discord.js' {
new?: any;
}
interface AvatarOptions {
interface ImageURLOptions {
format?: ImageExt;
size?: ImageSize;
}