diff --git a/packages/rest/__tests__/CDN.test.ts b/packages/rest/__tests__/CDN.test.ts index dd7a3eb..333903f 100644 --- a/packages/rest/__tests__/CDN.test.ts +++ b/packages/rest/__tests__/CDN.test.ts @@ -52,6 +52,22 @@ test('emoji gif', () => { expect(cdn.emoji(id, 'gif')).toBe(`${base}/emojis/${id}.gif`); }); +test('guildMemberAvatar default', () => { + expect(cdn.guildMemberAvatar(id, id, hash)).toBe(`${base}/guilds/${id}/users/${id}/avatars/${hash}.png`); +}); + +test('guildMemberAvatar dynamic-animated', () => { + expect(cdn.guildMemberAvatar(id, id, animatedHash, { dynamic: true })).toBe( + `${base}/guilds/${id}/users/${id}/avatars/${animatedHash}.gif`, + ); +}); + +test('guildMemberAvatar dynamic-not-animated', () => { + expect(cdn.guildMemberAvatar(id, id, hash, { dynamic: true })).toBe( + `${base}/guilds/${id}/users/${id}/avatars/${hash}.png`, + ); +}); + test('icon default', () => { expect(cdn.icon(id, hash)).toBe(`${base}/icons/${id}/${hash}.png`); }); diff --git a/packages/rest/src/lib/CDN.ts b/packages/rest/src/lib/CDN.ts index d2fa037..d49a416 100644 --- a/packages/rest/src/lib/CDN.ts +++ b/packages/rest/src/lib/CDN.ts @@ -93,6 +93,26 @@ export class CDN { return this.makeURL(`/emojis/${emojiId}`, { extension }); } + /** + * Generates a guild member avatar URL. + * @param guildId The id of the guild + * @param userId The id of the user + * @param iconHash The hash provided by Discord for this avatar + * @param options Optional options for the avatar + */ + public guildMemberAvatar( + guildId: string, + userId: string, + avatarHash: string, + { dynamic = false, ...options }: ImageURLOptions = {}, + ): string { + if (dynamic && avatarHash.startsWith('a_')) { + options.extension = 'gif'; + } + + return this.makeURL(`/guilds/${guildId}/users/${userId}/avatars/${avatarHash}`, options); + } + /** * Generates an icon URL, e.g. for a guild. * @param id The id that has the icon splash