buddy

node MVC discord bot
Log | Files | Refs | README

VoiceChannel.js (4131B)


      1 'use strict';
      2 
      3 const GuildChannel = require('./GuildChannel');
      4 const { Error } = require('../errors');
      5 const Collection = require('../util/Collection');
      6 const { browser } = require('../util/Constants');
      7 const Permissions = require('../util/Permissions');
      8 
      9 /**
     10  * Represents a guild voice channel on Discord.
     11  * @extends {GuildChannel}
     12  */
     13 class VoiceChannel extends GuildChannel {
     14   _patch(data) {
     15     super._patch(data);
     16     /**
     17      * The bitrate of this voice channel
     18      * @type {number}
     19      */
     20     this.bitrate = data.bitrate;
     21 
     22     /**
     23      * The maximum amount of users allowed in this channel - 0 means unlimited.
     24      * @type {number}
     25      */
     26     this.userLimit = data.user_limit;
     27   }
     28 
     29   /**
     30    * The members in this voice channel
     31    * @type {Collection<Snowflake, GuildMember>}
     32    * @name VoiceChannel#members
     33    * @readonly
     34    */
     35   get members() {
     36     const coll = new Collection();
     37     for (const state of this.guild.voiceStates.cache.values()) {
     38       if (state.channelID === this.id && state.member) {
     39         coll.set(state.id, state.member);
     40       }
     41     }
     42     return coll;
     43   }
     44 
     45   /**
     46    * Checks if the voice channel is full
     47    * @type {boolean}
     48    * @readonly
     49    */
     50   get full() {
     51     return this.userLimit > 0 && this.members.size >= this.userLimit;
     52   }
     53 
     54   /**
     55    * Whether the channel is deletable by the client user
     56    * @type {boolean}
     57    * @readonly
     58    */
     59   get deletable() {
     60     return super.deletable && this.permissionsFor(this.client.user).has(Permissions.FLAGS.CONNECT, false);
     61   }
     62 
     63   /**
     64    * Whether the channel is editable by the client user
     65    * @type {boolean}
     66    * @readonly
     67    */
     68   get editable() {
     69     return this.manageable && this.permissionsFor(this.client.user).has(Permissions.FLAGS.CONNECT, false);
     70   }
     71 
     72   /**
     73    * Whether the channel is joinable by the client user
     74    * @type {boolean}
     75    * @readonly
     76    */
     77   get joinable() {
     78     if (browser) return false;
     79     if (!this.viewable) return false;
     80     if (!this.permissionsFor(this.client.user).has(Permissions.FLAGS.CONNECT, false)) return false;
     81     if (this.full && !this.permissionsFor(this.client.user).has(Permissions.FLAGS.MOVE_MEMBERS, false)) return false;
     82     return true;
     83   }
     84 
     85   /**
     86    * Checks if the client has permission to send audio to the voice channel
     87    * @type {boolean}
     88    * @readonly
     89    */
     90   get speakable() {
     91     return this.permissionsFor(this.client.user).has(Permissions.FLAGS.SPEAK, false);
     92   }
     93 
     94   /**
     95    * Sets the bitrate of the channel.
     96    * @param {number} bitrate The new bitrate
     97    * @param {string} [reason] Reason for changing the channel's bitrate
     98    * @returns {Promise<VoiceChannel>}
     99    * @example
    100    * // Set the bitrate of a voice channel
    101    * voiceChannel.setBitrate(48000)
    102    *   .then(vc => console.log(`Set bitrate to ${vc.bitrate}bps for ${vc.name}`))
    103    *   .catch(console.error);
    104    */
    105   setBitrate(bitrate, reason) {
    106     return this.edit({ bitrate }, reason);
    107   }
    108 
    109   /**
    110    * Sets the user limit of the channel.
    111    * @param {number} userLimit The new user limit
    112    * @param {string} [reason] Reason for changing the user limit
    113    * @returns {Promise<VoiceChannel>}
    114    * @example
    115    * // Set the user limit of a voice channel
    116    * voiceChannel.setUserLimit(42)
    117    *   .then(vc => console.log(`Set user limit to ${vc.userLimit} for ${vc.name}`))
    118    *   .catch(console.error);
    119    */
    120   setUserLimit(userLimit, reason) {
    121     return this.edit({ userLimit }, reason);
    122   }
    123 
    124   /**
    125    * Attempts to join this voice channel.
    126    * @returns {Promise<VoiceConnection>}
    127    * @example
    128    * // Join a voice channel
    129    * voiceChannel.join()
    130    *   .then(connection => console.log('Connected!'))
    131    *   .catch(console.error);
    132    */
    133   join() {
    134     if (browser) return Promise.reject(new Error('VOICE_NO_BROWSER'));
    135     return this.client.voice.joinChannel(this);
    136   }
    137 
    138   /**
    139    * Leaves this voice channel.
    140    * @example
    141    * // Leave a voice channel
    142    * voiceChannel.leave();
    143    */
    144   leave() {
    145     if (browser) return;
    146     const connection = this.client.voice.connections.get(this.guild.id);
    147     if (connection && connection.channel.id === this.id) connection.disconnect();
    148   }
    149 }
    150 
    151 module.exports = VoiceChannel;