-
Notifications
You must be signed in to change notification settings - Fork 15
/
Player.js
318 lines (284 loc) · 8.1 KB
/
Player.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const Constants = require('eris').Constants;
var EventEmitter;
try {
EventEmitter = require('eventemitter3');
} catch (err) {
EventEmitter = require('events').EventEmitter;
}
/**
* Represents a player/voice connection to Lavalink
* @extends EventEmitter
* @prop {string} id Guild id for the player
* @prop {PlayerManager} manager Reference to the player manager
* @prop {Lavalink} node Lavalink node the player is connected to
* @prop {object} shard The eris shard the player is associated with
* @prop {string} hostname Hostname of the lavalink node
* @prop {string} guildId Guild ID
* @prop {string} channelId Channel ID
* @prop {boolean} ready If the connection is ready
* @prop {boolean} playing If the player is playing
* @prop {object} state The lavalink player state
* @prop {string} track The lavalink track to play
*/
class Player extends EventEmitter {
/**
* Player constructor
* @param {string} id Guild ID
* @param {Object} data Player data
* @param {string} data.channelId The channel id of the player
* @param {string} data.guildId The guild id of the player
* @param {string} data.hostname The hostname of the lavalink node
* @param {PlayerManager} data.manager The PlayerManager associated with this player
* @param {Lavalink} data.node The Lavalink node associated with this player
* @param {Shard} data.shard The eris shard associated with this player
* @param {Object} [data.options] Additional passed from the user to the player
*/
constructor(id, { hostname, guildId, channelId, shard, node, manager, options }) {
super();
this.id = id;
this.node = node;
this.hostname = hostname;
this.guildId = guildId;
this.channelId = channelId;
this.manager = manager || null;
this.options = options;
this.ready = false;
this.playing = false;
this.paused = false;
this.shard = shard;
this.state = {};
this.track = null;
this.sendQueue = [];
this.timestamp = Date.now();
}
/**
* Check the event queue
* @private
*/
checkEventQueue() {
if (this.sendQueue.length > 0) {
let event = this.sendQueue.splice(0,1);
this.sendEvent(event[0]);
}
}
/**
* Queue an event to be sent to Lavalink
* @param {*} data The payload to queue
* @private
*/
queueEvent(data) {
if (this.sendQueue.length > 0) {
this.sendQueue.push(data);
} else {
return this.sendEvent(data);
}
}
/**
* Send a payload to Lavalink
* @param {*} data The payload to send
* @private
*/
async sendEvent(data) {
this.node.send(data);
process.nextTick(() => this.checkEventQueue());
}
/**
* Connect to the Lavalink node
* @param {Object} data The data used to connect
* @param {string} data.guildId The guild ID to connect
* @param {string} data.sessionId The voice connection session ID
* @param {object} data.event The event data from the voice server update
* @returns {void}
*/
connect(data) {
this.emit('connect');
this.queueEvent({
op: 'voiceUpdate',
guildId: data.guildId,
sessionId: data.sessionId,
event: data.event,
});
process.nextTick(() => this.emit('ready'));
}
/**
* Disconnect from Lavalink
* @param {*} [msg] An optional disconnect message
* @returns {void}
*/
disconnect(msg) {
this._disconnect();
this.emit('disconnect', msg);
}
_disconnect() {
this.playing = false;
if (this.paused) {
this.resume();
}
this.queueEvent({ op: 'destroy', guildId: this.guildId });
this.stop();
}
/**
* Play a Lavalink track
* @param {string} track The track to play
* @param {Object} [options] Optional options to send
* @returns {void}
*/
play(track, options) {
this.lastTrack = this.track;
this.track = track;
this.playOptions = options;
if (this.node.draining) {
this.state.position = 0;
return this.manager.switchNode(this);
}
let payload = Object.assign({
op: 'play',
guildId: this.guildId,
track: track,
}, options);
this.queueEvent(payload);
this.playing = !this.paused;
this.timestamp = Date.now();
}
/**
* Stop playing
* @returns {void}
*/
stop() {
let payload = {
op: 'stop',
guildId: this.guildId,
};
this.queueEvent(payload);
this.playing = false;
this.lastTrack = this.track;
this.track = null;
}
/**
* Update player state
* @param {Object} state The state object received from Lavalink
* @private
*/
stateUpdate(state) {
this.state = state;
}
/**
* Used to pause/resume the player
* @param {boolean} pause Set pause to true/false
* @returns {void}
*/
setPause(pause) {
this.node.send({
op: 'pause',
guildId: this.guildId,
pause: pause,
});
this.paused = pause;
this.playing = !pause;
}
/**
* Used to pause the player
*/
pause() {
if (this.playing) {
this.setPause(true);
}
}
/**
* Used to resume the player
*/
resume() {
if (!this.playing && this.paused) {
this.setPause(false)
}
}
/**
* Used for seeking to a track position
* @param {number} position The position to seek to
* @returns {void}
*/
seek(position) {
this.node.send({
op: 'seek',
guildId: this.guildId,
position: position,
});
}
/**
* Set the volume of the player
* @param {number} volume The volume level to set
* @returns {void}
*/
setVolume(volume) {
this.node.send({
op: 'volume',
guildId: this.guildId,
volume: volume,
});
}
/**
* Called on track end
* @param {Object} message The end reason
* @private
*/
onTrackEnd(message) {
if (message.reason !== 'REPLACED') {
this.playing = false;
this.lastTrack = this.track;
this.track = null;
}
this.emit('end', message);
}
/**
* Called on track exception
* @param {Object} message The exception encountered
* @private
*/
onTrackException(message) {
this.emit('error', message);
}
/**
* Called on track stuck
* @param {Object} message The message if exists
* @private
*/
onTrackStuck(message) {
this.stop();
process.nextTick(() => this.emit('end', message));
}
/**
* Switch voice channel
* @param {string} channelId Called when switching channels
* @param {boolean} [reactive] Used if you want the bot to switch channels
* @returns {void}
*/
switchChannel(channelId, reactive) {
if(this.channelId === channelId) {
return;
}
this.channelId = channelId;
if (reactive === true) {
this.updateVoiceState(channelId);
}
}
getTimestamp() {
return Date.now() - this.timestamp;
}
/**
* Update the bot's voice state
* @param {boolean} selfMute Whether the bot muted itself or not (audio sending is unaffected)
* @param {boolean} selfDeaf Whether the bot deafened itself or not (audio receiving is unaffected)
* @private
*/
updateVoiceState(channelId, selfMute, selfDeaf) {
if (this.shard.sendWS) {
this.shard.sendWS(Constants.GatewayOPCodes.VOICE_STATE_UPDATE, {
guild_id: this.id === 'call' ? null : this.id,
channel_id: channelId || null,
self_mute: !!selfMute,
self_deaf: !!selfDeaf,
});
}
}
}
module.exports = Player;