Skip to content

refactor(Emoji)!: make imageURL() change extension dynamically #10613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/BaseGuildEmoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BaseGuildEmoji extends Emoji {
* @method imageURL
* @memberof BaseGuildEmoji
* @instance
* @param {BaseImageURLOptions} [options] Options for the image URL
* @param {ImageURLOptions} [options={}] Options for the image URL
* @returns {string}
*/

Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/src/structures/Emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class Emoji extends Base {

/**
* Returns a URL for the emoji or `null` if this is not a custom emoji.
* @param {BaseImageURLOptions} [options] Options for the image URL
* @param {ImageURLOptions} [options={}] Options for the image URL
* @returns {?string}
*/
imageURL(options) {
return this.id && this.client.rest.cdn.emoji(this.id, options);
imageURL(options = {}) {
return this.id && this.client.rest.cdn.emoji(this.id, this.animated, options);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ export abstract class BaseGuild extends Base {

export class BaseGuildEmoji extends Emoji {
protected constructor(client: Client<true>, data: RawGuildEmojiData, guild: Guild | GuildPreview);
public imageURL(options?: BaseImageURLOptions): string;
public imageURL(options?: ImageURLOptions): string;
public get url(): string;
public available: boolean | null;
public get createdAt(): Date;
Expand Down Expand Up @@ -1333,7 +1333,7 @@ export class Emoji extends Base {
public id: Snowflake | null;
public name: string | null;
public get identifier(): string;
public imageURL(options?: BaseImageURLOptions): string | null;
public imageURL(options?: ImageURLOptions): string | null;
public get url(): string | null;
public toJSON(): unknown;
public toString(): string;
Expand Down
24 changes: 20 additions & 4 deletions packages/rest/__tests__/CDN.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,28 @@ test('discoverySplash default', () => {
expect(cdn.discoverySplash(id, hash)).toEqual(`${baseCDN}/discovery-splashes/${id}/${hash}.webp`);
});

test('emoji default', () => {
expect(cdn.emoji(id)).toEqual(`${baseCDN}/emojis/${id}.webp`);
test('emoji static', () => {
expect(cdn.emoji(id, false)).toEqual(`${baseCDN}/emojis/${id}.webp`);
});

test('emoji gif', () => {
expect(cdn.emoji(id, { extension: 'gif' })).toEqual(`${baseCDN}/emojis/${id}.gif`);
test('emoji static with JPG extension', () => {
expect(cdn.emoji(id, false, { extension: 'jpg' })).toEqual(`${baseCDN}/emojis/${id}.jpg`);
});

test('emoji static with JPG extension with force static', () => {
expect(cdn.emoji(id, false, { extension: 'jpg', forceStatic: true })).toEqual(`${baseCDN}/emojis/${id}.jpg`);
});

test('emoji animated', () => {
expect(cdn.emoji(id, true)).toEqual(`${baseCDN}/emojis/${id}.gif`);
});

test('emoji animated with JPG extension', () => {
expect(cdn.emoji(id, true, { extension: 'jpg' })).toEqual(`${baseCDN}/emojis/${id}.gif`);
});

test('emoji animated with JPG extension with force static', () => {
expect(cdn.emoji(id, true, { extension: 'jpg', forceStatic: true })).toEqual(`${baseCDN}/emojis/${id}.jpg`);
});

test('guildMemberAvatar default', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ export class CDN {
* Generates an emoji's URL.
*
* @param emojiId - The emoji id
* @param animated - Whether the emoji is animated
* @param options - Optional options for the emoji
*/
public emoji(emojiId: string, options?: Readonly<BaseImageURLOptions>): string {
return this.makeURL(`/emojis/${emojiId}`, options);
public emoji(emojiId: string, animated: boolean, options?: Readonly<ImageURLOptions>): string {
return this.dynamicMakeURL(`/emojis/${emojiId}`, animated ? 'a_' : '', options);
}

/**
Expand Down
Loading