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