Improve emoji support

This commit is contained in:
Amish Shah
2016-10-27 16:12:02 +01:00
parent 81059885a2
commit d129457624
4 changed files with 99 additions and 8 deletions

View File

@@ -101,6 +101,18 @@ class Emoji {
toString() {
return this.requiresColons ? `<:${this.name}:${this.id}>` : this.name;
}
/**
* The identifier of this emoji, used for message reactions
* @readonly
* @type {string}
*/
get identifier() {
if (this.id) {
return `${this.name}:${this.id}`;
}
return encodeURIComponent(this.name);
}
}
module.exports = Emoji;

View File

@@ -158,7 +158,7 @@ class Message {
}
_addReaction(emoji, user) {
const emojiID = emoji.id || emoji;
const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
let reaction;
if (this.reactions.has(emojiID)) {
reaction = this.reactions.get(emojiID);
@@ -304,6 +304,14 @@ class Message {
}
addReaction(emoji) {
if (emoji.identifier) {
emoji = emoji.identifier;
} else if (typeof emoji === 'string') {
if (!emoji.includes('%')) emoji = encodeURIComponent(emoji);
} else {
return Promise.reject(`Emoji must be a string or an Emoji/ReactionEmoji`);
}
return this.client.rest.methods.addMessageReaction(this.channel.id, this.id, emoji);
}

View File

@@ -1,5 +1,55 @@
const Collection = require('../util/Collection');
const parseEmoji = require('../util/ParseEmoji');
const Emoji = require('./Emoji');
/**
* Represents a limited emoji set used for both custom and unicode emojis. Custom emojis
* will use this class opposed to the Emoji class when the client doesn't know enough
* information about them.
*/
class ReactionEmoji {
constructor(reaction, name, id) {
/**
* The message reaction this emoji refers to
* @type {MessageReaction}
*/
this.reaction = reaction;
/**
* The name of this reaction emoji.
* @type {string}
*/
this.name = name;
/**
* The ID of this reaction emoji.
* @type {string}
*/
this.id = id;
}
/**
* The identifier of this emoji, used for message reactions
* @readonly
* @type {string}
*/
get identifier() {
if (this.id) {
return `${this.name}:${this.id}`;
}
return encodeURIComponent(this.name);
}
/**
* Creates the text required to form a graphical emoji on Discord.
* @example
* ```js
* // send the emoji used in a reaction to the channel the reaction is part of
* reaction.message.channel.sendMessage(`The emoji used is ${reaction.emoji}`);
* ```
* @returns {string}
*/
toString() {
return this.id ? `<:${this.name}:${this.id}>` : this.name;
}
}
/**
* Represents a reaction to a message
@@ -11,11 +61,8 @@ class MessageReaction {
* @type {Message}
*/
this.message = message;
/**
* The emoji of this reaction, if this is a string it is a plain unicode emoji.
* @type {Emoji}
*/
this.emoji = emoji;
this._emoji = new ReactionEmoji(this, emoji.name, emoji.id);
/**
* The number of people that have given the same reaction.
* @type {number}
@@ -28,13 +75,34 @@ class MessageReaction {
this.users = new Collection();
}
/**
* The emoji of this reaction, either an Emoji object for known custom emojis, or a ReactionEmoji which has fewer
* properties. Whatever the prototype of the emoji, it will still have `name`, `id`, `identifier` and `toString()`
* @type {Emoji|ReactionEmoji}
*/
get emoji() {
if (this._emoji instanceof Emoji) {
return this._emoji;
}
// check to see if the emoji has become known to the client
if (this._emoji.id) {
const emojis = this.message.client.emojis;
if (emojis.has(this._emoji.id)) {
const emoji = emojis.get(this._emoji.id);
this._emoji = emoji;
return emoji;
}
}
return this._emoji;
}
/**
* If the client has given this reaction to a message, it is removed.
* @returns {Promise<MessageReaction>}
*/
remove() {
const message = this.message;
return message.client.rest.methods.removeMessageReaction(message.channel.id, message.id, parseEmoji(this.emoji));
return message.client.rest.methods.removeMessageReaction(message.channel.id, message.id, this.emoji.identifier);
}
}

View File

@@ -1,4 +1,7 @@
module.exports = function parseEmoji(text) {
if (text.includes('%')) {
text = decodeURIComponent(text);
}
if (text.includes(':')) {
const [name, id] = text.split(':');
return { name, id };