buddy

node MVC discord bot
Log | Files | Refs | README

Permissions.js (4322B)


      1 'use strict';
      2 
      3 const BitField = require('./BitField');
      4 
      5 /**
      6  * Data structure that makes it easy to interact with a permission bitfield. All {@link GuildMember}s have a set of
      7  * permissions in their guild, and each channel in the guild may also have {@link PermissionOverwrites} for the member
      8  * that override their default permissions.
      9  * @extends {BitField}
     10  */
     11 class Permissions extends BitField {
     12   /**
     13    * @name Permissions
     14    * @kind constructor
     15    * @memberof Permissions
     16    * @param {PermissionResolvable} [bits=0] Bit(s) to read from
     17    */
     18 
     19   /**
     20    * Data that can be resolved to give a permission number. This can be:
     21    * * A string (see {@link Permissions.FLAGS})
     22    * * A permission number
     23    * * An instance of Permissions
     24    * * An Array of PermissionResolvable
     25    * @typedef {string|number|Permissions|PermissionResolvable[]} PermissionResolvable
     26    */
     27 
     28   /**
     29    * Checks whether the bitfield has a permission, or any of multiple permissions.
     30    * @param {PermissionResolvable} permission Permission(s) to check for
     31    * @param {boolean} [checkAdmin=true] Whether to allow the administrator permission to override
     32    * @returns {boolean}
     33    */
     34   any(permission, checkAdmin = true) {
     35     return (checkAdmin && super.has(this.constructor.FLAGS.ADMINISTRATOR)) || super.any(permission);
     36   }
     37 
     38   /**
     39    * Checks whether the bitfield has a permission, or multiple permissions.
     40    * @param {PermissionResolvable} permission Permission(s) to check for
     41    * @param {boolean} [checkAdmin=true] Whether to allow the administrator permission to override
     42    * @returns {boolean}
     43    */
     44   has(permission, checkAdmin = true) {
     45     return (checkAdmin && super.has(this.constructor.FLAGS.ADMINISTRATOR)) || super.has(permission);
     46   }
     47 }
     48 
     49 /**
     50  * Numeric permission flags. All available properties:
     51  * * `ADMINISTRATOR` (implicitly has *all* permissions, and bypasses all channel overwrites)
     52  * * `CREATE_INSTANT_INVITE` (create invitations to the guild)
     53  * * `KICK_MEMBERS`
     54  * * `BAN_MEMBERS`
     55  * * `MANAGE_CHANNELS` (edit and reorder channels)
     56  * * `MANAGE_GUILD` (edit the guild information, region, etc.)
     57  * * `ADD_REACTIONS` (add new reactions to messages)
     58  * * `VIEW_AUDIT_LOG`
     59  * * `PRIORITY_SPEAKER`
     60  * * `STREAM`
     61  * * `VIEW_CHANNEL`
     62  * * `SEND_MESSAGES`
     63  * * `SEND_TTS_MESSAGES`
     64  * * `MANAGE_MESSAGES` (delete messages and reactions)
     65  * * `EMBED_LINKS` (links posted will have a preview embedded)
     66  * * `ATTACH_FILES`
     67  * * `READ_MESSAGE_HISTORY` (view messages that were posted prior to opening Discord)
     68  * * `MENTION_EVERYONE`
     69  * * `USE_EXTERNAL_EMOJIS` (use emojis from different guilds)
     70  * * `VIEW_GUILD_INSIGHTS`
     71  * * `CONNECT` (connect to a voice channel)
     72  * * `SPEAK` (speak in a voice channel)
     73  * * `MUTE_MEMBERS` (mute members across all voice channels)
     74  * * `DEAFEN_MEMBERS` (deafen members across all voice channels)
     75  * * `MOVE_MEMBERS` (move members between voice channels)
     76  * * `USE_VAD` (use voice activity detection)
     77  * * `CHANGE_NICKNAME`
     78  * * `MANAGE_NICKNAMES` (change other members' nicknames)
     79  * * `MANAGE_ROLES`
     80  * * `MANAGE_WEBHOOKS`
     81  * * `MANAGE_EMOJIS`
     82  * @type {Object}
     83  * @see {@link https://discordapp.com/developers/docs/topics/permissions}
     84  */
     85 Permissions.FLAGS = {
     86   CREATE_INSTANT_INVITE: 1 << 0,
     87   KICK_MEMBERS: 1 << 1,
     88   BAN_MEMBERS: 1 << 2,
     89   ADMINISTRATOR: 1 << 3,
     90   MANAGE_CHANNELS: 1 << 4,
     91   MANAGE_GUILD: 1 << 5,
     92   ADD_REACTIONS: 1 << 6,
     93   VIEW_AUDIT_LOG: 1 << 7,
     94   PRIORITY_SPEAKER: 1 << 8,
     95   STREAM: 1 << 9,
     96   VIEW_CHANNEL: 1 << 10,
     97   SEND_MESSAGES: 1 << 11,
     98   SEND_TTS_MESSAGES: 1 << 12,
     99   MANAGE_MESSAGES: 1 << 13,
    100   EMBED_LINKS: 1 << 14,
    101   ATTACH_FILES: 1 << 15,
    102   READ_MESSAGE_HISTORY: 1 << 16,
    103   MENTION_EVERYONE: 1 << 17,
    104   USE_EXTERNAL_EMOJIS: 1 << 18,
    105   VIEW_GUILD_INSIGHTS: 1 << 19,
    106   CONNECT: 1 << 20,
    107   SPEAK: 1 << 21,
    108   MUTE_MEMBERS: 1 << 22,
    109   DEAFEN_MEMBERS: 1 << 23,
    110   MOVE_MEMBERS: 1 << 24,
    111   USE_VAD: 1 << 25,
    112   CHANGE_NICKNAME: 1 << 26,
    113   MANAGE_NICKNAMES: 1 << 27,
    114   MANAGE_ROLES: 1 << 28,
    115   MANAGE_WEBHOOKS: 1 << 29,
    116   MANAGE_EMOJIS: 1 << 30,
    117 };
    118 
    119 /**
    120  * Bitfield representing every permission combined
    121  * @type {number}
    122  */
    123 Permissions.ALL = Object.values(Permissions.FLAGS).reduce((all, p) => all | p, 0);
    124 
    125 /**
    126  * Bitfield representing the default permissions for users
    127  * @type {number}
    128  */
    129 Permissions.DEFAULT = 104324673;
    130 
    131 module.exports = Permissions;