buddy

node MVC discord bot
Log | Files | Refs | README

GuildEmojiManager.js (4209B)


      1 'use strict';
      2 
      3 const BaseManager = require('./BaseManager');
      4 const { TypeError } = require('../errors');
      5 const GuildEmoji = require('../structures/GuildEmoji');
      6 const ReactionEmoji = require('../structures/ReactionEmoji');
      7 const Collection = require('../util/Collection');
      8 const DataResolver = require('../util/DataResolver');
      9 
     10 /**
     11  * Manages API methods for GuildEmojis and stores their cache.
     12  * @extends {BaseManager}
     13  */
     14 class GuildEmojiManager extends BaseManager {
     15   constructor(guild, iterable) {
     16     super(guild.client, iterable, GuildEmoji);
     17     /**
     18      * The guild this manager belongs to
     19      * @type {Guild}
     20      */
     21     this.guild = guild;
     22   }
     23 
     24   /**
     25    * The cache of GuildEmojis
     26    * @type {Collection<Snowflake, GuildEmoji>}
     27    * @name GuildEmojiManager#cache
     28    */
     29 
     30   add(data, cache) {
     31     return super.add(data, cache, { extras: [this.guild] });
     32   }
     33 
     34   /**
     35    * Creates a new custom emoji in the guild.
     36    * @param {BufferResolvable|Base64Resolvable} attachment The image for the emoji
     37    * @param {string} name The name for the emoji
     38    * @param {Object} [options] Options
     39    * @param {Collection<Snowflake, Role>|RoleResolvable[]} [options.roles] Roles to limit the emoji to
     40    * @param {string} [options.reason] Reason for creating the emoji
     41    * @returns {Promise<Emoji>} The created emoji
     42    * @example
     43    * // Create a new emoji from a url
     44    * guild.emojis.create('https://i.imgur.com/w3duR07.png', 'rip')
     45    *   .then(emoji => console.log(`Created new emoji with name ${emoji.name}!`))
     46    *   .catch(console.error);
     47    * @example
     48    * // Create a new emoji from a file on your computer
     49    * guild.emojis.create('./memes/banana.png', 'banana')
     50    *   .then(emoji => console.log(`Created new emoji with name ${emoji.name}!`))
     51    *   .catch(console.error);
     52    */
     53   async create(attachment, name, { roles, reason } = {}) {
     54     attachment = await DataResolver.resolveImage(attachment);
     55     if (!attachment) throw new TypeError('REQ_RESOURCE_TYPE');
     56 
     57     const data = { image: attachment, name };
     58     if (roles) {
     59       data.roles = [];
     60       for (let role of roles instanceof Collection ? roles.values() : roles) {
     61         role = this.guild.roles.resolve(role);
     62         if (!role) {
     63           return Promise.reject(
     64             new TypeError('INVALID_TYPE', 'options.roles', 'Array or Collection of Roles or Snowflakes', true),
     65           );
     66         }
     67         data.roles.push(role.id);
     68       }
     69     }
     70 
     71     return this.client.api
     72       .guilds(this.guild.id)
     73       .emojis.post({ data, reason })
     74       .then(emoji => this.client.actions.GuildEmojiCreate.handle(this.guild, emoji).emoji);
     75   }
     76 
     77   /**
     78    * Data that can be resolved into an GuildEmoji object. This can be:
     79    * * A custom emoji ID
     80    * * A GuildEmoji object
     81    * * A ReactionEmoji object
     82    * @typedef {Snowflake|GuildEmoji|ReactionEmoji} EmojiResolvable
     83    */
     84 
     85   /**
     86    * Resolves an EmojiResolvable to an Emoji object.
     87    * @param {EmojiResolvable} emoji The Emoji resolvable to identify
     88    * @returns {?GuildEmoji}
     89    */
     90   resolve(emoji) {
     91     if (emoji instanceof ReactionEmoji) return super.resolve(emoji.id);
     92     return super.resolve(emoji);
     93   }
     94 
     95   /**
     96    * Resolves an EmojiResolvable to an Emoji ID string.
     97    * @param {EmojiResolvable} emoji The Emoji resolvable to identify
     98    * @returns {?Snowflake}
     99    */
    100   resolveID(emoji) {
    101     if (emoji instanceof ReactionEmoji) return emoji.id;
    102     return super.resolveID(emoji);
    103   }
    104 
    105   /**
    106    * Data that can be resolved to give an emoji identifier. This can be:
    107    * * The unicode representation of an emoji
    108    * * An EmojiResolvable
    109    * @typedef {string|EmojiResolvable} EmojiIdentifierResolvable
    110    */
    111 
    112   /**
    113    * Resolves an EmojiResolvable to an emoji identifier.
    114    * @param {EmojiIdentifierResolvable} emoji The emoji resolvable to resolve
    115    * @returns {?string}
    116    */
    117   resolveIdentifier(emoji) {
    118     const emojiResolvable = this.resolve(emoji);
    119     if (emojiResolvable) return emojiResolvable.identifier;
    120     if (emoji instanceof ReactionEmoji) return emoji.identifier;
    121     if (typeof emoji === 'string') {
    122       if (!emoji.includes('%')) return encodeURIComponent(emoji);
    123       else return emoji;
    124     }
    125     return null;
    126   }
    127 }
    128 
    129 module.exports = GuildEmojiManager;