Skip to content

Commit

Permalink
animated emojis (#2182)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek authored and iCrawl committed Dec 31, 2017
1 parent 59d5c5a commit 84e4dd6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/structures/Emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Emoji extends Base {
*/
this.managed = data.managed;

/**
* Whether this emoji is animated
* @type {boolean}
*/
this.animated = data.animated;

this._roles = data.roles;
}

Expand Down Expand Up @@ -85,7 +91,7 @@ class Emoji extends Base {
* @readonly
*/
get url() {
return this.client.rest.cdn.Emoji(this.id);
return this.client.rest.cdn.Emoji(this.id, this.animated ? 'gif' : 'png');
}

/**
Expand Down Expand Up @@ -198,7 +204,11 @@ class Emoji extends Base {
* msg.reply(`Hello! ${emoji}`);
*/
toString() {
return this.requiresColons ? `<:${this.name}:${this.id}>` : this.name;
if (!this.id || !this.requiresColons) {
return this.name;
}

return `<${this.animated ? 'a' : ''}:${this.name}:${this.id}>`;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function makeImageUrl(root, { format = 'webp', size } = {}) {
exports.Endpoints = {
CDN(root) {
return {
Emoji: emojiID => `${root}/emojis/${emojiID}.png`,
Emoji: (emojiID, format = 'png') => `${root}/emojis/${emojiID}.${format}`,
Asset: name => `${root}/assets/${name}`,
DefaultAvatar: number => `${root}/embed/avatars/${number}.png`,
Avatar: (userID, hash, format = 'default', size) => {
Expand Down
13 changes: 8 additions & 5 deletions src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,21 @@ class Util {
* Parses emoji info out of a string. The string must be one of:
* * A UTF-8 emoji (no ID)
* * A URL-encoded UTF-8 emoji (no ID)
* * A Discord custom emoji (`<:name:id>`)
* * A Discord custom emoji (`<:name:id>` or `<a:name:id>`)
* @param {string} text Emoji string to parse
* @returns {Object} Object with `name` and `id` properties
* @returns {Object} Object with `animated`, `name`, and `id` properties
* @private
*/
static parseEmoji(text) {
if (text.includes('%')) text = decodeURIComponent(text);
if (text.includes(':')) {
const [name, id] = text.split(':');
return { name, id };
const m = text.match(/<?(a)?:(\w{2,32}):(\d{17,19})>?/);
if (!m) {
return null;
}
return { animated: Boolean(m[1]), name: m[2], id: m[3] };
} else {
return { name: text, id: null };
return { animated: false, name: text, id: null };
}
}

Expand Down

0 comments on commit 84e4dd6

Please # to comment.