buddy

node MVC discord bot
Log | Files | Refs | README

RoleManager.js (4170B)


      1 'use strict';
      2 
      3 const BaseManager = require('./BaseManager');
      4 const Role = require('../structures/Role');
      5 const Permissions = require('../util/Permissions');
      6 const { resolveColor } = require('../util/Util');
      7 
      8 /**
      9  * Manages API methods for roles and stores their cache.
     10  * @extends {BaseManager}
     11  */
     12 class RoleManager extends BaseManager {
     13   constructor(guild, iterable) {
     14     super(guild.client, iterable, Role);
     15     /**
     16      * The guild belonging to this manager
     17      * @type {Guild}
     18      */
     19     this.guild = guild;
     20   }
     21 
     22   /**
     23    * The role cache of this manager
     24    * @type {Collection<Snowflake, Role>}
     25    * @name RoleManager#cache
     26    */
     27 
     28   add(data, cache) {
     29     return super.add(data, cache, { extras: [this.guild] });
     30   }
     31 
     32   /**
     33    * Obtains one or more roles from Discord, or the role cache if they're already available.
     34    * @param {Snowflake} [id] ID or IDs of the role(s)
     35    * @param {boolean} [cache=true] Whether to cache the new roles objects if it weren't already
     36    * @returns {Promise<Role|RoleManager>}
     37    * @example
     38    * // Fetch all roles from the guild
     39    * message.guild.roles.fetch()
     40    *   .then(roles => console.log(`There are ${roles.cache.size} roles.`))
     41    *   .catch(console.error);
     42    * @example
     43    * // Fetch a single role
     44    * message.guild.roles.fetch('222078108977594368')
     45    *   .then(role => console.log(`The role color is: ${role.color}`))
     46    *   .catch(console.error);
     47    */
     48   async fetch(id, cache = true) {
     49     if (id) {
     50       const existing = this.cache.get(id);
     51       if (existing) return existing;
     52     }
     53 
     54     // We cannot fetch a single role, as of this commit's date, Discord API throws with 405
     55     const roles = await this.client.api.guilds(this.guild.id).roles.get();
     56     for (const role of roles) this.add(role, cache);
     57     return id ? this.cache.get(id) || null : this;
     58   }
     59 
     60   /**
     61    * Data that can be resolved to a Role object. This can be:
     62    * * A Role
     63    * * A Snowflake
     64    * @typedef {Role|Snowflake} RoleResolvable
     65    */
     66 
     67   /**
     68    * Resolves a RoleResolvable to a Role object.
     69    * @method resolve
     70    * @memberof RoleManager
     71    * @instance
     72    * @param {RoleResolvable} role The role resolvable to resolve
     73    * @returns {?Role}
     74    */
     75 
     76   /**
     77    * Resolves a RoleResolvable to a role ID string.
     78    * @method resolveID
     79    * @memberof RoleManager
     80    * @instance
     81    * @param {RoleResolvable} role The role resolvable to resolve
     82    * @returns {?Snowflake}
     83    */
     84 
     85   /**
     86    * Creates a new role in the guild with given information.
     87    * <warn>The position will silently reset to 1 if an invalid one is provided, or none.</warn>
     88    * @param {Object} [options] Options
     89    * @param {RoleData} [options.data] The data to create the role with
     90    * @param {string} [options.reason] Reason for creating this role
     91    * @returns {Promise<Role>}
     92    * @example
     93    * // Create a new role
     94    * guild.roles.create()
     95    *   .then(console.log)
     96    *   .catch(console.error);
     97    * @example
     98    * // Create a new role with data and a reason
     99    * guild.roles.create({
    100    *   data: {
    101    *     name: 'Super Cool People',
    102    *     color: 'BLUE',
    103    *   },
    104    *   reason: 'we needed a role for Super Cool People',
    105    * })
    106    *   .then(console.log)
    107    *   .catch(console.error);
    108    */
    109   create({ data = {}, reason } = {}) {
    110     if (data.color) data.color = resolveColor(data.color);
    111     if (data.permissions) data.permissions = Permissions.resolve(data.permissions);
    112 
    113     return this.guild.client.api
    114       .guilds(this.guild.id)
    115       .roles.post({ data, reason })
    116       .then(r => {
    117         const { role } = this.client.actions.GuildRoleCreate.handle({
    118           guild_id: this.guild.id,
    119           role: r,
    120         });
    121         if (data.position) return role.setPosition(data.position, reason);
    122         return role;
    123       });
    124   }
    125 
    126   /**
    127    * The `@everyone` role of the guild
    128    * @type {Role}
    129    * @readonly
    130    */
    131   get everyone() {
    132     return this.cache.get(this.guild.id);
    133   }
    134 
    135   /**
    136    * The role with the highest position in the cache
    137    * @type {Role}
    138    * @readonly
    139    */
    140   get highest() {
    141     return this.cache.reduce((prev, role) => (role.comparePositionTo(prev) > 0 ? role : prev), this.cache.first());
    142   }
    143 }
    144 
    145 module.exports = RoleManager;