Skip to content
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

feat(Sticker): added Sticker #4909

Merged
merged 91 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
65c9ef2
Merge pull request #2 from discordjs/master
wonderlandpark Oct 16, 2020
afae3fa
feat(Sticker): add sticker
wonderlandpark Oct 16, 2020
1302c40
Merge branch 'sticker' of https://github.com/wonderlandpark/discord.j…
wonderlandpark Oct 16, 2020
3064e9d
types(Sticker): added sticker class
wonderlandpark Oct 16, 2020
b812434
fix(Sticker): fixed Sticker url for LOTTIE format
wonderlandpark Oct 16, 2020
8549512
types(Sticker): fixed type number
wonderlandpark Oct 16, 2020
3f798ad
feat(Stickers): removed debugging code
wonderlandpark Oct 16, 2020
bc2f51f
fix(Stickers): Added name tag
wonderlandpark Oct 16, 2020
1e27b34
fix(Stickers): Added name tag
wonderlandpark Oct 16, 2020
900dd5a
fix(Stickers): Added name tag
wonderlandpark Oct 16, 2020
3fcf80d
fix(Stickers): Added name tag
wonderlandpark Oct 16, 2020
1d45a5f
fix(Stickers): Added name tag
wonderlandpark Oct 16, 2020
6e51b44
fix(Stickers): Added name tag
wonderlandpark Oct 16, 2020
194fa61
Update typings/index.d.ts
wonderlandpark Oct 16, 2020
4c8aa42
fix(Stickers): Added name tag
wonderlandpark Oct 16, 2020
b313f46
style(Sticker): fixed createAt -> createdAt
wonderlandpark Oct 16, 2020
ee255ef
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
462e96f
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
85f0d28
Update typings/index.d.ts
wonderlandpark Oct 16, 2020
9a60d0f
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
ef61773
type(Sticker): Fixed typo issue
wonderlandpark Oct 16, 2020
4772101
type(Sticker): Fixed typo issue
wonderlandpark Oct 16, 2020
3dcc0be
Merge branch 'sticker' of https://github.com/wonderlandpark/discord.j…
wonderlandpark Oct 16, 2020
f60f28a
types: fixed mistake
wonderlandpark Oct 16, 2020
b7e1706
feat(Sticker): added type for Sticker#name
wonderlandpark Oct 16, 2020
630e78b
types(Sticker:URLOptions): fixed type error
wonderlandpark Oct 16, 2020
8136858
feat(Sticker): added Sticker in index.js and esm/discord.mjs
wonderlandpark Oct 16, 2020
691f0ba
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
d6463ff
Update typings/index.d.ts
wonderlandpark Oct 16, 2020
4225f77
Update typings/index.d.ts
wonderlandpark Oct 16, 2020
7643b44
Merge branch 'sticker' of https://github.com/wonderlandpark/discord.j…
wonderlandpark Oct 16, 2020
c580985
style(Sticker): add docstring
wonderlandpark Oct 16, 2020
f984117
typo(Sticker): sentance improve
wonderlandpark Oct 16, 2020
21e04b5
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
86252b8
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
e5f0abf
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
123e3b4
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
919b10d
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
9acd175
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
ce1e64a
Update src/structures/Sticker.js
wonderlandpark Oct 16, 2020
4b9a22d
style(Sticker): changed warn block to info block
wonderlandpark Oct 16, 2020
b81353a
Merge branch 'sticker' of https://github.com/wonderlandpark/discord.j…
wonderlandpark Oct 16, 2020
ef60c92
fix: makeStickerUrl function
wonderlandpark Oct 16, 2020
4dfeb21
Update src/util/Constants.js
wonderlandpark Oct 19, 2020
e463763
fix(Sticker): eslint error fix
wonderlandpark Oct 19, 2020
6315ac7
Update src/util/Constants.js
wonderlandpark Oct 20, 2020
3dd5f67
types(Sticker): fixed type error
wonderlandpark Oct 20, 2020
cef8ae9
style(Sticker): fixed grammer
wonderlandpark Oct 21, 2020
0c25ed3
feat(Sticker): removed stickerPreviewURL
wonderlandpark Oct 21, 2020
62eb04a
Merge branch 'sticker' of https://github.com/wonderlandpark/discord.j…
wonderlandpark Oct 21, 2020
74e42b2
Update typings/index.d.ts
wonderlandpark Oct 23, 2020
e734b1b
Update src/util/Constants.js
wonderlandpark Oct 23, 2020
d985fca
Update src/util/Constants.js
wonderlandpark Oct 23, 2020
1e1a33e
style(Sticker): renamed option passthrough to animated
wonderlandpark Oct 23, 2020
e215c91
types(Sticker): added INVALID_STICKER_SENT error code
wonderlandpark Nov 14, 2020
b7cca00
feat(Sticker): added INVALID_STICKER_SENT error code
wonderlandpark Nov 14, 2020
eb613d6
Update src/structures/Sticker.js
wonderlandpark Nov 15, 2020
d81c264
Update src/structures/Sticker.js
wonderlandpark Nov 15, 2020
3b4627e
feat(Sticker): array of tags
wonderlandpark Nov 15, 2020
ce65042
docs(Sticker): typo change
wonderlandpark Nov 15, 2020
ff6d50e
Update src/structures/Sticker.js
wonderlandpark Nov 15, 2020
6bfca6a
Update src/structures/Sticker.js
wonderlandpark Nov 15, 2020
5fe411e
Update typings/index.d.ts
wonderlandpark Nov 15, 2020
7aa13b7
Update src/structures/Sticker.js
wonderlandpark Nov 15, 2020
c48ae1a
Update typings/index.d.ts
wonderlandpark Nov 16, 2020
599f3a2
Update typings/index.d.ts
wonderlandpark Nov 16, 2020
6c4746e
Update src/util/Constants.js
wonderlandpark Nov 21, 2020
2359ba6
Merge branch 'master' into sticker
wonderlandpark Nov 21, 2020
4c1b977
types(Sticker): removed sticker preview url typing
wonderlandpark Nov 21, 2020
df88127
Merge branch 'sticker' of https://github.com/wonderlandpark/discord.j…
wonderlandpark Nov 21, 2020
a211c05
type(Sticker): removed trailing whitespace
wonderlandpark Nov 21, 2020
c8211bc
Update src/util/Constants.js
wonderlandpark Nov 29, 2020
7a59572
feat: not using passthrough, size params
wonderlandpark Nov 29, 2020
bacb71b
Update src/structures/Sticker.js
wonderlandpark Nov 30, 2020
f33d1d5
Update src/structures/Sticker.js
wonderlandpark Nov 30, 2020
e77fe3a
feat: removed makeStickerUrl function
wonderlandpark Nov 30, 2020
53fcec8
Update src/structures/Sticker.js
wonderlandpark Dec 29, 2020
bb69f63
fix: added missing StickerFormatTypes Constatns
wonderlandpark Jan 26, 2021
477595a
chore: defaulting the value of tags as an array
wonderlandpark Feb 1, 2021
b449871
types: sticker tags are always string[]
wonderlandpark Feb 6, 2021
9e2cc15
Update src/structures/Sticker.js
wonderlandpark Feb 6, 2021
53c741f
Merge branch 'master' into sticker
wonderlandpark Feb 14, 2021
567009d
chore: using getter for url
wonderlandpark Feb 15, 2021
f1204f4
types: added missing type
wonderlandpark Feb 15, 2021
91e5e2f
feat: using createEnum function
wonderlandpark Feb 15, 2021
9bf7f1b
Update typings/index.d.ts
wonderlandpark Feb 16, 2021
de7b1d7
Update src/structures/Sticker.js
wonderlandpark Apr 15, 2021
0e3d721
Update typings/index.d.ts
wonderlandpark Apr 15, 2021
7e1061d
types: added stickers at message class
wonderlandpark Apr 28, 2021
ec3d99e
style: missing semi
wonderlandpark Apr 28, 2021
46db4c7
Update typings/index.d.ts
wonderlandpark Apr 29, 2021
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
12 changes: 12 additions & 0 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MessageAttachment = require('./MessageAttachment');
const Embed = require('./MessageEmbed');
const Mentions = require('./MessageMentions');
const ReactionCollector = require('./ReactionCollector');
const Sticker = require('./Sticker');
const { Error, TypeError } = require('../errors');
const ReactionManager = require('../managers/ReactionManager');
const Collection = require('../util/Collection');
Expand Down Expand Up @@ -133,6 +134,17 @@ class Message extends Base {
}
}

/**
* A collection of stickers in the message
* @type {Collection<Snowflake, Sticker>}
*/
this.stickers = new Collection();
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
if (data.stickers) {
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
for (const sticker of data.stickers) {
this.stickers.set(sticker.id, new Sticker(this.client, sticker));
}
}
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved

/**
* The timestamp the message was sent at
* @type {number}
Expand Down
102 changes: 102 additions & 0 deletions src/structures/Sticker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

const Base = require('./Base');
const { StickerFormatTypes } = require('../util/Constants');
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
const Snowflake = require('../util/Snowflake');

class Sticker extends Base {
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
constructor(client, sticker) {
super(client);
/**
* The ID of the sticker
* @type {Snowflake}
*/
this.id = sticker.id;

/**
* The ID of the sticker's image
* @type {string}
*/
this.asset = sticker.asset;

/**
* The description of the sticker
* @type {string}
*/
this.description = sticker.description;

/**
* The format of the sticker
* @type {string}
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
*/
this.format = StickerFormatTypes[sticker.format_type];
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved

/**
* The name of the sticker
* @type {string}
*/
this.name = sticker.name;

/**
* The ID of the pack the sticker is from
* @type {Snowflake}
*/
this.packID = sticker.pack_id;

/**
* The ID of sticker preview image
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
* @type {?string}
*/
this.previewAsset = sticker.preview_asset;

/**
* A comma-separated list of tags for the sticker
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
* @type {?string}
*/
this.tags = sticker.tags;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* A link to the sticker.
* <warn>If format is "LOTTIE" it would return URL of Lottie json file</warn>
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
* @param {StickerURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
stickerURL({ size } = {}) {
if (!this.id || !this.asset) return null;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
return this.client.rest.cdn.Sticker(this.id, this.asset, this.format, size);
}

/**
* A link to the sticker preview image.
* <warn>If format is "LOTTIE" it would return URL of Lottie json file</warn>
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
* @param {StickerURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
stickerPreviewURL({ size } = {}) {
if (!this.id || !this.asset) return null;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
return this.client.rest.cdn.Sticker(this.id, this.previewAsset, this.format, size);
Copy link
Member

Choose a reason for hiding this comment

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

Take the URL https://media.discordapp.net/stickers/748293342357356564/17e05030dcafa6604ee7479789652459.png?size=256&passthrough=false

Setting passthrough to false seems to make the sticker static, while setting it to true or omitting it seems to make it animate

Do we want to document this? Seems like there's the discord.com/stickers/<skuID>/stickerID URL and then there's the preview one from media? Seems like they're different URLs altogether (between asset and previewAsset), so this might need further research and another CDN route

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, I added passthrough option at new function makeStickerUrl.

When we discover previewAsset's CDN I will add it.

}

/**
* The timestamp the sticker was created at, or null if unicode
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
* @type {?number}
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
* @readonly
*/
get createdTimestamp() {
if (!this.id) return null;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
return Snowflake.deconstruct(this.id).timestamp;
}

/**
* The time the stikcer was created at, or null if unicode
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
* @type {?Date}
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
* @readonly
*/
get createdAt() {
if (!this.id) return null;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
return new Date(this.createdTimestamp);
}
}

module.exports = Sticker;
23 changes: 23 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,18 @@ function makeImageUrl(root, { format = 'webp', size } = {}) {
* @property {number} [size] One of `16`, `32`, `64`, `128`, `256`, `512`, `1024`, `2048`, `4096`
*/

/**
* Options for Sticker Image URL.
* @typedef {Object} StickerURLOptions
* @property {number} [size] One of `16`, `32`, `64`, `128`, `256`, `512`, `1024`, `2048`, `4096`
*/

exports.Endpoints = {
CDN(root) {
return {
Emoji: (emojiID, format = 'png') => `${root}/emojis/${emojiID}.${format}`,
Sticker: (stickerID, hash, format, size) =>
makeImageUrl(`${root}/stickers/${stickerID}/${hash}`, { format: format !== 'LOTTIE' ? 'png' : 'json', size }),
Asset: name => `${root}/assets/${name}`,
DefaultAvatar: discriminator => `${root}/embed/avatars/${discriminator}.png`,
Avatar: (userID, hash, format = 'webp', size, dynamic = false) => {
Expand Down Expand Up @@ -644,6 +652,21 @@ exports.WebhookTypes = [
'Channel Follower',
];

/**
* The type for sticker format
* * PNG
* * APNG
* * LOTTIE
* @typedef {string} StickerFormatTypes
*/
exports.StickerFormatTypes = [
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
// They start at 1
null,
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
'PNG',
'APNG',
'LOTTIE',
];

function keyMirror(arr) {
let tmp = Object.create(null);
for (const value of arr) tmp[value] = value;
Expand Down
22 changes: 22 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3114,6 +3114,28 @@ declare module 'discord.js' {

type Status = number;

export class Sticker extends Base {
constructor(client: Client, data: object);
public asset: string;
public readonly createdTimestamp: number;
public readonly createdAt: Date;
public description: string;
public format: StickerFormatTypes;
public id: Snowflake;
public name: string;
public packID: Snowflake;
public previewAsset?: string;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
public tags: string;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
public stickerURL(options?: StickerURLOptions): string | null;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
public stickerPreviewURL(options?: StickerURLOptions): string | null;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
}

interface StickerURLOptions {
size: ImageSize;
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved
}

type StickerFormatTypes = 'PNG' | 'APNG' | 'LOTTIE';
wonderlandpark marked this conversation as resolved.
Show resolved Hide resolved

interface StreamOptions {
type?: StreamType;
seek?: number;
Expand Down