-
Notifications
You must be signed in to change notification settings - Fork 15
/
PlayerManager.js
445 lines (392 loc) · 14.1 KB
/
PlayerManager.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* Created by Julian & NoobLance on 25.05.2017.
* DISCLAIMER: We reuse some eris code
*/
const { Collection } = require('eris');
const Lavalink = require('./Lavalink');
const Player = require('./Player');
/**
* Player Manager
* @extends Map
* @prop {Player} baseObject The player class used to create new players
* @prop {object} client The eris client
* @prop {object} defaultRegions The default region config
* @prop {object} regions The region config being used
*/
class PlayerManager extends Collection {
/**
* PlayerManager constructor
* @param {Client} client Eris client
* @param {Object[]} nodes The Lavalink nodes to connect to
* @param {Object} [options] Setup options
* @param {string} [options.defaultRegion] The default region
* @param {number} [options.failoverRate=250] Failover rate in ms
* @param {number} [options.failoverLimit=1] Number of connections to failover per rate limit
* @param {Object} [options.player] Optional Player class to replace the default Player
* @param {number} [options.reconnectThreshold=2000] The amount of time to skip ahead in a song when reconnecting in ms
* @param {Object} [options.regions] Region mapping object
*/
constructor(client, nodes, options) {
super(options.player || Player);
this.client = client;
this.nodes = new Collection();
this.pendingGuilds = {};
this.options = options || {};
this.failoverQueue = [];
this.failoverRate = options.failoverRate || 250;
this.failoverLimit = options.failoverLimit || 1;
this.defaultRegions = {
asia: ['hongkong', 'singapore', 'sydney'],
eu: ['eu', 'amsterdam', 'frankfurt', 'russia'],
us: ['us', 'brazil'],
};
this.regions = options.regions || this.defaultRegions;
for (let node of nodes) {
this.createNode(Object.assign({}, node, options));
}
this.shardReadyListener = this.shardReady.bind(this);
this.client.on('shardReady', this.shardReadyListener);
}
/**
* Create a Lavalink node
* @param {Object} options Lavalink node options
* @param {string} options.host The hostname to connect to
* @param {string} options.port The port to connect with
* @param {string} options.region The region of the node
* @param {number} options.numShards The number of shards the bot is running
* @param {string} options.userId The user id of the bot
* @param {string} options.password The password for the Lavalink node
* @returns {void}
*/
createNode(options) {
let node = new Lavalink({
host: options.host,
port: options.port,
secure: options.secure,
region: options.region,
numShards: options.numShards,
userId: options.userId,
password: options.password,
});
node.on('error', this.onError.bind(this, node));
node.on('disconnect', this.onDisconnect.bind(this, node));
node.on('message', this.onMessage.bind(this, node));
this.nodes.set(options.host, node);
}
/**
* Remove a Lavalink node
* @param {string} host The hostname of the node
* @returns {void}
*/
removeNode(host) {
let node = this.nodes.get(host);
if (!host) return;
node.destroy();
this.nodes.delete(host);
this.onDisconnect(node);
}
/**
* Check the failover queue
* @private
*/
checkFailoverQueue() {
if (this.failoverQueue.length > 0) {
let fns = this.failoverQueue.splice(0, this.failoverLimit);
for (let fn of fns) {
this.processQueue(fn);
}
}
}
/**
* Queue a failover
* @param {Function} fn The failover function to queue
* @private
*/
queueFailover(fn) {
if (this.failoverQueue.length > 0) {
this.failoverQueue.push(fn);
} else {
return this.processQueue(fn);
}
}
/**
* Process the failover queue
* @param {Function} fn The failover function to call
* @private
*/
processQueue(fn) {
fn();
setTimeout(() => this.checkFailoverQueue(), this.failoverRate);
}
/**
* Called when an error is received from a Lavalink node
* @param {Lavalink} node The Lavalink node
* @param {string|Error} err The error received
* @private
*/
onError(node, err) {
this.client.emit('error', err);
}
/**
* Called when a node disconnects
* @param {Lavalink} node The Lavalink node
* @param {*} msg The disconnect message if sent
* @private
*/
onDisconnect(node, msg) {
let players = this.filter(player => player.node.host === node.host);
for (let player of players) {
this.queueFailover(this.switchNode.bind(this, player, true));
}
}
/**
* Called when a shard readies
* @param {number} id Shard ID
* @private
*/
shardReady(id) {
let players = this.filter(player => player.shard && player.shard.id === id);
for (let player of players) {
this.queueFailover(this.switchNode.bind(this, player));
}
}
/**
* Switch the voice node of a player
* @param {Player} player The Player instance
* @param {boolean} leave Whether to leave the channel or not on our side
* @returns {void}
*/
switchNode(player, leave) {
let { guildId, channelId, track, paused } = player,
position = (player.state.position || 0) + (this.options.reconnectThreshold || 2000);
let listeners = player.listeners('end'),
endListeners = [];
if (listeners && listeners.length) {
for (let listener of listeners) {
endListeners.push(listener);
player.removeListener('end', listener);
}
}
player.once('end', () => {
for (let listener of endListeners) {
player.on('end', listener);
}
});
this.delete(guildId);
player.playing = false;
if (leave) {
player.updateVoiceState(null);
}
process.nextTick(() => {
this.join(guildId, channelId, null, player).then(player => {
if (paused) {
player.pause();
}
player.play(track, { startTime: position });
player.emit('reconnect');
this.set(guildId, player);
})
.catch(err => {
player.disconnect();
});
});
}
/**
* Called when a message is received from the voice node
* @param {Lavalink} node The Lavalink node
* @param {*} message The message received
* @private
*/
onMessage(node, message) {
if (!message.op) return;
switch (message.op) {
case 'playerUpdate': {
let player = this.get(message.guildId);
if (!player) return;
return player.stateUpdate(message.state);
}
case 'event': {
let player = this.get(message.guildId);
if (!player) return;
switch (message.type) {
case 'TrackEndEvent':
return player.onTrackEnd(message);
case 'TrackExceptionEvent':
return player.onTrackException(message);
case 'TrackStuckEvent':
return player.onTrackStuck(message);
default:
return player.emit('warn', `Unexpected event type: ${message.type}`);
}
}
}
}
/**
* Join a voice channel
* @param {string} guildId The guild ID
* @param {string} channelId The channel ID
* @param {Object} options Join options
* @param {Player} [player] Optionally pass an existing player
* @returns {Promise<Player>}
*/
async join(guildId, channelId, options, player) {
options = options || {};
player = player || this.get(guildId);
if (player && player.channelId !== channelId) {
player.switchChannel(channelId);
return Promise.resolve(player);
}
let region = this.getRegionFromData(options.region || 'us');
let node = await this.findIdealNode(region);
if (!node) {
return Promise.reject('No available voice nodes.');
}
return new Promise((res, rej) => {
this.pendingGuilds[guildId] = {
channelId: channelId,
options: options || {},
player: player || null,
node: node,
res: res,
rej: rej,
timeout: setTimeout(() => {
delete this.pendingGuilds[guildId];
rej(new Error('Voice connection timeout'));
}, 10000),
};
});
}
/**
* Leave a voice channel
* @param {string} guildId The guild ID
* @returns {void}
*/
async leave(guildId) {
let player = this.get(guildId);
if (!player) {
return;
}
player._disconnect();
this.delete(guildId);
}
/**
* Find the ideal voice node based on load and region
* @param {string} region Guild region
* @returns {Lavalink} node Node with the lowest load for a region
*/
async findIdealNode(region) {
let nodes = [...this.nodes.values()].filter(node => !node.draining && node.ws && node.connected);
if (region) {
let regionalNodes = nodes.filter(node => node.region === region);
if (regionalNodes && regionalNodes.length) {
nodes = regionalNodes;
}
}
nodes = nodes.sort((a, b) => {
let aload = a.stats.cpu ? (a.stats.cpu.systemLoad / a.stats.cpu.cores) * 100 : 0,
bload = b.stats.cpu ? (b.stats.cpu.systemLoad / b.stats.cpu.cores) * 100 : 0;
return aload - bload;
});
return nodes[0];
}
/**
* Called by eris when a voice server update is received
* @param {*} data The voice server update from eris
* @private
*/
async voiceServerUpdate(data) {
if (this.pendingGuilds[data.guild_id] && this.pendingGuilds[data.guild_id].timeout) {
clearTimeout(this.pendingGuilds[data.guild_id].timeout);
this.pendingGuilds[data.guild_id].timeout = null;
}
let player = this.get(data.guild_id);
if (!player) {
if (!this.pendingGuilds[data.guild_id]) {
return;
}
player = this.pendingGuilds[data.guild_id].player;
if (player) {
player.sessionId = data.sessionId;
player.hostname = this.pendingGuilds[data.guild_id].hostname;
player.node = this.pendingGuilds[data.guild_id].node;
player.event = data;
this.set(data.guild_id, player);
}
player = player || this.add(new this.baseObject(data.guild_id, {
shard: data.shard,
guildId: data.guild_id,
sessionId: data.session_id,
channelId: this.pendingGuilds[data.guild_id].channelId,
hostname: this.pendingGuilds[data.guild_id].hostname,
node: this.pendingGuilds[data.guild_id].node,
options: this.pendingGuilds[data.guild_id].options,
event: data,
manager: this,
}));
}
const channelId = player.channelId || this.pendingGuilds[data.guild_id].channelId;
if (!channelId) {
if (this.pendingGuilds[data.guild_id]) {
delete this.pendingGuilds[data.guild_id];
return this.pendingGuilds[data.guild_id].rej(new Error('Invalid Channel ID'));
}
return;
}
player.connect({
sessionId: data.session_id,
guildId: data.guild_id,
channelId: channelId,
event: {
endpoint: data.endpoint,
guild_id: data.guild_id,
token: data.token,
},
});
let disconnectHandler = () => {
player = this.get(data.guild_id);
if (!this.pendingGuilds[data.guild_id]) {
if (player) {
player.removeListener('ready', readyHandler);
}
return;
}
player.removeListener('ready', readyHandler);
this.pendingGuilds[data.guild_id].rej(new Error('Disconnected'));
delete this.pendingGuilds[data.guild_id];
};
let readyHandler = () => {
player = this.get(data.guild_id);
if (!this.pendingGuilds[data.guild_id]) {
if (player) {
player.removeListener('disconnect', disconnectHandler);
}
return;
}
player.removeListener('disconnect', disconnectHandler);
this.pendingGuilds[data.guild_id].res(player);
delete this.pendingGuilds[data.guild_id];
};
player.once('ready', readyHandler).once('disconnect', disconnectHandler);
}
/**
* Get ideal region from data
* @param {string} endpoint Endpoint or region
* @private
*/
getRegionFromData(endpoint) {
if (!endpoint) return this.options.defaultRegion || 'us';
endpoint = endpoint.replace('vip-', '');
for (let key in this.regions) {
let nodes = this.nodes.filter(n => n.region === key);
if (!nodes || !nodes.length) continue;
if (!nodes.find(n => n.connected && !n.draining)) continue;
for (let region of this.regions[key]) {
if (endpoint.startsWith(region)) {
return key;
}
}
}
return this.options.defaultRegion || 'us';
}
}
module.exports = PlayerManager;