GuildEmojiRoleManager.js (3523B)
1 'use strict'; 2 3 const { TypeError } = require('../errors'); 4 const Collection = require('../util/Collection'); 5 6 /** 7 * Manages API methods for roles belonging to emojis and stores their cache. 8 */ 9 class GuildEmojiRoleManager { 10 constructor(emoji) { 11 /** 12 * The emoji belonging to this manager 13 * @type {GuildEmoji} 14 */ 15 this.emoji = emoji; 16 /** 17 * The guild belonging to this manager 18 * @type {Guild} 19 */ 20 this.guild = emoji.guild; 21 /** 22 * The client belonging to this manager 23 * @type {Client} 24 * @readonly 25 */ 26 Object.defineProperty(this, 'client', { value: emoji.client }); 27 } 28 29 /** 30 * The filtered collection of roles of the guild emoji 31 * @type {Collection<Snowflake, Role>} 32 * @private 33 * @readonly 34 */ 35 get _roles() { 36 return this.guild.roles.cache.filter(role => this.emoji._roles.includes(role.id)); 37 } 38 39 /** 40 * The cache of roles belonging to this emoji 41 * @type {Collection<Snowflake, Role>} 42 * @readonly 43 */ 44 get cache() { 45 return this._roles; 46 } 47 48 /** 49 * Adds a role (or multiple roles) to the list of roles that can use this emoji. 50 * @param {RoleResolvable|RoleResolvable[]|Collection<Snowflake, Role>} roleOrRoles The role or roles to add 51 * @returns {Promise<GuildEmoji>} 52 */ 53 add(roleOrRoles) { 54 if (roleOrRoles instanceof Collection) return this.add(roleOrRoles.keyArray()); 55 if (!Array.isArray(roleOrRoles)) return this.add([roleOrRoles]); 56 roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r)); 57 58 if (roleOrRoles.includes(null)) { 59 return Promise.reject(new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true)); 60 } 61 62 const newRoles = [...new Set(roleOrRoles.concat(...this._roles.values()))]; 63 return this.set(newRoles); 64 } 65 66 /** 67 * Removes a role (or multiple roles) from the list of roles that can use this emoji. 68 * @param {RoleResolvable|RoleResolvable[]|Collection<Snowflake, Role>} roleOrRoles The role or roles to remove 69 * @returns {Promise<GuildEmoji>} 70 */ 71 remove(roleOrRoles) { 72 if (roleOrRoles instanceof Collection) return this.remove(roleOrRoles.keyArray()); 73 if (!Array.isArray(roleOrRoles)) return this.remove([roleOrRoles]); 74 roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolveID(r)); 75 76 if (roleOrRoles.includes(null)) { 77 return Promise.reject(new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true)); 78 } 79 80 const newRoles = this._roles.keyArray().filter(role => !roleOrRoles.includes(role)); 81 return this.set(newRoles); 82 } 83 84 /** 85 * Sets the role(s) that can use this emoji. 86 * @param {Collection<Snowflake, Role>|RoleResolvable[]} roles The roles or role IDs to apply 87 * @returns {Promise<GuildEmoji>} 88 * @example 89 * // Set the emoji's roles to a single role 90 * guildEmoji.roles.set(['391156570408615936']) 91 * .then(console.log) 92 * .catch(console.error); 93 * @example 94 * // Remove all roles from an emoji 95 * guildEmoji.roles.set([]) 96 * .then(console.log) 97 * .catch(console.error); 98 */ 99 set(roles) { 100 return this.emoji.edit({ roles }); 101 } 102 103 clone() { 104 const clone = new this.constructor(this.emoji); 105 clone._patch(this._roles.keyArray().slice()); 106 return clone; 107 } 108 109 /** 110 * Patches the roles for this manager's cache 111 * @param {Snowflake[]} roles The new roles 112 * @private 113 */ 114 _patch(roles) { 115 this.emoji._roles = roles; 116 } 117 } 118 119 module.exports = GuildEmojiRoleManager;