aqualink 2.18.1 → 2.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/handlers/autoplay.js +148 -121
- package/build/handlers/fetchImage.js +152 -148
- package/build/index.d.ts +1401 -1080
- package/build/index.js +23 -12
- package/build/structures/Aqua.js +1085 -896
- package/build/structures/AqualinkEvents.js +43 -45
- package/build/structures/Connection.js +576 -483
- package/build/structures/Filters.js +351 -244
- package/build/structures/Node.js +666 -548
- package/build/structures/Player.js +1143 -904
- package/build/structures/Plugins.js +10 -11
- package/build/structures/Queue.js +129 -111
- package/build/structures/Rest.js +856 -645
- package/build/structures/Track.js +158 -129
- package/package.json +1 -1
package/build/structures/Aqua.js
CHANGED
|
@@ -1,897 +1,1086 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
const
|
|
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
|
-
this.
|
|
100
|
-
this.
|
|
101
|
-
this.
|
|
102
|
-
this.
|
|
103
|
-
this.
|
|
104
|
-
this.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
this.
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
this.
|
|
116
|
-
this.
|
|
117
|
-
this.
|
|
118
|
-
this.
|
|
119
|
-
this.
|
|
120
|
-
this.
|
|
121
|
-
this.
|
|
122
|
-
this.
|
|
123
|
-
this.
|
|
124
|
-
this.
|
|
125
|
-
this.
|
|
126
|
-
|
|
127
|
-
this.
|
|
128
|
-
|
|
129
|
-
this.
|
|
130
|
-
this.
|
|
131
|
-
this.
|
|
132
|
-
this.
|
|
133
|
-
this.
|
|
134
|
-
this.
|
|
135
|
-
this.
|
|
136
|
-
this.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
if (!
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
this.
|
|
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
|
-
this.
|
|
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
|
-
this.
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
this.
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
this.
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
this.
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
if (
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
this.
|
|
312
|
-
this
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
const
|
|
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
|
-
this.
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
this.
|
|
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
|
-
const
|
|
396
|
-
if (
|
|
397
|
-
|
|
398
|
-
this.
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
this.
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
this.
|
|
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
|
-
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
const
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
if (
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
if (
|
|
645
|
-
}
|
|
646
|
-
return
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
if (
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
const
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
if (
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
} catch
|
|
824
|
-
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
async
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
}
|
|
839
|
-
const
|
|
840
|
-
if (
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
1
|
+
const fs = require('node:fs')
|
|
2
|
+
const readline = require('node:readline')
|
|
3
|
+
const { EventEmitter } = require('tseep')
|
|
4
|
+
const { AqualinkEvents } = require('./AqualinkEvents')
|
|
5
|
+
const Node = require('./Node')
|
|
6
|
+
const Player = require('./Player')
|
|
7
|
+
const Track = require('./Track')
|
|
8
|
+
const { version: pkgVersion } = require('../../package.json')
|
|
9
|
+
|
|
10
|
+
const SEARCH_PREFIX = ':'
|
|
11
|
+
const EMPTY_ARRAY = Object.freeze([])
|
|
12
|
+
const EMPTY_TRACKS_RESPONSE = Object.freeze({
|
|
13
|
+
loadType: 'empty',
|
|
14
|
+
exception: null,
|
|
15
|
+
playlistInfo: null,
|
|
16
|
+
pluginInfo: {},
|
|
17
|
+
tracks: EMPTY_ARRAY
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const MAX_CONCURRENT_OPS = 10
|
|
21
|
+
const BROKEN_PLAYER_TTL = 300000
|
|
22
|
+
const FAILOVER_CLEANUP_TTL = 600000
|
|
23
|
+
const PLAYER_BATCH_SIZE = 20
|
|
24
|
+
const RECONNECT_DELAY = 400
|
|
25
|
+
const CACHE_VALID_TIME = 12000
|
|
26
|
+
const NODE_TIMEOUT = 30000
|
|
27
|
+
const MAX_CACHE_SIZE = 20
|
|
28
|
+
const MAX_FAILOVER_QUEUE = 50
|
|
29
|
+
const MAX_REBUILD_LOCKS = 100
|
|
30
|
+
const WRITE_BUFFER_SIZE = 100
|
|
31
|
+
|
|
32
|
+
const DEFAULT_OPTIONS = Object.freeze({
|
|
33
|
+
shouldDeleteMessage: false,
|
|
34
|
+
defaultSearchPlatform: 'ytsearch',
|
|
35
|
+
leaveOnEnd: false,
|
|
36
|
+
restVersion: 'v4',
|
|
37
|
+
plugins: [],
|
|
38
|
+
autoResume: true,
|
|
39
|
+
infiniteReconnects: true,
|
|
40
|
+
loadBalancer: 'leastLoad',
|
|
41
|
+
useHttp2: false,
|
|
42
|
+
failoverOptions: Object.freeze({
|
|
43
|
+
enabled: true,
|
|
44
|
+
maxRetries: 3,
|
|
45
|
+
retryDelay: 1000,
|
|
46
|
+
preservePosition: true,
|
|
47
|
+
resumePlayback: true,
|
|
48
|
+
cooldownTime: 5000,
|
|
49
|
+
maxFailoverAttempts: 5
|
|
50
|
+
}),
|
|
51
|
+
maxQueueSave: 10,
|
|
52
|
+
maxTracksRestore: 20
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const _functions = {
|
|
56
|
+
delay: (ms) =>
|
|
57
|
+
new Promise((r) => {
|
|
58
|
+
const t = setTimeout(r, ms)
|
|
59
|
+
t.unref?.()
|
|
60
|
+
}),
|
|
61
|
+
noop: () => {},
|
|
62
|
+
isUrl: (query) => {
|
|
63
|
+
if (typeof query !== 'string' || query.length <= 8) return false
|
|
64
|
+
const q = query.trimStart()
|
|
65
|
+
return q.startsWith('http://') || q.startsWith('https://')
|
|
66
|
+
},
|
|
67
|
+
formatQuery(query, source) {
|
|
68
|
+
return this.isUrl(query) ? query : `${source}${SEARCH_PREFIX}${query}`
|
|
69
|
+
},
|
|
70
|
+
makeTrack: (t, requester, node) => new Track(t, requester, node),
|
|
71
|
+
safeCall(fn) {
|
|
72
|
+
try {
|
|
73
|
+
const result = fn()
|
|
74
|
+
return result?.then ? result.catch(this.noop) : result
|
|
75
|
+
} catch {}
|
|
76
|
+
},
|
|
77
|
+
parseRequester(str) {
|
|
78
|
+
if (!str || typeof str !== 'string') return null
|
|
79
|
+
const i = str.indexOf(':')
|
|
80
|
+
return i > 0
|
|
81
|
+
? { id: str.substring(0, i), username: str.substring(i + 1) }
|
|
82
|
+
: null
|
|
83
|
+
},
|
|
84
|
+
unrefTimeout: (fn, ms) => {
|
|
85
|
+
const t = setTimeout(fn, ms)
|
|
86
|
+
t.unref?.()
|
|
87
|
+
return t
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
class Aqua extends EventEmitter {
|
|
92
|
+
constructor(client, nodes, options = {}) {
|
|
93
|
+
super()
|
|
94
|
+
if (!client) throw new Error('Client is required')
|
|
95
|
+
if (!Array.isArray(nodes) || !nodes.length)
|
|
96
|
+
throw new TypeError('Nodes must be non-empty Array')
|
|
97
|
+
|
|
98
|
+
this.client = client
|
|
99
|
+
this.nodes = nodes
|
|
100
|
+
this.nodeMap = new Map()
|
|
101
|
+
this.players = new Map()
|
|
102
|
+
this.clientId = null
|
|
103
|
+
this.initiated = false
|
|
104
|
+
this.version = pkgVersion
|
|
105
|
+
|
|
106
|
+
const merged = { ...DEFAULT_OPTIONS, ...options }
|
|
107
|
+
this.options = merged
|
|
108
|
+
this.failoverOptions = {
|
|
109
|
+
...DEFAULT_OPTIONS.failoverOptions,
|
|
110
|
+
...options.failoverOptions
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this.shouldDeleteMessage = merged.shouldDeleteMessage
|
|
114
|
+
this.defaultSearchPlatform = merged.defaultSearchPlatform
|
|
115
|
+
this.leaveOnEnd = merged.leaveOnEnd
|
|
116
|
+
this.restVersion = merged.restVersion || 'v4'
|
|
117
|
+
this.plugins = merged.plugins
|
|
118
|
+
this.autoResume = merged.autoResume
|
|
119
|
+
this.infiniteReconnects = merged.infiniteReconnects
|
|
120
|
+
this.urlFilteringEnabled = merged.urlFilteringEnabled
|
|
121
|
+
this.restrictedDomains = merged.restrictedDomains || []
|
|
122
|
+
this.allowedDomains = merged.allowedDomains || []
|
|
123
|
+
this.loadBalancer = merged.loadBalancer
|
|
124
|
+
this.useHttp2 = merged.useHttp2
|
|
125
|
+
this.maxQueueSave = merged.maxQueueSave
|
|
126
|
+
this.maxTracksRestore = merged.maxTracksRestore
|
|
127
|
+
this.send = merged.send || this._createDefaultSend()
|
|
128
|
+
|
|
129
|
+
this._nodeStates = new Map()
|
|
130
|
+
this._failoverQueue = new Map()
|
|
131
|
+
this._lastFailoverAttempt = new Map()
|
|
132
|
+
this._brokenPlayers = new Map()
|
|
133
|
+
this._rebuildLocks = new Set()
|
|
134
|
+
this._leastUsedNodesCache = null
|
|
135
|
+
this._leastUsedNodesCacheTime = 0
|
|
136
|
+
this._nodeLoadCache = new Map()
|
|
137
|
+
this._eventHandlers = null
|
|
138
|
+
this._loading = false
|
|
139
|
+
|
|
140
|
+
if (this.autoResume) this._bindEventHandlers()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
_createDefaultSend() {
|
|
144
|
+
return (packet) => {
|
|
145
|
+
const guildId = packet?.d?.guild_id
|
|
146
|
+
if (!guildId) return
|
|
147
|
+
const guild =
|
|
148
|
+
this.client.guilds?.cache?.get?.(guildId) ||
|
|
149
|
+
this.client.cache?.guilds?.get?.(guildId)
|
|
150
|
+
if (!guild) return
|
|
151
|
+
const gateway = this.client.gateway
|
|
152
|
+
if (gateway?.send) gateway.send(gateway.calculateShardId(guildId), packet)
|
|
153
|
+
else if (guild.shard?.send) guild.shard.send(packet)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
_bindEventHandlers() {
|
|
158
|
+
this._eventHandlers = {
|
|
159
|
+
onNodeConnect: async (node) => {
|
|
160
|
+
this._invalidateCache()
|
|
161
|
+
await this._rebuildBrokenPlayers(node)
|
|
162
|
+
this._performCleanup()
|
|
163
|
+
},
|
|
164
|
+
onNodeDisconnect: (node) => {
|
|
165
|
+
this._invalidateCache()
|
|
166
|
+
queueMicrotask(() => {
|
|
167
|
+
this._storeBrokenPlayers(node)
|
|
168
|
+
this._performCleanup()
|
|
169
|
+
})
|
|
170
|
+
},
|
|
171
|
+
onNodeReady: (node, { resumed }) => {
|
|
172
|
+
if (!resumed) return
|
|
173
|
+
const batch = []
|
|
174
|
+
for (const player of this.players.values()) {
|
|
175
|
+
if (player.nodes === node && player.connection) batch.push(player)
|
|
176
|
+
}
|
|
177
|
+
if (batch.length)
|
|
178
|
+
queueMicrotask(() =>
|
|
179
|
+
batch.forEach((p) => {
|
|
180
|
+
p.connection.resendVoiceUpdate()
|
|
181
|
+
})
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
this.on(AqualinkEvents.NodeConnect, this._eventHandlers.onNodeConnect)
|
|
186
|
+
this.on(AqualinkEvents.NodeDisconnect, this._eventHandlers.onNodeDisconnect)
|
|
187
|
+
this.on(AqualinkEvents.NodeReady, this._eventHandlers.onNodeReady)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
destroy() {
|
|
191
|
+
if (this._eventHandlers) {
|
|
192
|
+
this.off(AqualinkEvents.NodeConnect, this._eventHandlers.onNodeConnect)
|
|
193
|
+
this.off(
|
|
194
|
+
AqualinkEvents.NodeDisconnect,
|
|
195
|
+
this._eventHandlers.onNodeDisconnect
|
|
196
|
+
)
|
|
197
|
+
this.off(AqualinkEvents.NodeReady, this._eventHandlers.onNodeReady)
|
|
198
|
+
this._eventHandlers = null
|
|
199
|
+
}
|
|
200
|
+
this.removeAllListeners()
|
|
201
|
+
|
|
202
|
+
for (const id of Array.from(this.nodeMap.keys())) this._destroyNode(id)
|
|
203
|
+
for (const player of Array.from(this.players.values()))
|
|
204
|
+
_functions.safeCall(() => player.destroy())
|
|
205
|
+
|
|
206
|
+
this.players.clear()
|
|
207
|
+
this._nodeStates.clear()
|
|
208
|
+
this._failoverQueue.clear()
|
|
209
|
+
this._lastFailoverAttempt.clear()
|
|
210
|
+
this._brokenPlayers.clear()
|
|
211
|
+
this._rebuildLocks.clear()
|
|
212
|
+
this._nodeLoadCache.clear()
|
|
213
|
+
this._invalidateCache()
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
get leastUsedNodes() {
|
|
217
|
+
const now = Date.now()
|
|
218
|
+
if (
|
|
219
|
+
this._leastUsedNodesCache &&
|
|
220
|
+
now - this._leastUsedNodesCacheTime < CACHE_VALID_TIME
|
|
221
|
+
) {
|
|
222
|
+
return this._leastUsedNodesCache
|
|
223
|
+
}
|
|
224
|
+
const connected = []
|
|
225
|
+
for (const n of this.nodeMap.values()) {
|
|
226
|
+
if (n.connected) connected.push(n)
|
|
227
|
+
}
|
|
228
|
+
let sorted
|
|
229
|
+
if (this.loadBalancer === 'leastRest') {
|
|
230
|
+
sorted = connected.sort(
|
|
231
|
+
(a, b) => (a.rest?.calls || 0) - (b.rest?.calls || 0)
|
|
232
|
+
)
|
|
233
|
+
} else if (this.loadBalancer === 'random') {
|
|
234
|
+
sorted = connected.sort(() => Math.random() - 0.5)
|
|
235
|
+
} else {
|
|
236
|
+
const withLoads = connected.map((n) => ({
|
|
237
|
+
node: n,
|
|
238
|
+
load: this._getNodeLoad(n)
|
|
239
|
+
}))
|
|
240
|
+
withLoads.sort((a, b) => a.load - b.load)
|
|
241
|
+
sorted = withLoads.map((x) => x.node)
|
|
242
|
+
}
|
|
243
|
+
this._leastUsedNodesCache = Object.freeze(sorted)
|
|
244
|
+
this._leastUsedNodesCacheTime = now
|
|
245
|
+
return this._leastUsedNodesCache
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
_invalidateCache() {
|
|
249
|
+
this._leastUsedNodesCache = null
|
|
250
|
+
this._leastUsedNodesCacheTime = 0
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
_getNodeLoad(node) {
|
|
254
|
+
const id = node.name || node.host
|
|
255
|
+
const now = Date.now()
|
|
256
|
+
const cached = this._nodeLoadCache.get(id)
|
|
257
|
+
if (cached && now - cached.time < 5000) {
|
|
258
|
+
this._nodeLoadCache.delete(id)
|
|
259
|
+
this._nodeLoadCache.set(id, cached)
|
|
260
|
+
return cached.load
|
|
261
|
+
}
|
|
262
|
+
const stats = node?.stats
|
|
263
|
+
if (!stats) return 0
|
|
264
|
+
const cores = Math.max(1, stats.cpu?.cores || 1)
|
|
265
|
+
const reservable = Math.max(1, stats.memory?.reservable || 1)
|
|
266
|
+
const load =
|
|
267
|
+
(stats.cpu ? stats.cpu.systemLoad / cores : 0) * 100 +
|
|
268
|
+
(stats.playingPlayers || 0) * 0.75 +
|
|
269
|
+
(stats.memory ? stats.memory.used / reservable : 0) * 40 +
|
|
270
|
+
(node.rest?.calls || 0) * 0.001
|
|
271
|
+
if (this._nodeLoadCache.size >= MAX_CACHE_SIZE) {
|
|
272
|
+
const iterator = this._nodeLoadCache.keys()
|
|
273
|
+
while (this._nodeLoadCache.size >= MAX_CACHE_SIZE) {
|
|
274
|
+
const oldest = iterator.next().value
|
|
275
|
+
if (!oldest) break
|
|
276
|
+
this._nodeLoadCache.delete(oldest)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
this._nodeLoadCache.set(id, { load, time: now })
|
|
280
|
+
return load
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async init(clientId) {
|
|
284
|
+
if (clientId) {
|
|
285
|
+
const newId = String(clientId)
|
|
286
|
+
if (this.clientId !== newId) {
|
|
287
|
+
this.clientId = newId
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (this.initiated) return this
|
|
292
|
+
if (!this.clientId) return this
|
|
293
|
+
await this._loadNodeSessions().catch(() => {})
|
|
294
|
+
const results = await Promise.allSettled(
|
|
295
|
+
this.nodes.map((n) =>
|
|
296
|
+
Promise.race([
|
|
297
|
+
this._createNode(n),
|
|
298
|
+
_functions.delay(NODE_TIMEOUT).then(() => {
|
|
299
|
+
throw new Error('Timeout')
|
|
300
|
+
})
|
|
301
|
+
])
|
|
302
|
+
)
|
|
303
|
+
)
|
|
304
|
+
if (!results.some((r) => r.status === 'fulfilled'))
|
|
305
|
+
throw new Error('No nodes connected')
|
|
306
|
+
if (this.plugins?.length) {
|
|
307
|
+
await Promise.allSettled(
|
|
308
|
+
this.plugins.map((p) => _functions.safeCall(() => p.load(this)))
|
|
309
|
+
)
|
|
310
|
+
}
|
|
311
|
+
this.initiated = true
|
|
312
|
+
return this
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async _createNode(options) {
|
|
316
|
+
const id = options.name || options.host
|
|
317
|
+
this._destroyNode(id)
|
|
318
|
+
const node = new Node(this, options, this.options)
|
|
319
|
+
node.players = new Set()
|
|
320
|
+
this.nodeMap.set(id, node)
|
|
321
|
+
this._nodeStates.set(id, { connected: false, failoverInProgress: false })
|
|
322
|
+
try {
|
|
323
|
+
await node.connect()
|
|
324
|
+
this._nodeStates.set(id, { connected: true, failoverInProgress: false })
|
|
325
|
+
this._invalidateCache()
|
|
326
|
+
this.emit(AqualinkEvents.NodeCreate, node)
|
|
327
|
+
return node
|
|
328
|
+
} catch (error) {
|
|
329
|
+
this._cleanupNode(id)
|
|
330
|
+
throw error
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
_destroyNode(id) {
|
|
335
|
+
const node = this.nodeMap.get(id)
|
|
336
|
+
if (!node) return
|
|
337
|
+
_functions.safeCall(() => node.destroy())
|
|
338
|
+
this._cleanupNode(id)
|
|
339
|
+
this.emit(AqualinkEvents.NodeDestroy, node)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
_cleanupNode(id) {
|
|
343
|
+
const node = this.nodeMap.get(id)
|
|
344
|
+
if (node) {
|
|
345
|
+
_functions.safeCall(() => node.removeAllListeners())
|
|
346
|
+
_functions.safeCall(() => node.players.clear())
|
|
347
|
+
this.nodeMap.delete(id)
|
|
348
|
+
}
|
|
349
|
+
this._nodeStates.delete(id)
|
|
350
|
+
this._failoverQueue.delete(id)
|
|
351
|
+
this._lastFailoverAttempt.delete(id)
|
|
352
|
+
this._nodeLoadCache.delete(id)
|
|
353
|
+
this._invalidateCache()
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
_storeBrokenPlayers(node) {
|
|
357
|
+
const id = node.name || node.host
|
|
358
|
+
const now = Date.now()
|
|
359
|
+
for (const player of this.players.values()) {
|
|
360
|
+
if (player.nodes !== node) continue
|
|
361
|
+
const state = this._capturePlayerState(player)
|
|
362
|
+
if (state) {
|
|
363
|
+
state.originalNodeId = id
|
|
364
|
+
state.brokenAt = now
|
|
365
|
+
this._brokenPlayers.set(String(player.guildId), state)
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async _rebuildBrokenPlayers(node) {
|
|
371
|
+
const id = node.name || node.host
|
|
372
|
+
const rebuilds = []
|
|
373
|
+
const now = Date.now()
|
|
374
|
+
for (const [guildId, state] of this._brokenPlayers) {
|
|
375
|
+
if (
|
|
376
|
+
state.originalNodeId === id &&
|
|
377
|
+
now - state.brokenAt < BROKEN_PLAYER_TTL
|
|
378
|
+
) {
|
|
379
|
+
rebuilds.push({ guildId, state })
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (!rebuilds.length) return
|
|
383
|
+
const successes = []
|
|
384
|
+
for (let i = 0; i < rebuilds.length; i += MAX_CONCURRENT_OPS) {
|
|
385
|
+
const batch = rebuilds.slice(i, i + MAX_CONCURRENT_OPS)
|
|
386
|
+
const results = await Promise.allSettled(
|
|
387
|
+
batch.map(({ guildId, state }) =>
|
|
388
|
+
this._rebuildPlayer(state, node).then(() => guildId)
|
|
389
|
+
)
|
|
390
|
+
)
|
|
391
|
+
for (const r of results) {
|
|
392
|
+
if (r.status === 'fulfilled') successes.push(r.value)
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
for (const guildId of successes) this._brokenPlayers.delete(guildId)
|
|
396
|
+
if (successes.length)
|
|
397
|
+
this.emit(AqualinkEvents.PlayersRebuilt, node, successes.length)
|
|
398
|
+
this._performCleanup()
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async _rebuildPlayer(state, targetNode) {
|
|
402
|
+
const {
|
|
403
|
+
guildId,
|
|
404
|
+
textChannel,
|
|
405
|
+
voiceChannel,
|
|
406
|
+
current,
|
|
407
|
+
volume = 65,
|
|
408
|
+
deaf = true
|
|
409
|
+
} = state
|
|
410
|
+
const lockKey = `rebuild_${guildId}`
|
|
411
|
+
if (this._rebuildLocks.has(lockKey)) return
|
|
412
|
+
this._rebuildLocks.add(lockKey)
|
|
413
|
+
try {
|
|
414
|
+
if (this.players.has(guildId)) {
|
|
415
|
+
await this.destroyPlayer(guildId)
|
|
416
|
+
await _functions.delay(RECONNECT_DELAY)
|
|
417
|
+
}
|
|
418
|
+
const player = this.createPlayer(targetNode, {
|
|
419
|
+
guildId,
|
|
420
|
+
textChannel,
|
|
421
|
+
voiceChannel,
|
|
422
|
+
defaultVolume: volume,
|
|
423
|
+
deaf
|
|
424
|
+
})
|
|
425
|
+
if (current && player?.queue?.add) {
|
|
426
|
+
player.queue.add(current)
|
|
427
|
+
await player.play()
|
|
428
|
+
if (state.position > 0) {
|
|
429
|
+
const seekOnce = (p) => {
|
|
430
|
+
if (p.guildId === guildId) {
|
|
431
|
+
this.off(AqualinkEvents.TrackStart, seekOnce)
|
|
432
|
+
_functions.unrefTimeout(() => player.seek?.(state.position), 50)
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
this.once(AqualinkEvents.TrackStart, seekOnce)
|
|
436
|
+
player.once('destroy', () =>
|
|
437
|
+
this.off(AqualinkEvents.TrackStart, seekOnce)
|
|
438
|
+
)
|
|
439
|
+
}
|
|
440
|
+
if (state.paused) player.pause(true)
|
|
441
|
+
}
|
|
442
|
+
return player
|
|
443
|
+
} finally {
|
|
444
|
+
this._rebuildLocks.delete(lockKey)
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
async handleNodeFailover(failedNode) {
|
|
449
|
+
if (!this.failoverOptions.enabled) return
|
|
450
|
+
const id = failedNode.name || failedNode.host
|
|
451
|
+
const now = Date.now()
|
|
452
|
+
const state = this._nodeStates.get(id)
|
|
453
|
+
if (state?.failoverInProgress) return
|
|
454
|
+
const lastAttempt = this._lastFailoverAttempt.get(id)
|
|
455
|
+
if (lastAttempt && now - lastAttempt < this.failoverOptions.cooldownTime)
|
|
456
|
+
return
|
|
457
|
+
const attempts = this._failoverQueue.get(id) || 0
|
|
458
|
+
if (attempts >= this.failoverOptions.maxFailoverAttempts) return
|
|
459
|
+
|
|
460
|
+
this._nodeStates.set(id, { connected: false, failoverInProgress: true })
|
|
461
|
+
this._lastFailoverAttempt.set(id, now)
|
|
462
|
+
this._failoverQueue.set(id, attempts + 1)
|
|
463
|
+
|
|
464
|
+
try {
|
|
465
|
+
this.emit(AqualinkEvents.NodeFailover, failedNode)
|
|
466
|
+
const players = Array.from(failedNode.players || [])
|
|
467
|
+
if (!players.length) return
|
|
468
|
+
const available = []
|
|
469
|
+
for (const n of this.nodeMap.values()) {
|
|
470
|
+
if (n !== failedNode && n.connected) available.push(n)
|
|
471
|
+
}
|
|
472
|
+
if (!available.length) throw new Error('No failover nodes')
|
|
473
|
+
const results = await this._migratePlayersOptimized(players, available)
|
|
474
|
+
const successful = results.filter((r) => r.success).length
|
|
475
|
+
if (successful) {
|
|
476
|
+
this.emit(
|
|
477
|
+
AqualinkEvents.NodeFailoverComplete,
|
|
478
|
+
failedNode,
|
|
479
|
+
successful,
|
|
480
|
+
results.length - successful
|
|
481
|
+
)
|
|
482
|
+
this._performCleanup()
|
|
483
|
+
}
|
|
484
|
+
} catch (error) {
|
|
485
|
+
this.emit(AqualinkEvents.Error, null, error)
|
|
486
|
+
} finally {
|
|
487
|
+
this._nodeStates.set(id, { connected: false, failoverInProgress: false })
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
async _migratePlayersOptimized(players, nodes) {
|
|
492
|
+
const loads = new Map()
|
|
493
|
+
const counts = new Map()
|
|
494
|
+
for (const n of nodes) {
|
|
495
|
+
loads.set(n, this._getNodeLoad(n))
|
|
496
|
+
counts.set(n, 0)
|
|
497
|
+
}
|
|
498
|
+
const pickNode = () => {
|
|
499
|
+
let best = nodes[0],
|
|
500
|
+
bestScore = loads.get(best) + counts.get(best)
|
|
501
|
+
for (let i = 1; i < nodes.length; i++) {
|
|
502
|
+
const score = loads.get(nodes[i]) + counts.get(nodes[i])
|
|
503
|
+
if (score < bestScore) {
|
|
504
|
+
best = nodes[i]
|
|
505
|
+
bestScore = score
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
counts.set(best, counts.get(best) + 1)
|
|
509
|
+
return best
|
|
510
|
+
}
|
|
511
|
+
const results = []
|
|
512
|
+
for (let i = 0; i < players.length; i += MAX_CONCURRENT_OPS) {
|
|
513
|
+
const batch = players.slice(i, i + MAX_CONCURRENT_OPS)
|
|
514
|
+
const batchResults = await Promise.allSettled(
|
|
515
|
+
batch.map((p) => this._migratePlayer(p, pickNode))
|
|
516
|
+
)
|
|
517
|
+
for (const r of batchResults)
|
|
518
|
+
results.push({ success: r.status === 'fulfilled', error: r.reason })
|
|
519
|
+
}
|
|
520
|
+
return results
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
async _migratePlayer(player, pickNode) {
|
|
524
|
+
const state = this._capturePlayerState(player)
|
|
525
|
+
if (!state) throw new Error('Failed to capture state')
|
|
526
|
+
const { maxRetries, retryDelay } = this.failoverOptions
|
|
527
|
+
for (let retry = 0; retry < maxRetries; retry++) {
|
|
528
|
+
try {
|
|
529
|
+
const targetNode = pickNode()
|
|
530
|
+
const newPlayer = this._createPlayerOnNode(targetNode, state)
|
|
531
|
+
await this._restorePlayerState(newPlayer, state)
|
|
532
|
+
this.emit(AqualinkEvents.PlayerMigrated, player, newPlayer, targetNode)
|
|
533
|
+
return newPlayer
|
|
534
|
+
} catch (error) {
|
|
535
|
+
if (retry === maxRetries - 1) throw error
|
|
536
|
+
await _functions.delay(retryDelay * 1.5 ** retry)
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
_capturePlayerState(player) {
|
|
542
|
+
if (!player) return null
|
|
543
|
+
let position = player.position || 0
|
|
544
|
+
if (player.playing && !player.paused && player.timestamp) {
|
|
545
|
+
const elapsed = Date.now() - player.timestamp
|
|
546
|
+
position = Math.min(
|
|
547
|
+
position + elapsed,
|
|
548
|
+
player.current?.info?.length || position + elapsed
|
|
549
|
+
)
|
|
550
|
+
}
|
|
551
|
+
return {
|
|
552
|
+
guildId: player.guildId,
|
|
553
|
+
textChannel: player.textChannel,
|
|
554
|
+
voiceChannel: player.voiceChannel,
|
|
555
|
+
volume: player.volume ?? 100,
|
|
556
|
+
paused: !!player.paused,
|
|
557
|
+
position,
|
|
558
|
+
current: player.current || null,
|
|
559
|
+
queue: player.queue?.toArray?.() || EMPTY_ARRAY,
|
|
560
|
+
loop: player.loop,
|
|
561
|
+
shuffle: player.shuffle,
|
|
562
|
+
deaf: player.deaf ?? false,
|
|
563
|
+
connected: !!player.connected
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
_createPlayerOnNode(targetNode, state) {
|
|
568
|
+
return this.createPlayer(targetNode, {
|
|
569
|
+
guildId: state.guildId,
|
|
570
|
+
textChannel: state.textChannel,
|
|
571
|
+
voiceChannel: state.voiceChannel,
|
|
572
|
+
defaultVolume: state.volume || 100,
|
|
573
|
+
deaf: state.deaf || false
|
|
574
|
+
})
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
async _restorePlayerState(newPlayer, state) {
|
|
578
|
+
const ops = []
|
|
579
|
+
if (typeof state.volume === 'number') {
|
|
580
|
+
if (typeof newPlayer.setVolume === 'function')
|
|
581
|
+
ops.push(newPlayer.setVolume(state.volume))
|
|
582
|
+
else newPlayer.volume = state.volume
|
|
583
|
+
}
|
|
584
|
+
if (state.queue?.length && newPlayer.queue?.add)
|
|
585
|
+
newPlayer.queue.add(...state.queue)
|
|
586
|
+
if (state.current && this.failoverOptions.preservePosition) {
|
|
587
|
+
newPlayer.queue?.add?.(state.current, { toFront: true })
|
|
588
|
+
if (this.failoverOptions.resumePlayback) {
|
|
589
|
+
ops.push(newPlayer.play())
|
|
590
|
+
if (state.position > 0) {
|
|
591
|
+
const guildId = newPlayer.guildId
|
|
592
|
+
const seekOnce = (p) => {
|
|
593
|
+
if (p.guildId === guildId) {
|
|
594
|
+
this.off(AqualinkEvents.TrackStart, seekOnce)
|
|
595
|
+
_functions.unrefTimeout(
|
|
596
|
+
() => newPlayer.seek?.(state.position),
|
|
597
|
+
50
|
|
598
|
+
)
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
this.once(AqualinkEvents.TrackStart, seekOnce)
|
|
602
|
+
newPlayer.once('destroy', () =>
|
|
603
|
+
this.off(AqualinkEvents.TrackStart, seekOnce)
|
|
604
|
+
)
|
|
605
|
+
}
|
|
606
|
+
if (state.paused) ops.push(newPlayer.pause(true))
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
newPlayer.loop = state.loop
|
|
610
|
+
newPlayer.shuffle = state.shuffle
|
|
611
|
+
await Promise.allSettled(ops)
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
updateVoiceState({ d, t }) {
|
|
615
|
+
if (
|
|
616
|
+
!d?.guild_id ||
|
|
617
|
+
(t !== 'VOICE_STATE_UPDATE' && t !== 'VOICE_SERVER_UPDATE')
|
|
618
|
+
)
|
|
619
|
+
return
|
|
620
|
+
const player = this.players.get(String(d.guild_id))
|
|
621
|
+
if (!player) return
|
|
622
|
+
|
|
623
|
+
d.txId = player.txId
|
|
624
|
+
if (t === 'VOICE_STATE_UPDATE') {
|
|
625
|
+
if (d.user_id !== this.clientId) return
|
|
626
|
+
if (player.connection) {
|
|
627
|
+
if (!d.channel_id && player.connection.voiceChannel) {
|
|
628
|
+
player.connection.setStateUpdate(d)
|
|
629
|
+
} else {
|
|
630
|
+
player.connection.sessionId = d.session_id
|
|
631
|
+
player.connection.setStateUpdate(d)
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
} else {
|
|
635
|
+
player.connection?.setServerUpdate(d)
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
fetchRegion(region) {
|
|
640
|
+
if (!region) return this.leastUsedNodes
|
|
641
|
+
const lower = region.toLowerCase()
|
|
642
|
+
const filtered = []
|
|
643
|
+
for (const n of this.nodeMap.values()) {
|
|
644
|
+
if (n.connected && n.regions?.includes(lower)) filtered.push(n)
|
|
645
|
+
}
|
|
646
|
+
return Object.freeze(
|
|
647
|
+
filtered.sort((a, b) => this._getNodeLoad(a) - this._getNodeLoad(b))
|
|
648
|
+
)
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
createConnection(options) {
|
|
652
|
+
if (!this.initiated) throw new Error('Aqua not initialized')
|
|
653
|
+
const existing = this.players.get(String(options.guildId))
|
|
654
|
+
if (existing && !existing.destroyed) {
|
|
655
|
+
if (
|
|
656
|
+
options.voiceChannel &&
|
|
657
|
+
existing.voiceChannel !== options.voiceChannel
|
|
658
|
+
) {
|
|
659
|
+
_functions.safeCall(() => existing.connect(options))
|
|
660
|
+
}
|
|
661
|
+
return existing
|
|
662
|
+
}
|
|
663
|
+
const candidates = options.region
|
|
664
|
+
? this.fetchRegion(options.region)
|
|
665
|
+
: this.leastUsedNodes
|
|
666
|
+
if (!candidates.length) throw new Error('No nodes available')
|
|
667
|
+
return this.createPlayer(this._chooseLeastBusyNode(candidates), options)
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
createPlayer(node, options) {
|
|
671
|
+
const existing = this.players.get(options.guildId)
|
|
672
|
+
if (existing) {
|
|
673
|
+
_functions.safeCall(() =>
|
|
674
|
+
existing.destroy({
|
|
675
|
+
preserveMessage:
|
|
676
|
+
options.preserveMessage || !!options.resuming || false,
|
|
677
|
+
preserveTracks: !!options.resuming || false
|
|
678
|
+
})
|
|
679
|
+
)
|
|
680
|
+
}
|
|
681
|
+
const player = new Player(this, node, options)
|
|
682
|
+
const guildId = String(options.guildId)
|
|
683
|
+
this.players.set(guildId, player)
|
|
684
|
+
node?.players?.add?.(player)
|
|
685
|
+
player.once('destroy', () => this._handlePlayerDestroy(player))
|
|
686
|
+
player.connect(options)
|
|
687
|
+
this.emit(AqualinkEvents.PlayerCreate, player)
|
|
688
|
+
return player
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
_handlePlayerDestroy(player) {
|
|
692
|
+
player.nodes?.players?.delete?.(player)
|
|
693
|
+
const guildId = String(player.guildId)
|
|
694
|
+
if (this.players.get(guildId) === player) this.players.delete(guildId)
|
|
695
|
+
this.emit(AqualinkEvents.PlayerDestroyed, player)
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
async destroyPlayer(guildId) {
|
|
699
|
+
const id = String(guildId)
|
|
700
|
+
const player = this.players.get(id)
|
|
701
|
+
if (!player) return
|
|
702
|
+
this.players.delete(id)
|
|
703
|
+
_functions.safeCall(() => player.removeAllListeners())
|
|
704
|
+
await _functions.safeCall(() => player.destroy())
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
async resolve({ query, source, requester, nodes }) {
|
|
708
|
+
if (!this.initiated) throw new Error('Aqua not initialized')
|
|
709
|
+
const node = this._getRequestNode(nodes)
|
|
710
|
+
if (!node) throw new Error('No nodes available')
|
|
711
|
+
const formatted = _functions.formatQuery(
|
|
712
|
+
query,
|
|
713
|
+
source || this.defaultSearchPlatform
|
|
714
|
+
)
|
|
715
|
+
const endpoint = `/${this.restVersion}/loadtracks?identifier=${encodeURIComponent(formatted)}`
|
|
716
|
+
try {
|
|
717
|
+
const response = await node.rest.makeRequest('GET', endpoint)
|
|
718
|
+
if (
|
|
719
|
+
!response ||
|
|
720
|
+
response.loadType === 'empty' ||
|
|
721
|
+
response.loadType === 'NO_MATCHES'
|
|
722
|
+
)
|
|
723
|
+
return EMPTY_TRACKS_RESPONSE
|
|
724
|
+
return this._constructResponse(response, requester, node)
|
|
725
|
+
} catch (error) {
|
|
726
|
+
throw new Error(
|
|
727
|
+
error?.name === 'AbortError'
|
|
728
|
+
? 'Request timeout'
|
|
729
|
+
: `Resolve failed: ${error?.message || error}`
|
|
730
|
+
)
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
_getRequestNode(nodes) {
|
|
735
|
+
if (!nodes) return this._chooseLeastBusyNode(this.leastUsedNodes)
|
|
736
|
+
if (nodes instanceof Node) return nodes
|
|
737
|
+
if (Array.isArray(nodes)) {
|
|
738
|
+
const candidates = nodes.filter((n) => n?.connected)
|
|
739
|
+
return this._chooseLeastBusyNode(
|
|
740
|
+
candidates.length ? candidates : this.leastUsedNodes
|
|
741
|
+
)
|
|
742
|
+
}
|
|
743
|
+
if (typeof nodes === 'string') {
|
|
744
|
+
const node = this.nodeMap.get(nodes)
|
|
745
|
+
return node?.connected
|
|
746
|
+
? node
|
|
747
|
+
: this._chooseLeastBusyNode(this.leastUsedNodes)
|
|
748
|
+
}
|
|
749
|
+
throw new TypeError(`Invalid nodes: ${typeof nodes}`)
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
_chooseLeastBusyNode(nodes) {
|
|
753
|
+
if (!nodes?.length) return null
|
|
754
|
+
if (nodes.length === 1) return nodes[0]
|
|
755
|
+
let best = nodes[0],
|
|
756
|
+
bestScore = this._getNodeLoad(best)
|
|
757
|
+
for (let i = 1; i < nodes.length; i++) {
|
|
758
|
+
const score = this._getNodeLoad(nodes[i])
|
|
759
|
+
if (score < bestScore) {
|
|
760
|
+
best = nodes[i]
|
|
761
|
+
bestScore = score
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
return best
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
_constructResponse(response, requester, node) {
|
|
768
|
+
const { loadType, data, pluginInfo: rootPlugin } = response || {}
|
|
769
|
+
const base = {
|
|
770
|
+
loadType,
|
|
771
|
+
exception: null,
|
|
772
|
+
playlistInfo: null,
|
|
773
|
+
pluginInfo: rootPlugin || {},
|
|
774
|
+
tracks: []
|
|
775
|
+
}
|
|
776
|
+
if (loadType === 'error' || loadType === 'LOAD_FAILED') {
|
|
777
|
+
base.exception = data || response.exception || null
|
|
778
|
+
return base
|
|
779
|
+
}
|
|
780
|
+
if (loadType === 'track' && data) {
|
|
781
|
+
base.pluginInfo =
|
|
782
|
+
data.info?.pluginInfo || data.pluginInfo || base.pluginInfo
|
|
783
|
+
base.tracks.push(_functions.makeTrack(data, requester, node))
|
|
784
|
+
} else if (loadType === 'playlist' && data) {
|
|
785
|
+
const info = data.info
|
|
786
|
+
if (info) {
|
|
787
|
+
base.playlistInfo = {
|
|
788
|
+
name: info.name || info.title,
|
|
789
|
+
thumbnail:
|
|
790
|
+
data.pluginInfo?.artworkUrl ||
|
|
791
|
+
data.tracks?.[0]?.info?.artworkUrl ||
|
|
792
|
+
null,
|
|
793
|
+
...info
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
base.pluginInfo = data.pluginInfo || rootPlugin || base.pluginInfo
|
|
797
|
+
base.tracks = Array.isArray(data.tracks)
|
|
798
|
+
? data.tracks.map((t) => _functions.makeTrack(t, requester, node))
|
|
799
|
+
: []
|
|
800
|
+
} else if (loadType === 'search') {
|
|
801
|
+
base.tracks = Array.isArray(data)
|
|
802
|
+
? data.map((t) => _functions.makeTrack(t, requester, node))
|
|
803
|
+
: []
|
|
804
|
+
}
|
|
805
|
+
return base
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
get(guildId) {
|
|
809
|
+
const player = this.players.get(String(guildId))
|
|
810
|
+
if (!player) throw new Error(`Player not found: ${guildId}`)
|
|
811
|
+
return player
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
async search(query, requester, source) {
|
|
815
|
+
if (!query || !requester) return null
|
|
816
|
+
try {
|
|
817
|
+
const { tracks } = await this.resolve({
|
|
818
|
+
query,
|
|
819
|
+
source: source || this.defaultSearchPlatform,
|
|
820
|
+
requester
|
|
821
|
+
})
|
|
822
|
+
return tracks || null
|
|
823
|
+
} catch {
|
|
824
|
+
return null
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
async savePlayer(filePath = './AquaPlayers.jsonl') {
|
|
829
|
+
const lockFile = `${filePath}.lock`
|
|
830
|
+
const tempFile = `${filePath}.tmp`
|
|
831
|
+
let ws = null
|
|
832
|
+
try {
|
|
833
|
+
await fs.promises.writeFile(lockFile, String(process.pid), { flag: 'wx' })
|
|
834
|
+
ws = fs.createWriteStream(tempFile, { encoding: 'utf8', flags: 'w' })
|
|
835
|
+
const buffer = []
|
|
836
|
+
let drainPromise = Promise.resolve()
|
|
837
|
+
|
|
838
|
+
const nodeSessions = {}
|
|
839
|
+
for (const node of this.nodeMap.values()) {
|
|
840
|
+
if (node.sessionId) nodeSessions[node.name] = node.sessionId
|
|
841
|
+
}
|
|
842
|
+
buffer.push(JSON.stringify({ type: 'node_sessions', data: nodeSessions }))
|
|
843
|
+
for (const player of this.players.values()) {
|
|
844
|
+
const requester = player.requester || player.current?.requester
|
|
845
|
+
const data = {
|
|
846
|
+
g: player.guildId,
|
|
847
|
+
t: player.textChannel,
|
|
848
|
+
v: player.voiceChannel,
|
|
849
|
+
u: player.current?.uri || null,
|
|
850
|
+
p: player.position || 0,
|
|
851
|
+
ts: player.timestamp || 0,
|
|
852
|
+
q: player.queue
|
|
853
|
+
.toArray()
|
|
854
|
+
.slice(0, this.maxQueueSave)
|
|
855
|
+
.map((tr) => tr.uri),
|
|
856
|
+
r: requester ? `${requester.id}:${requester.username}` : null,
|
|
857
|
+
vol: player.volume,
|
|
858
|
+
pa: player.paused,
|
|
859
|
+
pl: player.playing,
|
|
860
|
+
nw: player.nowPlayingMessage?.id || null,
|
|
861
|
+
resuming: true
|
|
862
|
+
}
|
|
863
|
+
buffer.push(JSON.stringify(data))
|
|
864
|
+
|
|
865
|
+
if (buffer.length >= WRITE_BUFFER_SIZE) {
|
|
866
|
+
const chunk = `${buffer.join('\n')}\n`
|
|
867
|
+
buffer.length = 0
|
|
868
|
+
if (!ws.write(chunk)) {
|
|
869
|
+
drainPromise = drainPromise.then(
|
|
870
|
+
() => new Promise((r) => ws.once('drain', r))
|
|
871
|
+
)
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
if (buffer.length) ws.write(`${buffer.join('\n')}\n`)
|
|
877
|
+
await drainPromise
|
|
878
|
+
await new Promise((resolve, reject) =>
|
|
879
|
+
ws.end((err) => (err ? reject(err) : resolve()))
|
|
880
|
+
)
|
|
881
|
+
ws = null
|
|
882
|
+
await fs.promises.rename(tempFile, filePath)
|
|
883
|
+
} catch (error) {
|
|
884
|
+
console.error(`[Aqua/Autoresume]Error saving players:`, error)
|
|
885
|
+
this.emit(AqualinkEvents.Error, null, error)
|
|
886
|
+
if (ws) _functions.safeCall(() => ws.destroy())
|
|
887
|
+
await fs.promises.unlink(tempFile).catch(_functions.noop)
|
|
888
|
+
} finally {
|
|
889
|
+
if (ws) _functions.safeCall(() => ws.destroy())
|
|
890
|
+
await fs.promises.unlink(lockFile).catch(_functions.noop)
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
async loadPlayers(filePath = './AquaPlayers.jsonl') {
|
|
895
|
+
if (this._loading) return
|
|
896
|
+
this._loading = true
|
|
897
|
+
const lockFile = `${filePath}.lock`
|
|
898
|
+
let stream = null,
|
|
899
|
+
rl = null
|
|
900
|
+
try {
|
|
901
|
+
await fs.promises.access(filePath)
|
|
902
|
+
await fs.promises.writeFile(lockFile, String(process.pid), { flag: 'wx' })
|
|
903
|
+
await this._waitForFirstNode()
|
|
904
|
+
|
|
905
|
+
stream = fs.createReadStream(filePath, { encoding: 'utf8' })
|
|
906
|
+
rl = readline.createInterface({ input: stream, crlfDelay: Infinity })
|
|
907
|
+
|
|
908
|
+
const batch = []
|
|
909
|
+
for await (const line of rl) {
|
|
910
|
+
if (!line.trim()) continue
|
|
911
|
+
try {
|
|
912
|
+
const parsed = JSON.parse(line)
|
|
913
|
+
if (parsed.type === 'node_sessions') continue
|
|
914
|
+
batch.push(parsed)
|
|
915
|
+
} catch {
|
|
916
|
+
continue
|
|
917
|
+
}
|
|
918
|
+
if (batch.length >= PLAYER_BATCH_SIZE) {
|
|
919
|
+
await Promise.allSettled(batch.map((p) => this._restorePlayer(p)))
|
|
920
|
+
batch.length = 0
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
if (batch.length)
|
|
924
|
+
await Promise.allSettled(batch.map((p) => this._restorePlayer(p)))
|
|
925
|
+
await fs.promises.writeFile(filePath, '')
|
|
926
|
+
} catch (err) {
|
|
927
|
+
if (err.code !== 'ENOENT') {
|
|
928
|
+
console.error(`[Aqua/Autoresume]Error loading players:`, err)
|
|
929
|
+
this.emit(AqualinkEvents.Error, null, err)
|
|
930
|
+
}
|
|
931
|
+
} finally {
|
|
932
|
+
this._loading = false
|
|
933
|
+
if (rl) _functions.safeCall(() => rl.close())
|
|
934
|
+
if (stream) _functions.safeCall(() => stream.destroy())
|
|
935
|
+
await fs.promises.unlink(lockFile).catch(_functions.noop)
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
async _restorePlayer(p) {
|
|
940
|
+
try {
|
|
941
|
+
const gId = String(p.g)
|
|
942
|
+
const existing = this.players.get(gId)
|
|
943
|
+
if (existing?.playing) return
|
|
944
|
+
|
|
945
|
+
const player =
|
|
946
|
+
existing ||
|
|
947
|
+
this.createPlayer(this._chooseLeastBusyNode(this.leastUsedNodes), {
|
|
948
|
+
guildId: gId,
|
|
949
|
+
textChannel: p.t,
|
|
950
|
+
voiceChannel: p.v,
|
|
951
|
+
defaultVolume: p.vol || 65,
|
|
952
|
+
deaf: true,
|
|
953
|
+
resuming: !!p.resuming
|
|
954
|
+
})
|
|
955
|
+
player._resuming = !!p.resuming
|
|
956
|
+
const requester = _functions.parseRequester(p.r)
|
|
957
|
+
const tracksToResolve = [p.u, ...(p.q || [])]
|
|
958
|
+
.filter(Boolean)
|
|
959
|
+
.slice(0, this.maxTracksRestore)
|
|
960
|
+
const resolved = await Promise.all(
|
|
961
|
+
tracksToResolve.map((uri) =>
|
|
962
|
+
this.resolve({ query: uri, requester }).catch(() => null)
|
|
963
|
+
)
|
|
964
|
+
)
|
|
965
|
+
const validTracks = resolved.flatMap((r) => r?.tracks || [])
|
|
966
|
+
if (validTracks.length && player.queue?.add) {
|
|
967
|
+
player.queue.add(...validTracks)
|
|
968
|
+
}
|
|
969
|
+
if (p.u && validTracks[0]) {
|
|
970
|
+
if (p.vol != null) {
|
|
971
|
+
if (typeof player.setVolume === 'function')
|
|
972
|
+
await player.setVolume(p.vol)
|
|
973
|
+
else player.volume = p.vol
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
if (p.p > 0) {
|
|
977
|
+
const seekOnce = (pl) => {
|
|
978
|
+
if (pl.guildId === gId) {
|
|
979
|
+
this.off(AqualinkEvents.TrackStart, seekOnce)
|
|
980
|
+
_functions.unrefTimeout(() => player.seek?.(p.p), 100)
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
this.on(AqualinkEvents.TrackStart, seekOnce)
|
|
984
|
+
player.once('destroy', () => this.off(AqualinkEvents.TrackStart, seekOnce))
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
await player.play(undefined, { startTime: p.p, paused: p.pa })
|
|
988
|
+
}
|
|
989
|
+
if (p.nw && p.t) {
|
|
990
|
+
const channel = this.client.channels?.cache?.get(p.t)
|
|
991
|
+
if (channel?.messages)
|
|
992
|
+
player.nowPlayingMessage = await channel.messages
|
|
993
|
+
.fetch(p.nw)
|
|
994
|
+
.catch(() => null)
|
|
995
|
+
}
|
|
996
|
+
} catch (e) {
|
|
997
|
+
console.error(
|
|
998
|
+
`[Aqua/Autoresume]Failed to restore player for guild: ${p.g}`,
|
|
999
|
+
e
|
|
1000
|
+
)
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
async _waitForFirstNode(timeout = NODE_TIMEOUT) {
|
|
1005
|
+
if (this.leastUsedNodes.length) return
|
|
1006
|
+
return new Promise((resolve, reject) => {
|
|
1007
|
+
let resolved = false
|
|
1008
|
+
const cleanup = () => {
|
|
1009
|
+
if (resolved) return
|
|
1010
|
+
resolved = true
|
|
1011
|
+
clearTimeout(timer)
|
|
1012
|
+
this.off(AqualinkEvents.NodeConnect, onReady)
|
|
1013
|
+
this.off(AqualinkEvents.NodeCreate, onReady)
|
|
1014
|
+
}
|
|
1015
|
+
const onReady = () => {
|
|
1016
|
+
if (this.leastUsedNodes.length) {
|
|
1017
|
+
cleanup()
|
|
1018
|
+
resolve()
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
const timer = setTimeout(() => {
|
|
1022
|
+
cleanup()
|
|
1023
|
+
reject(new Error('Timeout waiting for first node'))
|
|
1024
|
+
}, timeout)
|
|
1025
|
+
timer.unref?.()
|
|
1026
|
+
this.on(AqualinkEvents.NodeConnect, onReady)
|
|
1027
|
+
this.on(AqualinkEvents.NodeCreate, onReady)
|
|
1028
|
+
onReady()
|
|
1029
|
+
})
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
_performCleanup() {
|
|
1033
|
+
const now = Date.now()
|
|
1034
|
+
for (const [guildId, state] of this._brokenPlayers) {
|
|
1035
|
+
if (now - state.brokenAt > BROKEN_PLAYER_TTL)
|
|
1036
|
+
this._brokenPlayers.delete(guildId)
|
|
1037
|
+
}
|
|
1038
|
+
for (const [id, ts] of this._lastFailoverAttempt) {
|
|
1039
|
+
if (now - ts > FAILOVER_CLEANUP_TTL) {
|
|
1040
|
+
this._lastFailoverAttempt.delete(id)
|
|
1041
|
+
this._failoverQueue.delete(id)
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
if (this._failoverQueue.size > MAX_FAILOVER_QUEUE)
|
|
1045
|
+
this._failoverQueue.clear()
|
|
1046
|
+
if (this._rebuildLocks.size > MAX_REBUILD_LOCKS) this._rebuildLocks.clear()
|
|
1047
|
+
for (const id of this._nodeStates.keys()) {
|
|
1048
|
+
if (!this.nodeMap.has(id)) this._nodeStates.delete(id)
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
async _loadNodeSessions(filePath = './AquaPlayers.jsonl') {
|
|
1053
|
+
let stream = null,
|
|
1054
|
+
rl = null
|
|
1055
|
+
try {
|
|
1056
|
+
await fs.promises.access(filePath)
|
|
1057
|
+
stream = fs.createReadStream(filePath, { encoding: 'utf8' })
|
|
1058
|
+
rl = readline.createInterface({ input: stream, crlfDelay: Infinity })
|
|
1059
|
+
|
|
1060
|
+
for await (const line of rl) {
|
|
1061
|
+
if (!line.trim()) continue
|
|
1062
|
+
try {
|
|
1063
|
+
const parsed = JSON.parse(line)
|
|
1064
|
+
if (parsed.type === 'node_sessions') {
|
|
1065
|
+
for (const [name, sessionId] of Object.entries(parsed.data)) {
|
|
1066
|
+
const nodeOptions = this.nodes.find(
|
|
1067
|
+
(n) => (n.name || n.host) === name
|
|
1068
|
+
)
|
|
1069
|
+
if (nodeOptions) {
|
|
1070
|
+
nodeOptions.sessionId = sessionId
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
break
|
|
1074
|
+
}
|
|
1075
|
+
} catch {
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
} catch {
|
|
1079
|
+
} finally {
|
|
1080
|
+
if (rl) _functions.safeCall(() => rl.close())
|
|
1081
|
+
if (stream) _functions.safeCall(() => stream.destroy())
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
897
1086
|
module.exports = Aqua
|