-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathPlayer.js
1001 lines (1001 loc) · 38 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
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
446
447
448
449
450
451
452
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
641
642
643
644
645
646
647
648
649
650
651
652
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
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
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Player = exports.validAudioOutputs = void 0;
const Utils_1 = require("./Utils");
exports.validAudioOutputs = {
mono: {
leftToLeft: 0.5,
leftToRight: 0.5,
rightToLeft: 0.5,
rightToRight: 0.5,
},
stereo: {
leftToLeft: 1,
leftToRight: 0,
rightToLeft: 0,
rightToRight: 1,
},
left: {
leftToLeft: 0.5,
leftToRight: 0,
rightToLeft: 0.5,
rightToRight: 0,
},
right: {
leftToLeft: 0,
leftToRight: 0.5,
rightToLeft: 0,
rightToRight: 0.5,
},
};
function check(options) {
if (!options)
throw new TypeError("PlayerOptions must not be empty.");
if (!/^\d+$/.test(options.guild))
throw new TypeError('Player option "guild" must be present and be a non-empty string.');
if (options.textChannel && !/^\d+$/.test(options.textChannel))
throw new TypeError('Player option "textChannel" must be a non-empty string.');
if (options.voiceChannel && !/^\d+$/.test(options.voiceChannel))
throw new TypeError('Player option "voiceChannel" must be a non-empty string.');
if (options.node && typeof options.node !== "string")
throw new TypeError('Player option "node" must be a non-empty string.');
if (typeof options.volume !== "undefined" &&
typeof options.volume !== "number")
throw new TypeError('Player option "volume" must be a number.');
if (typeof options.selfMute !== "undefined" &&
typeof options.selfMute !== "boolean")
throw new TypeError('Player option "selfMute" must be a boolean.');
if (typeof options.selfDeafen !== "undefined" &&
typeof options.selfDeafen !== "boolean")
throw new TypeError('Player option "selfDeafen" must be a boolean.');
}
class Player {
options;
/** The Queue for the Player. */
queue = new (Utils_1.Structure.get("Queue"))();
/** Whether the queue repeats the track. */
trackRepeat = false;
/** Whether the queue repeats the queue. */
queueRepeat = false;
/** The time the player is in the track. */
position = 0;
/** Whether the player is playing. */
playing = false;
/** Whether the player is paused. */
paused = false;
/** The volume for the player */
volume;
/** The real volume for the player (if volumedecrementer is used this will be diffrent to player.volume) */
lavalinkVolume;
/** The Node for the Player. */
node;
/** The guild for the player. */
guild;
/** The voice channel for the player. */
voiceChannel = null;
/** The text channel for the player. */
textChannel = null;
/** The current state of the player. */
state = "DISCONNECTED";
/** The equalizer bands array. */
bands = new Array(15).fill(0.0);
/** @deprecated The voice state object from Discord. */
voiceState;
/** The new VoiceState Data from Lavalink */
voice;
/** The Manager. */
manager;
static _manager;
data = {};
/** Checker if filters should be updated or not! */
filterUpdated;
/** When the player was created [Date] (from lavalink) */
createdAt;
/** When the player was created [Timestamp] (from lavalink) */
createdTimeStamp;
/** If lavalink says it's connected or not */
connected;
/** Last sent payload from lavalink */
payload;
/** A Voice-Region for voice-regioned based - Node identification(s) */
region;
/** The Ping to the Lavalink Client in ms | < 0 == not connected | undefined == not defined yet. */
ping;
/** The Voice Connection Ping from Lavalink in ms | < 0 == not connected | null == lavalinkversion is < 3.5.1 in where there is no ping info. | undefined == not defined yet. */
wsPing;
/** All States of a Filter, however you can manually overwrite it with a string, if you need so */
filters;
/** The Current Filter Data(s) */
filterData;
/**
* Set custom data.
* @param key
* @param value
*/
set(key, value) {
this.data[key] = value;
}
/**
* Get custom data.
* @param key
*/
get(key) {
return this.data[key];
}
/** @hidden */
static init(manager) {
this._manager = manager;
}
/**
* Creates a new player, returns one if it already exists.
* @param options
*/
constructor(options) {
this.options = options;
if (!this.manager)
this.manager = Utils_1.Structure.get("Player")._manager;
if (!this.manager)
throw new RangeError("Manager has not been initiated.");
if (this.manager.players.has(options.guild)) {
return this.manager.players.get(options.guild);
}
check(options);
/** When the player was created [Date] (from lavalink) | null */
this.createdAt = null;
/** When the player was created [Timestamp] (from lavalink) | 0 */
this.createdTimeStamp = 0;
/** If lavalink says it's connected or not */
this.connected = undefined;
/** Last sent payload from lavalink */
this.payload = {};
/** Ping to Lavalink from Client */
this.ping = undefined;
/** The Voice Connection Ping from Lavalink */
this.wsPing = undefined,
/** The equalizer bands array. */
this.bands = new Array(15).fill(0.0);
this.set("lastposition", undefined);
if (typeof options.customData === "object" && Object.keys(options.customData).length) {
this.data = { ...this.data, ...options.customData };
}
this.guild = options.guild;
this.voiceState = Object.assign({ op: "voiceUpdate", guildId: options.guild });
if (options.voiceChannel)
this.voiceChannel = options.voiceChannel;
if (options.textChannel)
this.textChannel = options.textChannel;
if (typeof options.instaUpdateFiltersFix === "undefined")
this.options.instaUpdateFiltersFix = true;
if (!this.manager.leastUsedNodes?.size) {
if (this.manager.initiated)
this.manager.initiated = false;
this.manager.init(this.manager.options?.clientId);
}
this.region = options?.region;
const customNode = this.manager.nodes.get(options.node);
const regionNode = this.manager.leastUsedNodes.filter(x => x.regions?.includes(options.region?.toLowerCase()))?.first();
this.node = customNode || regionNode || this.manager.leastUsedNodes.first();
if (!this.node)
throw new RangeError("No available nodes.");
this.filters = {
volume: false,
vaporwave: false,
custom: false,
nightcore: false,
echo: false,
reverb: false,
rotating: false,
rotation: false,
karaoke: false,
tremolo: false,
vibrato: false,
lowPass: false,
audioOutput: "stereo",
};
this.filterData = {
lowPass: {
smoothing: 0
},
karaoke: {
level: 0,
monoLevel: 0,
filterBand: 0,
filterWidth: 0
},
timescale: {
speed: 1,
pitch: 1,
rate: 1 // 0 = x
},
echo: {
delay: 0,
decay: 0
},
reverb: {
delay: 0,
decay: 0
},
rotation: {
rotationHz: 0
},
tremolo: {
frequency: 0,
depth: 0 // 0 < x = 1
},
vibrato: {
frequency: 0,
depth: 0 // 0 < x = 1
},
channelMix: exports.validAudioOutputs.stereo,
/*distortion: {
sinOffset: 0,
sinScale: 1,
cosOffset: 0,
cosScale: 1,
tanOffset: 0,
tanScale: 1,
offset: 0,
scale: 1
}*/
};
this.manager.players.set(options.guild, this);
this.manager.emit("playerCreate", this);
this.setVolume(options.volume ?? 100);
}
checkFiltersState(oldFilterTimescale) {
this.filters.rotation = this.filterData.rotation.rotationHz !== 0;
this.filters.vibrato = this.filterData.vibrato.frequency !== 0 || this.filterData.vibrato.depth !== 0;
this.filters.tremolo = this.filterData.tremolo.frequency !== 0 || this.filterData.tremolo.depth !== 0;
this.filters.echo = this.filterData.echo.decay !== 0 || this.filterData.echo.delay !== 0;
this.filters.reverb = this.filterData.reverb.decay !== 0 || this.filterData.reverb.delay !== 0;
this.filters.lowPass = this.filterData.lowPass.smoothing !== 0;
this.filters.karaoke = Object.values(this.filterData.karaoke).some(v => v !== 0);
if ((this.filters.nightcore || this.filters.vaporwave) && oldFilterTimescale) {
if (oldFilterTimescale.pitch !== this.filterData.timescale.pitch || oldFilterTimescale.rate !== this.filterData.timescale.rate || oldFilterTimescale.speed !== this.filterData.timescale.speed) {
this.filters.custom = Object.values(this.filterData.timescale).some(v => v !== 1);
this.filters.nightcore = false;
this.filters.vaporwave = false;
}
}
return true;
}
/**
* Reset all Filters
*/
async resetFilters() {
this.filters.echo = false;
this.filters.reverb = false;
this.filters.nightcore = false;
this.filters.lowPass = false;
this.filters.rotating = false;
this.filters.rotation = false;
this.filters.tremolo = false;
this.filters.vibrato = false;
this.filters.karaoke = false;
this.filters.karaoke = false;
this.filters.volume = false;
this.filters.audioOutput = "stereo";
// disable all filters
for (const [key, value] of Object.entries({
volume: 1,
lowPass: {
smoothing: 0
},
karaoke: {
level: 0,
monoLevel: 0,
filterBand: 0,
filterWidth: 0
},
timescale: {
speed: 1,
pitch: 1,
rate: 1 // 0 = x
},
echo: {
delay: 0,
decay: 0
},
reverb: {
delay: 0,
decay: 0
},
rotation: {
rotationHz: 0
},
tremolo: {
frequency: 2,
depth: 0.1 // 0 < x = 1
},
vibrato: {
frequency: 2,
depth: 0.1 // 0 < x = 1
},
channelMix: exports.validAudioOutputs.stereo,
})) {
this.filterData[key] = value;
}
await this.updatePlayerFilters();
return this.filters;
}
/**
* Set the AudioOutput Filter
* @param type
*/
async setAudioOutput(type) {
if (this.node.info && !this.node.info?.filters?.includes("channelMix"))
throw new Error("Node#Info#filters does not include the 'channelMix' Filter (Node has it not enable)");
if (!type || !exports.validAudioOutputs[type])
throw "Invalid audio type added, must be 'mono' / 'stereo' / 'left' / 'right'";
this.filterData.channelMix = exports.validAudioOutputs[type];
this.filters.audioOutput = type;
await this.updatePlayerFilters();
return this.filters.audioOutput;
}
/**
* Set custom filter.timescale#speed . This method disabled both: nightcore & vaporwave. use 1 to reset it to normal
* @param speed
* @returns
*/
async setSpeed(speed = 1) {
if (this.node.info && !this.node.info?.filters?.includes("timescale"))
throw new Error("Node#Info#filters does not include the 'timescale' Filter (Node has it not enable)");
// reset nightcore / vaporwave filter if enabled
if (this.filters.nightcore || this.filters.vaporwave) {
this.filterData.timescale.pitch = 1;
this.filterData.timescale.speed = 1;
this.filterData.timescale.rate = 1;
this.filters.nightcore = false;
this.filters.vaporwave = false;
}
this.filterData.timescale.speed = speed;
// check if custom filter is active / not
this.isCustomFilterActive();
await this.updatePlayerFilters();
return this.filters.custom;
}
/**
* Set custom filter.timescale#pitch . This method disabled both: nightcore & vaporwave. use 1 to reset it to normal
* @param speed
* @returns
*/
async setPitch(pitch = 1) {
if (this.node.info && !this.node.info?.filters?.includes("timescale"))
throw new Error("Node#Info#filters does not include the 'timescale' Filter (Node has it not enable)");
// reset nightcore / vaporwave filter if enabled
if (this.filters.nightcore || this.filters.vaporwave) {
this.filterData.timescale.pitch = 1;
this.filterData.timescale.speed = 1;
this.filterData.timescale.rate = 1;
this.filters.nightcore = false;
this.filters.vaporwave = false;
}
this.filterData.timescale.pitch = pitch;
// check if custom filter is active / not
this.isCustomFilterActive();
await this.updatePlayerFilters();
return this.filters.custom;
}
/**
* Set custom filter.timescale#rate . This method disabled both: nightcore & vaporwave. use 1 to reset it to normal
* @param speed
* @returns
*/
async setRate(rate = 1) {
if (this.node.info && !this.node.info?.filters?.includes("timescale"))
throw new Error("Node#Info#filters does not include the 'timescale' Filter (Node has it not enable)");
// reset nightcore / vaporwave filter if enabled
if (this.filters.nightcore || this.filters.vaporwave) {
this.filterData.timescale.pitch = 1;
this.filterData.timescale.speed = 1;
this.filterData.timescale.rate = 1;
this.filters.nightcore = false;
this.filters.vaporwave = false;
}
this.filterData.timescale.rate = rate;
// check if custom filter is active / not
this.isCustomFilterActive();
await this.updatePlayerFilters();
return this.filters.custom;
}
/**
* Enabels / Disables the rotation effect, (Optional: provide your Own Data)
* @param rotationHz
* @returns
*/
async toggleRotation(rotationHz = 0.2) {
if (this.node.info && !this.node.info?.filters?.includes("rotation"))
throw new Error("Node#Info#filters does not include the 'rotation' Filter (Node has it not enable)");
this.filterData.rotation.rotationHz = this.filters.rotation ? 0 : rotationHz;
this.filters.rotation = !this.filters.rotation;
/** @deprecated but sync with rotating */
this.filters.rotating = this.filters.rotation;
return await this.updatePlayerFilters(), this.filters.rotation;
}
/**
* @deprected - use #toggleRotation() Enabels / Disables the rotation effect, (Optional: provide your Own Data)
* @param rotationHz
* @returns
*/
async toggleRotating(rotationHz = 0.2) {
if (this.node.info && !this.node.info?.filters?.includes("rotation"))
throw new Error("Node#Info#filters does not include the 'rotation' Filter (Node has it not enable)");
this.filterData.rotation.rotationHz = this.filters.rotation ? 0 : rotationHz;
this.filters.rotation = !this.filters.rotation;
/** @deprecated but sync with rotating */
this.filters.rotating = this.filters.rotation;
return await this.updatePlayerFilters(), this.filters.rotation;
}
/**
* Enabels / Disables the Vibrato effect, (Optional: provide your Own Data)
* @param frequency
* @param depth
* @returns
*/
async toggleVibrato(frequency = 10, depth = 1) {
if (this.node.info && !this.node.info?.filters?.includes("vibrato"))
throw new Error("Node#Info#filters does not include the 'vibrato' Filter (Node has it not enable)");
this.filterData.vibrato.frequency = this.filters.vibrato ? 0 : frequency;
this.filterData.vibrato.depth = this.filters.vibrato ? 0 : depth;
this.filters.vibrato = !this.filters.vibrato;
await this.updatePlayerFilters();
return this.filters.vibrato;
}
/**
* Enabels / Disables the Tremolo effect, (Optional: provide your Own Data)
* @param frequency
* @param depth
* @returns
*/
async toggleTremolo(frequency = 4, depth = 0.8) {
if (this.node.info && !this.node.info?.filters?.includes("tremolo"))
throw new Error("Node#Info#filters does not include the 'tremolo' Filter (Node has it not enable)");
this.filterData.tremolo.frequency = this.filters.tremolo ? 0 : frequency;
this.filterData.tremolo.depth = this.filters.tremolo ? 0 : depth;
this.filters.tremolo = !this.filters.tremolo;
await this.updatePlayerFilters();
return this.filters.tremolo;
}
/**
* Enabels / Disables the LowPass effect, (Optional: provide your Own Data)
* @param smoothing
* @returns
*/
async toggleLowPass(smoothing = 20) {
if (this.node.info && !this.node.info?.filters?.includes("lowPass"))
throw new Error("Node#Info#filters does not include the 'lowPass' Filter (Node has it not enable)");
this.filterData.lowPass.smoothing = this.filters.lowPass ? 0 : smoothing;
this.filters.lowPass = !this.filters.lowPass;
await this.updatePlayerFilters();
return this.filters.lowPass;
}
/**
* Enabels / Disables the Echo effect, IMPORTANT! Only works with the correct Lavalink Plugin installed. (Optional: provide your Own Data)
* @param delay
* @param decay
* @returns
*/
async toggleEcho(delay = 1, decay = 0.5) {
if (this.node.info && !this.node.info?.filters?.includes("echo"))
throw new Error("Node#Info#filters does not include the 'echo' Filter (Node has it not enable aka not installed!)");
this.filterData.echo.delay = this.filters.echo ? 0 : delay;
this.filterData.echo.decay = this.filters.echo ? 0 : decay;
this.filters.echo = !this.filters.echo;
await this.updatePlayerFilters();
return this.filters.echo;
}
/**
* Enabels / Disables the Echo effect, IMPORTANT! Only works with the correct Lavalink Plugin installed. (Optional: provide your Own Data)
* @param delay
* @param decay
* @returns
*/
async toggleReverb(delay = 1, decay = 0.5) {
if (this.node.info && !this.node.info?.filters?.includes("reverb"))
throw new Error("Node#Info#filters does not include the 'reverb' Filter (Node has it not enable aka not installed!)");
this.filterData.reverb.delay = this.filters.reverb ? 0 : delay;
this.filterData.reverb.decay = this.filters.reverb ? 0 : decay;
this.filters.reverb = !this.filters.reverb;
await this.updatePlayerFilters();
return this.filters.reverb;
}
/**
* Enables / Disabels a Nightcore-like filter Effect. Disables/Overwrides both: custom and Vaporwave Filter
* @param speed
* @param pitch
* @param rate
* @returns
*/
async toggleNightcore(speed = 1.289999523162842, pitch = 1.289999523162842, rate = 0.9365999523162842) {
if (this.node.info && !this.node.info?.filters?.includes("timescale"))
throw new Error("Node#Info#filters does not include the 'timescale' Filter (Node has it not enable)");
this.filterData.timescale.speed = this.filters.nightcore ? 1 : speed;
this.filterData.timescale.pitch = this.filters.nightcore ? 1 : pitch;
this.filterData.timescale.rate = this.filters.nightcore ? 1 : rate;
this.filters.nightcore = !this.filters.nightcore;
this.filters.vaporwave = false;
this.filters.custom = false;
await this.updatePlayerFilters();
return this.filters.nightcore;
}
/**
* Enables / Disabels a Vaporwave-like filter Effect. Disables/Overwrides both: custom and nightcore Filter
* @param speed
* @param pitch
* @param rate
* @returns
*/
async toggleVaporwave(speed = 0.8500000238418579, pitch = 0.800000011920929, rate = 1) {
if (this.node.info && !this.node.info?.filters?.includes("timescale"))
throw new Error("Node#Info#filters does not include the 'timescale' Filter (Node has it not enable)");
this.filterData.timescale.speed = this.filters.vaporwave ? 1 : speed;
this.filterData.timescale.pitch = this.filters.vaporwave ? 1 : pitch;
this.filterData.timescale.rate = this.filters.vaporwave ? 1 : rate;
this.filters.vaporwave = !this.filters.vaporwave;
this.filters.nightcore = false;
this.filters.custom = false;
await this.updatePlayerFilters();
return this.filters.vaporwave;
}
/**
* Enable / Disables a Karaoke like Filter Effect
* @param level
* @param monoLevel
* @param filterBand
* @param filterWidth
* @returns
*/
async toggleKaraoke(level = 1, monoLevel = 1, filterBand = 220, filterWidth = 100) {
if (this.node.info && !this.node.info?.filters?.includes("karaoke"))
throw new Error("Node#Info#filters does not include the 'karaoke' Filter (Node has it not enable)");
this.filterData.karaoke.level = this.filters.karaoke ? 0 : level;
this.filterData.karaoke.monoLevel = this.filters.karaoke ? 0 : monoLevel;
this.filterData.karaoke.filterBand = this.filters.karaoke ? 0 : filterBand;
this.filterData.karaoke.filterWidth = this.filters.karaoke ? 0 : filterWidth;
this.filters.karaoke = !this.filters.karaoke;
await this.updatePlayerFilters();
return this.filters.karaoke;
}
/** Function to find out if currently there is a custom timescamle etc. filter applied */
isCustomFilterActive() {
this.filters.custom = !this.filters.nightcore && !this.filters.vaporwave && Object.values(this.filterData.timescale).some(d => d !== 1);
return this.filters.custom;
}
// function to update all filters at ONCE (and eqs)
async updatePlayerFilters() {
const sendData = { ...this.filterData };
if (!this.filters.volume)
delete sendData.volume;
if (!this.filters.tremolo)
delete sendData.tremolo;
if (!this.filters.vibrato)
delete sendData.vibrato;
//if(!this.filters.karaoke) delete sendData.karaoke;
if (!this.filters.echo)
delete sendData.echo;
if (!this.filters.reverb)
delete sendData.reverb;
if (!this.filters.lowPass)
delete sendData.lowPass;
if (!this.filters.karaoke)
delete sendData.karaoke;
//if(!this.filters.rotating) delete sendData.rotating;
if (this.filters.audioOutput === "stereo")
delete sendData.channelMix;
const now = Date.now();
if (!this.node.sessionId) {
if (sendData.rotation) {
// @ts-ignore
sendData.rotating = sendData.rotation;
delete sendData.rotation;
} // on websocket it's called rotating, and on rest it's called rotation
console.warn("@deprecated - The Lavalink-Node is either not up to date (or not ready)! -- Using WEBSOCKET instead of REST (player#updatePlayerFilters)");
await this.node.send({
op: "filters",
guildId: this.guild,
equalizer: this.bands.map((gain, band) => ({ band, gain })),
...sendData
});
}
else {
sendData.equalizer = this.bands.map((gain, band) => ({ band, gain }));
for (const key of [...Object.keys(sendData)]) {
if (this.node.info && !this.node.info?.filters?.includes?.(key))
delete sendData[key];
}
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: {
filters: sendData,
}
});
}
this.ping = Date.now() - now;
if (this.options.instaUpdateFiltersFix === true)
this.filterUpdated = 1;
return this;
}
/**
* Same as Manager#search() but a shortcut on the player itself. Custom Node is provided via player.node internally
* @param query
* @param requester
*/
search(query, requester) {
return this.manager.search(query, requester, this.node);
}
/**
* Sets the players equalizer band on-top of the existing ones.
* @param bands
*/
async setEQ(...bands) {
// Hacky support for providing an array
if (Array.isArray(bands[0]))
bands = bands[0];
if (!bands.length || !bands.every((band) => JSON.stringify(Object.keys(band).sort()) === '["band","gain"]'))
throw new TypeError("Bands must be a non-empty object array containing 'band' and 'gain' properties.");
for (const { band, gain } of bands)
this.bands[band] = gain;
if (!this.node.sessionId) {
console.warn("@deprecated - The Lavalink-Node is either not up to date (or not ready)! -- Using WEBSOCKET instead of REST (player#setEQ)");
await this.node.send({
op: "filters",
guildId: this.guild,
equalizer: this.bands.map((gain, band) => ({ band, gain })),
});
}
else {
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: {
filters: { equalizer: this.bands.map((gain, band) => ({ band, gain })) }
}
});
}
return this;
}
/** Clears the equalizer bands. */
async clearEQ() {
this.bands = new Array(15).fill(0.0);
if (!this.node.sessionId) {
console.warn("@deprecated - The Lavalink-Node is either not up to date (or not ready)! -- Using WEBSOCKET instead of REST (player#clearEQ)");
await this.node.send({
op: "filters",
guildId: this.guild,
equalizer: this.bands.map((gain, band) => ({ band, gain })),
});
}
else {
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: {
filters: { equalizer: this.bands.map((gain, band) => ({ band, gain })) }
}
});
}
return this;
}
/** Connect to the voice channel. */
connect() {
if (!this.voiceChannel)
throw new RangeError("No voice channel has been set.");
this.state = "CONNECTING";
this.manager.options.send(this.guild, {
op: 4,
d: {
guild_id: this.guild,
channel_id: this.voiceChannel,
self_mute: this.options.selfMute || false,
self_deaf: this.options.selfDeafen || false,
},
});
this.state = "CONNECTED";
return this;
}
/** Disconnect from the voice channel. */
disconnect() {
if (this.voiceChannel === null)
return this;
this.state = "DISCONNECTING";
this.pause(true);
this.manager.options.send(this.guild, {
op: 4,
d: {
guild_id: this.guild,
channel_id: null,
self_mute: false,
self_deaf: false,
},
});
this.voiceChannel = null;
this.state = "DISCONNECTED";
return this;
}
/** Destroys the player. */
async destroy(disconnect = true) {
this.state = "DESTROYING";
if (disconnect) {
this.disconnect();
}
await this.node.destroyPlayer(this.guild);
this.manager.emit("playerDestroy", this);
this.manager.players.delete(this.guild);
}
/**
* Sets the player voice channel.
* @param channel
*/
setVoiceChannel(channel) {
if (typeof channel !== "string")
throw new TypeError("Channel must be a non-empty string.");
this.voiceChannel = channel;
this.connect();
return this;
}
/**
* Sets the player text channel.
* @param channel
*/
setTextChannel(channel) {
if (typeof channel !== "string")
throw new TypeError("Channel must be a non-empty string.");
this.textChannel = channel;
return this;
}
async play(optionsOrTrack, playOptions) {
if (typeof optionsOrTrack !== "undefined" &&
Utils_1.TrackUtils.validate(optionsOrTrack)) {
if (this.queue.current)
this.queue.previous = this.queue.current;
this.queue.current = optionsOrTrack;
}
if (!this.queue.current)
throw new RangeError("No current track.");
const finalOptions = getOptions(playOptions || optionsOrTrack, !!this.node.sessionId) ? optionsOrTrack : {};
if (Utils_1.TrackUtils.isUnresolvedTrack(this.queue.current)) {
try {
this.queue.current = await Utils_1.TrackUtils.getClosestTrack(this.queue.current, this.node);
}
catch (error) {
this.manager.emit("trackError", this, this.queue.current, error);
if (this.queue[0])
return this.play(this.queue[0]);
return;
}
}
const options = {
guildId: this.guild,
encodedTrack: this.queue.current.track,
...finalOptions,
};
if (typeof options.encodedTrack !== "string") {
options.encodedTrack = options.encodedTrack.track;
}
if (typeof options.volume === "number" && !isNaN(options.volume)) {
this.volume = Math.max(Math.min(options.volume, 500), 0);
let vol = Number(this.volume);
if (this.manager.options.volumeDecrementer)
vol *= this.manager.options.volumeDecrementer;
this.lavalinkVolume = Math.floor(vol * 100) / 100;
options.volume = vol;
}
this.set("lastposition", this.position);
const now = Date.now();
if (!this.node.sessionId) {
console.warn("@deprecated - The Lavalink-Node is either not up to date (or not ready)! -- Using WEBSOCKET instead of REST (player#play)");
await this.node.send({
track: options.encodedTrack,
op: "play",
guildId: this.guild,
...finalOptions
});
}
else {
await this.node.updatePlayer({
guildId: this.guild,
noReplace: finalOptions.noReplace ?? false,
playerOptions: options,
});
}
this.ping = Date.now() - now;
return;
}
/**
* Sets the player volume.
* @param volume 0-500
*/
async setVolume(volume) {
volume = Number(volume);
if (isNaN(volume))
throw new TypeError("Volume must be a number.");
this.volume = Math.max(Math.min(volume, 500), 0);
let vol = Number(this.volume);
if (this.manager.options.volumeDecrementer)
vol *= this.manager.options.volumeDecrementer;
this.lavalinkVolume = Math.floor(vol * 100) / 100;
const now = Date.now();
if (!this.node.sessionId) {
console.warn("@deprecated - The Lavalink-Node is either not up to date (or not ready)! -- Using WEBSOCKET instead of REST (player#setVolume)");
await this.node.send({
op: "volume",
guildId: this.guild,
volume: vol,
});
}
else {
if (this.manager.options.applyVolumeAsFilter) {
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: {
filters: { volume: vol / 100 }
}
});
}
else {
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: {
volume: vol
}
});
}
}
this.ping = Date.now() - now;
return this;
}
/**
* Applies a Node-Filter for Volume (make it louder/quieter without distortion | only for new REST api).
* @param volume 0-5
*/
async setVolumeFilter(volume) {
if (!this.node.sessionId)
throw new Error("The Lavalink-Node is either not ready, or not up to date! (REST Api must be useable)");
volume = Number(volume);
if (isNaN(volume))
throw new TypeError("Volume must be a number.");
this.filterData.volume = Math.max(Math.min(volume, 5), 0);
this.filters.volume = this.filterData.volume === 1 ? false : true;
const now = Date.now();
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: {
filters: { volume: this.filterData.volume }
}
});
this.ping = Date.now() - now;
return this;
}
/**
* Sets the track repeat.
* @param repeat
*/
setTrackRepeat(repeat) {
if (typeof repeat !== "boolean")
throw new TypeError('Repeat can only be "true" or "false".');
if (repeat) {
this.trackRepeat = true;
this.queueRepeat = false;
}
else {
this.trackRepeat = false;
this.queueRepeat = false;
}
return this;
}
/**
* Sets the queue repeat.
* @param repeat
*/
setQueueRepeat(repeat) {
if (typeof repeat !== "boolean")
throw new TypeError('Repeat can only be "true" or "false".');
if (repeat) {
this.trackRepeat = false;
this.queueRepeat = true;
}
else {
this.trackRepeat = false;
this.queueRepeat = false;
}
return this;
}
/** Stops the current track, optionally give an amount to skip to, e.g 5 would play the 5th song. */
async stop(amount) {
if (typeof amount === "number" && amount > 1) {
if (amount > this.queue.length)
throw new RangeError("Cannot skip more than the queue length.");
this.queue.splice(0, amount - 1);
}
const now = Date.now();
if (!this.node.sessionId) {
console.warn("@deprecated - The Lavalink-Node is either not up to date (or not ready)! -- Using WEBSOCKET instead of REST (player#stop)");
await this.node.send({
op: "stop",
guildId: this.guild,
});
}
else {
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: { encodedTrack: null }
});
}
this.ping = Date.now() - now;
return this;
}
/**
* Pauses the current track.
* @param pause
*/
async pause(paused) {
if (typeof paused !== "boolean")
throw new RangeError('Pause can only be "true" or "false".');
// If already paused or the queue is empty do nothing https://github.com/MenuDocs/erela.js/issues/58
if (this.paused === paused || !this.queue.totalSize)
return this;
this.playing = !paused;
this.paused = paused;
const now = Date.now();
if (!this.node.sessionId) {
console.warn("@deprecated - The Lavalink-Node is either not up to date (or not ready)! -- Using WEBSOCKET instead of REST (player#pause)");
await this.node.send({
op: "pause",
guildId: this.guild,
pause: paused,
});
}
else {
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: { paused },
});
}
this.ping = Date.now() - now;
return this;
}
/**
* Seeks to the position in the current track.
* @param position
*/
async seek(position) {
if (!this.queue.current)
return undefined;
position = Number(position);
if (isNaN(position)) {
throw new RangeError("Position must be a number.");
}
if (position < 0 || position > this.queue.current.duration)
position = Math.max(Math.min(position, this.queue.current.duration), 0);
this.position = position;
this.set("lastposition", this.position);
const now = Date.now();
if (!this.node.sessionId) {
console.warn("@deprecated - The Lavalink-Node is either not up to date (or not ready)! -- Using WEBSOCKET instead of REST (player#seek)");
await this.node.send({
op: "seek",
guildId: this.guild,
position,
});
}
else {
await this.node.updatePlayer({
guildId: this.guild,
playerOptions: { position }
});
}
this.ping = Date.now() - now;
return this;
}
}
exports.Player = Player;
function getOptions(opts, allowFilters) {
const valids = ["startTime", "endTime", "noReplace", "volume", "pause", "filters"];
const returnObject = {};
if (!opts)
return false;
for (const [key, value] of Object.entries(Object.assign({}, opts))) {
if (valids.includes(key) && (key !== "filters" || (key === "filters" && allowFilters))) {
returnObject[key] = value;
}
}
return returnObject;