Skip to content

Commit

Permalink
feat(MessageReaction): backport animated, client, created*, and url (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC authored Jan 24, 2020
1 parent 4ca1864 commit 88b675d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/structures/MessageReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MessageReaction {
*/
this.users = new Collection();

this._emoji = new ReactionEmoji(this, emoji.name, emoji.id);
this._emoji = new ReactionEmoji(this, emoji);
}

/**
Expand Down
57 changes: 53 additions & 4 deletions src/structures/ReactionEmoji.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
const Constants = require('../util/Constants');
const Snowflake = require('../util/Snowflake');

/**
* 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) {
constructor(reaction, emoji) {
/**
* The client that instantiated this object
* @name ReactionEmoji#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: reaction.message.client });

/**
* The message reaction this emoji refers to
* @type {MessageReaction}
Expand All @@ -15,13 +26,49 @@ class ReactionEmoji {
* The name of this reaction emoji
* @type {string}
*/
this.name = name;
this.name = emoji.name;

/**
* The ID of this reaction emoji
* @type {?Snowflake}
*/
this.id = id;
this.id = emoji.id;

/**
* Whether this reaction emoji is animated
* @type {boolean}
*/
this.animated = emoji.animated || false;
}

/**
* The timestamp the reaction emoji was created at, or null if unicode
* @type {?number}
* @readonly
*/
get createdTimestamp() {
if (!this.id) return null;
return Snowflake.deconstruct(this.id).timestamp;
}

/**
* The time the reaction emoji was created, or null if unicode
* @type {?Date}
* @readonly
*/
get createdAt() {
if (!this.id) return null;
return new Date(this.createdTimestamp);
}

/**
* The URL to the reaction emoji file, or null if unicode
* @type {string}
* @readonly
*/
get url() {
if (!this.id) return null;
return Constants.Endpoints.CDN(this.client.options.http.cdn).Emoji(this.id, this.animated ? 'gif' : 'png');
}

/**
Expand All @@ -42,7 +89,9 @@ class ReactionEmoji {
* @returns {string}
*/
toString() {
return this.id ? `<:${this.name}:${this.id}>` : this.name;
if (!this.id) return this.name;

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

Expand Down
7 changes: 6 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,11 +1027,16 @@ declare module 'discord.js' {
}

export class ReactionEmoji {
constructor(reaction: MessageReaction, name: string, id: string);
constructor(reaction: MessageReaction, emoji: object);
public animated: boolean;
public readonly client: Client;
public readonly createdAt: number | null;
public readonly createdTimestamp: number | null;
public id: Snowflake;
public readonly identifier: string;
public name: string;
public reaction: MessageReaction;
public readonly url: string | null;
public toString(): string;
}

Expand Down

0 comments on commit 88b675d

Please sign in to comment.