Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Commit

Permalink
feat(CDN): guild member avatars (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaporoxx authored Oct 9, 2021
1 parent d1758c7 commit 90c25ad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/rest/__tests__/CDN.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
Expand Down
20 changes: 20 additions & 0 deletions packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 90c25ad

Please sign in to comment.