buddy

node MVC discord bot
Log | Files | Refs | README

GuildEmoji.js (4287B)


      1 'use strict';
      2 
      3 const BaseGuildEmoji = require('./BaseGuildEmoji');
      4 const { Error } = require('../errors');
      5 const GuildEmojiRoleManager = require('../managers/GuildEmojiRoleManager');
      6 const Permissions = require('../util/Permissions');
      7 
      8 /**
      9  * Represents a custom emoji.
     10  * @extends {BaseGuildEmoji}
     11  */
     12 class GuildEmoji extends BaseGuildEmoji {
     13   /**
     14    * @name GuildEmoji
     15    * @kind constructor
     16    * @memberof GuildEmoji
     17    * @param {Client} client The instantiating client
     18    * @param {Object} data The data for the guild emoji
     19    * @param {Guild} guild The guild the guild emoji is part of
     20    */
     21 
     22   /**
     23    * The guild this emoji is part of
     24    * @type {Guild}
     25    * @name GuildEmoji#guild
     26    */
     27 
     28   _clone() {
     29     const clone = super._clone();
     30     clone._roles = this._roles.slice();
     31     return clone;
     32   }
     33 
     34   /**
     35    * Whether the emoji is deletable by the client user
     36    * @type {boolean}
     37    * @readonly
     38    */
     39   get deletable() {
     40     if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME');
     41     return !this.managed && this.guild.me.hasPermission(Permissions.FLAGS.MANAGE_EMOJIS);
     42   }
     43 
     44   /**
     45    * A manager for roles this emoji is active for.
     46    * @type {GuildEmojiRoleManager}
     47    * @readonly
     48    */
     49   get roles() {
     50     return new GuildEmojiRoleManager(this);
     51   }
     52 
     53   /**
     54    * Fetches the author for this emoji
     55    * @returns {Promise<User>}
     56    */
     57   fetchAuthor() {
     58     if (this.managed) {
     59       return Promise.reject(new Error('EMOJI_MANAGED'));
     60     } else {
     61       if (!this.guild.me) return Promise.reject(new Error('GUILD_UNCACHED_ME'));
     62       if (!this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS)) {
     63         return Promise.reject(new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild));
     64       }
     65     }
     66     return this.client.api
     67       .guilds(this.guild.id)
     68       .emojis(this.id)
     69       .get()
     70       .then(emoji => this.client.users.add(emoji.user));
     71   }
     72 
     73   /**
     74    * Data for editing an emoji.
     75    * @typedef {Object} GuildEmojiEditData
     76    * @property {string} [name] The name of the emoji
     77    * @property {Collection<Snowflake, Role>|RoleResolvable[]} [roles] Roles to restrict emoji to
     78    */
     79 
     80   /**
     81    * Edits the emoji.
     82    * @param {GuildEmojiEditData} data The new data for the emoji
     83    * @param {string} [reason] Reason for editing this emoji
     84    * @returns {Promise<GuildEmoji>}
     85    * @example
     86    * // Edit an emoji
     87    * emoji.edit({ name: 'newemoji' })
     88    *   .then(e => console.log(`Edited emoji ${e}`))
     89    *   .catch(console.error);
     90    */
     91   edit(data, reason) {
     92     const roles = data.roles ? data.roles.map(r => r.id || r) : undefined;
     93     return this.client.api
     94       .guilds(this.guild.id)
     95       .emojis(this.id)
     96       .patch({
     97         data: {
     98           name: data.name,
     99           roles,
    100         },
    101         reason,
    102       })
    103       .then(newData => {
    104         const clone = this._clone();
    105         clone._patch(newData);
    106         return clone;
    107       });
    108   }
    109 
    110   /**
    111    * Sets the name of the emoji.
    112    * @param {string} name The new name for the emoji
    113    * @param {string} [reason] Reason for changing the emoji's name
    114    * @returns {Promise<GuildEmoji>}
    115    */
    116   setName(name, reason) {
    117     return this.edit({ name }, reason);
    118   }
    119 
    120   /**
    121    * Deletes the emoji.
    122    * @param {string} [reason] Reason for deleting the emoji
    123    * @returns {Promise<GuildEmoji>}
    124    */
    125   delete(reason) {
    126     return this.client.api
    127       .guilds(this.guild.id)
    128       .emojis(this.id)
    129       .delete({ reason })
    130       .then(() => this);
    131   }
    132 
    133   /**
    134    * Whether this emoji is the same as another one.
    135    * @param {GuildEmoji|Object} other The emoji to compare it to
    136    * @returns {boolean} Whether the emoji is equal to the given emoji or not
    137    */
    138   equals(other) {
    139     if (other instanceof GuildEmoji) {
    140       return (
    141         other.id === this.id &&
    142         other.name === this.name &&
    143         other.managed === this.managed &&
    144         other.requiresColons === this.requiresColons &&
    145         other.roles.cache.size === this.roles.cache.size &&
    146         other.roles.cache.every(role => this.roles.cache.has(role.id))
    147       );
    148     } else {
    149       return (
    150         other.id === this.id &&
    151         other.name === this.name &&
    152         other.roles.length === this.roles.cache.size &&
    153         other.roles.every(role => this.roles.cache.has(role))
    154       );
    155     }
    156   }
    157 }
    158 
    159 module.exports = GuildEmoji;