Skip to content

Commit

Permalink
feat(Sticker): add support for gif stickers (#9038)
Browse files Browse the repository at this point in the history
* feat(Sticker): add support for gif stickers

* chore: upgrade discord-api-types

* refactor: requested changes
  • Loading branch information
almeidx authored Jan 13, 2023
1 parent 8c265b6 commit 6a9875d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/discord.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@discordjs/util": "workspace:^",
"@sapphire/snowflake": "^3.4.0",
"@types/ws": "^8.5.4",
"discord-api-types": "^0.37.27",
"discord-api-types": "^0.37.28",
"fast-deep-equal": "^3.1.3",
"lodash.snakecase": "^4.1.1",
"tslib": "^2.4.1",
Expand Down
5 changes: 3 additions & 2 deletions packages/discord.js/src/structures/Sticker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

const { DiscordSnowflake } = require('@sapphire/snowflake');
const { Routes, StickerFormatType } = require('discord-api-types/v10');
const { Routes } = require('discord-api-types/v10');
const Base = require('./Base');
const { DiscordjsError, ErrorCodes } = require('../errors');
const { StickerFormatExtensionMap } = require('../util/Constants');

/**
* Represents a Sticker.
Expand Down Expand Up @@ -164,7 +165,7 @@ class Sticker extends Base {
* @readonly
*/
get url() {
return this.client.rest.cdn.sticker(this.id, this.format === StickerFormatType.Lottie ? 'json' : 'png');
return this.client.rest.cdn.sticker(this.id, StickerFormatExtensionMap[this.format]);
}

/**
Expand Down
18 changes: 17 additions & 1 deletion packages/discord.js/src/util/Constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { ChannelType, MessageType, ComponentType } = require('discord-api-types/v10');
const { ChannelType, MessageType, ComponentType, ImageFormat, StickerFormatType } = require('discord-api-types/v10');

/**
* Max bulk deletable message age
Expand Down Expand Up @@ -138,6 +138,21 @@ exports.SelectMenuTypes = [
ComponentType.ChannelSelect,
];

/**
* A mapping between sticker formats and their respective image formats.
* * {@link StickerFormatType.PNG} -> {@link ImageFormat.PNG}
* * {@link StickerFormatType.APNG} -> {@link ImageFormat.PNG}
* * {@link StickerFormatType.Lottie} -> {@link ImageFormat.Lottie}
* * {@link StickerFormatType.GIF} -> {@link ImageFormat.GIF}
* @typedef {Object} StickerFormatExtensionMap
*/
exports.StickerFormatExtensionMap = {
[StickerFormatType.PNG]: ImageFormat.PNG,
[StickerFormatType.APNG]: ImageFormat.PNG,
[StickerFormatType.Lottie]: ImageFormat.Lottie,
[StickerFormatType.GIF]: ImageFormat.GIF,
};

/**
* @typedef {Object} Constants Constants that can be used in an enum or object-like way.
* @property {number} MaxBulkDeletableMessageAge Max bulk deletable message age
Expand All @@ -147,4 +162,5 @@ exports.SelectMenuTypes = [
* @property {ThreadChannelTypes} ThreadChannelTypes The types of channels that are threads
* @property {VoiceBasedChannelTypes} VoiceBasedChannelTypes The types of channels that are voice-based
* @property {SelectMenuTypes} SelectMenuTypes The types of components that are select menus.
* @property {Object} StickerFormatExtensionMap A mapping between sticker formats and their respective image formats.
*/
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ import {
ForumLayoutType,
ApplicationRoleConnectionMetadataType,
APIApplicationRoleConnectionMetadata,
ImageFormat,
} from 'discord-api-types/v10';
import { ChildProcess } from 'node:child_process';
import { EventEmitter } from 'node:events';
Expand Down Expand Up @@ -3445,6 +3446,7 @@ export const Constants: {
ThreadChannelTypes: ThreadChannelType[];
VoiceBasedChannelTypes: VoiceBasedChannelTypes[];
SelectMenuTypes: SelectMenuType[];
StickerFormatExtensionMap: Record<StickerFormatType, ImageFormat>;
};

export const version: string;
Expand Down
9 changes: 4 additions & 5 deletions packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,11 @@ export class CDN {
*
* @param stickerId - The sticker id
* @param extension - The extension of the sticker
* @privateRemarks
* Stickers cannot have a `.webp` extension, so we default to a `.png`
*/
public sticker(stickerId: string, extension?: StickerExtension): string {
return this.makeURL(`/stickers/${stickerId}`, {
allowedExtensions: ALLOWED_STICKER_EXTENSIONS,
extension: extension ?? 'png', // Stickers cannot have a `.webp` extension, so we default to a `.png`
});
public sticker(stickerId: string, extension: StickerExtension = 'png'): string {
return this.makeURL(`/stickers/${stickerId}`, { allowedExtensions: ALLOWED_STICKER_EXTENSIONS, extension });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const enum RESTEvents {
}

export const ALLOWED_EXTENSIONS = ['webp', 'png', 'jpg', 'jpeg', 'gif'] as const satisfies readonly string[];
export const ALLOWED_STICKER_EXTENSIONS = ['png', 'json'] as const satisfies readonly string[];
export const ALLOWED_STICKER_EXTENSIONS = ['png', 'json', 'gif'] as const satisfies readonly string[];
export const ALLOWED_SIZES = [16, 32, 64, 128, 256, 512, 1_024, 2_048, 4_096] as const satisfies readonly number[];

export type ImageExtension = (typeof ALLOWED_EXTENSIONS)[number];
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8436,10 +8436,10 @@ __metadata:
languageName: node
linkType: hard

"discord-api-types@npm:^0.37.27":
version: 0.37.27
resolution: "discord-api-types@npm:0.37.27"
checksum: f3c404db151862871686008a1e7902f22e2147b391840f9184568d5bc54593babac567fed90bd4831a6b793bd84c424172b081af36badbf950ea0e6fcd5d2e8a
"discord-api-types@npm:^0.37.27, discord-api-types@npm:^0.37.28":
version: 0.37.28
resolution: "discord-api-types@npm:0.37.28"
checksum: c033b9c82d5ecb3f273d563db578e21e982a35d106cc66e7ea42d7ef0a79817ca43badd3aade8a5ddb491bc8679ed08b1bcccc105e64ecd1ea5a53a11c2b1f8b
languageName: node
linkType: hard

Expand All @@ -8457,7 +8457,7 @@ __metadata:
"@sapphire/snowflake": ^3.4.0
"@types/node": 16.18.11
"@types/ws": ^8.5.4
discord-api-types: ^0.37.27
discord-api-types: ^0.37.28
dtslint: ^4.2.1
eslint: ^8.31.0
eslint-formatter-pretty: ^4.1.0
Expand Down

2 comments on commit 6a9875d

@vercel
Copy link

@vercel vercel bot commented on 6a9875d Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 6a9875d Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.