buddy

node MVC discord bot
Log | Files | Refs | README

Emoji.js (2394B)


      1 'use strict';
      2 
      3 const Base = require('./Base');
      4 const Snowflake = require('../util/Snowflake');
      5 
      6 /**
      7  * Represents an emoji, see {@link GuildEmoji} and {@link ReactionEmoji}.
      8  * @extends {Base}
      9  */
     10 class Emoji extends Base {
     11   constructor(client, emoji) {
     12     super(client);
     13     /**
     14      * Whether this emoji is animated
     15      * @type {boolean}
     16      */
     17     this.animated = emoji.animated;
     18 
     19     /**
     20      * The name of this emoji
     21      * @type {string}
     22      */
     23     this.name = emoji.name;
     24 
     25     /**
     26      * The ID of this emoji
     27      * @type {?Snowflake}
     28      */
     29     this.id = emoji.id;
     30 
     31     /**
     32      * Whether this emoji has been deleted
     33      * @type {boolean}
     34      */
     35     this.deleted = false;
     36   }
     37 
     38   /**
     39    * The identifier of this emoji, used for message reactions
     40    * @type {string}
     41    * @readonly
     42    */
     43   get identifier() {
     44     if (this.id) return `${this.animated ? 'a:' : ''}${this.name}:${this.id}`;
     45     return encodeURIComponent(this.name);
     46   }
     47 
     48   /**
     49    * The URL to the emoji file if its a custom emoji
     50    * @type {?string}
     51    * @readonly
     52    */
     53   get url() {
     54     if (!this.id) return null;
     55     return this.client.rest.cdn.Emoji(this.id, this.animated ? 'gif' : 'png');
     56   }
     57 
     58   /**
     59    * The timestamp the emoji was created at, or null if unicode
     60    * @type {?number}
     61    * @readonly
     62    */
     63   get createdTimestamp() {
     64     if (!this.id) return null;
     65     return Snowflake.deconstruct(this.id).timestamp;
     66   }
     67 
     68   /**
     69    * The time the emoji was created at, or null if unicode
     70    * @type {?Date}
     71    * @readonly
     72    */
     73   get createdAt() {
     74     if (!this.id) return null;
     75     return new Date(this.createdTimestamp);
     76   }
     77 
     78   /**
     79    * When concatenated with a string, this automatically returns the text required to form a graphical emoji on Discord
     80    * instead of the Emoji object.
     81    * @returns {string}
     82    * @example
     83    * // Send a custom emoji from a guild:
     84    * const emoji = guild.emojis.cache.first();
     85    * msg.reply(`Hello! ${emoji}`);
     86    * @example
     87    * // Send the emoji used in a reaction to the channel the reaction is part of
     88    * reaction.message.channel.send(`The emoji used was: ${reaction.emoji}`);
     89    */
     90   toString() {
     91     return this.id ? `<${this.animated ? 'a' : ''}:${this.name}:${this.id}>` : this.name;
     92   }
     93 
     94   toJSON() {
     95     return super.toJSON({
     96       guild: 'guildID',
     97       createdTimestamp: true,
     98       url: true,
     99       identifier: true,
    100     });
    101   }
    102 }
    103 
    104 module.exports = Emoji;