-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MoonlinkPlayer.ts
546 lines (501 loc) · 17 KB
/
MoonlinkPlayer.ts
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
import {
MoonlinkRestFul,
MoonlinkManager,
MoonlinkQueue,
MoonlinkNode,
Structure
} from "../../index";
import { PlayerInfos, connectOptions, PreviousInfosPlayer } from "../@Typings";
export class MoonlinkPlayer {
public manager: MoonlinkManager;
private infos: PlayerInfos;
private map: Map<string, any>;
public guildId: string;
public textChannel: string;
public voiceChannel: string;
public autoPlay: boolean | null;
public autoLeave: boolean | null;
public connected: boolean | null;
public playing: boolean | null;
public paused: boolean | null;
public loop: number | null;
public volume: number;
public shuffled: boolean | null;
public ping: number;
public queue: MoonlinkQueue;
public current: Record<string, any>;
public previous: Record<string, any>;
public data: Record<string, any>;
public node: MoonlinkNode | any;
public rest: MoonlinkRestFul;
/**
* Creates an instance of MoonlinkPlayer.
* @param infos - Player information.
* @param manager - MoonlinkManager instance.
* @param map - Map for storing player data.
*/
constructor(
infos: PlayerInfos,
manager: MoonlinkManager,
map: Map<string, any>
) {
// Initialize properties and set default values based on input parameters.
this.guildId = infos.guildId;
this.textChannel = infos.textChannel;
this.voiceChannel = infos.voiceChannel;
this.autoPlay = infos.autoPlay;
this.autoLeave = infos.autoLeave || false;
this.connected = infos.connected || null;
this.playing = infos.playing || null;
this.paused = infos.paused || null;
this.loop = infos.loop || null;
this.volume = infos.volume || 90;
this.shuffled = infos.shuffled || false;
this.ping = infos.ping || 0;
this.queue = new (Structure.get("MoonlinkQueue"))(manager, this);
this.current = map.get("current") || {};
this.current = this.current[this.guildId];
this.previous = map.get("previous") || {};
this.previous = this.previous[this.guildId];
this.map = map;
this.data = this.map.get("players") || {};
this.data = this.data[this.guildId];
this.node = manager.nodes.get(this.get("node"));
this.rest = this.node.rest;
this.manager = manager;
const existingData =
this.queue.db.get<PreviousInfosPlayer>(`players.${this.guildId}`) ||
{};
if (
this.voiceChannel &&
this.voiceChannel !==
(existingData.voiceChannel && existingData.voiceChannel)
) {
existingData.voiceChannel = this.voiceChannel;
}
if (
this.textChannel &&
this.textChannel !==
(existingData.textChannel && existingData.textChannel)
) {
existingData.textChannel = this.textChannel;
}
if (
existingData !==
(this.queue.db.get<PreviousInfosPlayer>(
`players.${this.guildId}`
) || {})
) {
this.queue.db.set<PreviousInfosPlayer>(
`players.${this.guildId}`,
existingData
);
}
}
/**
* Private method to update player information in the map.
*/
private updatePlayers(): void {
let players = this.map.get("players") || {};
players[this.guildId] = this.data;
this.map.set("players", players);
}
/**
* Set a key-value pair in the player's data and update the map.
* @param key - The key to set.
* @param value - The value to set.
*/
public set(key: string, value: unknown): void {
this.data[key] = value;
this.updatePlayers();
}
/**
* Get a value from the player's data.
* @param key - The key to retrieve.
* @returns The value associated with the key.
*/
public get<T>(key: string): T {
this.updatePlayers();
return (this.data[key] as T) || null;
}
/**
* Set the text channel for the player.
* @param channelId - The ID of the text channel.
* @returns True if the channel was set successfully.
* @throws Error if channelId is empty or not a string.
*/
public setTextChannel(channelId: string): boolean {
if (!channelId) {
throw new Error(
'[ @Moonlink/Player ]: "channelId" option is empty'
);
}
if (typeof channelId !== "string") {
throw new Error(
'[ @Moonlink/Player ]: option "channelId" is different from a string'
);
}
this.set("textChannel", channelId);
this.textChannel = channelId;
return true;
}
/**
* Set the voice channel for the player.
* @param channelId - The ID of the voice channel.
* @returns True if the channel was set successfully.
* @throws Error if channelId is empty or not a string.
*/
public setVoiceChannel(channelId: string): boolean {
if (!channelId) {
throw new Error(
'[ @Moonlink/Player ]: "channelId" option is empty'
);
}
if (typeof channelId !== "string") {
throw new Error(
'[ @Moonlink/Player ]: option "channelId" is different from a string'
);
}
this.set("voiceChannel", channelId);
this.voiceChannel = channelId;
return true;
}
/* Logic created by PiscesXD */
public setAutoLeave(mode?: boolean | null): boolean | null {
if (typeof mode !== "boolean") {
throw new Error(
'[ @Moonlink/Player ]: "mode" option is empty or different from a boolean'
);
}
mode ? mode : (mode = !this.autoLeave);
this.set("autoLeave", mode);
this.autoLeave = mode;
return mode;
}
/**
* Set the auto-play mode for the player.
* @param mode - Auto-play mode (true/false).
* @returns True if the mode was set successfully.
* @throws Error if mode is not a boolean.
*/
public setAutoPlay(mode: boolean): boolean {
if (typeof mode !== "boolean") {
throw new Error(
'[ @Moonlink/Player ]: "mode" option is empty or different from a boolean'
);
}
this.set("autoPlay", mode);
this.autoPlay = mode;
return mode;
}
/**
* Connect the player to a voice channel with optional connection options.
* @param options - Connection options (setMute, setDeaf).
* @returns True if the connection was successful.
*/
public connect(options: connectOptions): boolean | null {
options = options || { setDeaf: false, setMute: false };
const { setDeaf, setMute } = options;
this.set("connected", true);
this.manager._SPayload(
this.guildId,
JSON.stringify({
op: 4,
d: {
guild_id: this.guildId,
channel_id: this.voiceChannel,
self_mute: setMute,
self_deaf: setDeaf
}
})
);
return true;
}
/**
* Disconnect the player from the voice channel.
* @returns True if the disconnection was successful.
*/
public disconnect(): boolean {
this.set("connected", false);
this.set("voiceChannel", null);
this.manager._SPayload(
this.guildId,
JSON.stringify({
op: 4,
d: {
guild_id: this.guildId,
channel_id: null,
self_mute: false,
self_deaf: false
}
})
);
return true;
}
/**
* Restart the player by reconnecting and updating its state.
*/
public async restart(): Promise<void> {
if (!this.current) return;
await this.manager.players.attemptConnection(this.guildId);
await this.rest.update({
guildId: this.guildId,
data: {
track: {
encoded: this.current.encoded
},
position: this.current.position,
volume: this.volume
}
});
}
/**
* Play the next track in the queue.
*/
public async play(): Promise<void> {
// modified by PiscesXD
if (!this.queue.size) return;
let queue: any = this.queue.db.get(`queue.${this.guildId}`);
let data: any = queue.shift();
if (!data) return;
let current = this.map.get("current") || {};
if (this.loop && Object.keys(current).length != 0) {
current[this.guildId].time
? (current[this.guildId].time = 0)
: false;
this.set("ping", undefined);
queue.push(current[this.guildId]);
}
current[this.guildId] = data;
this.current = current[this.guildId];
this.map.set("current", current);
await this.queue.db.set(`queue.${this.guildId}`, queue);
await this.rest.update({
guildId: this.guildId,
data: {
track: {
encoded: data.encoded
},
volume: this.volume
}
});
}
/**
* Pause the playback.
* @returns True if paused successfully.
*/
public async pause(): Promise<boolean> {
if (this.paused) return true;
await this.updatePlaybackStatus(true);
return true;
}
/**
* Resume the playback.
* @returns True if resumed successfully.
*/
public async resume(): Promise<boolean> {
if (this.playing) return true;
await this.updatePlaybackStatus(false);
return true;
}
/**
* Private method to update the playback status (paused or resumed).
* @param paused - Indicates whether to pause or resume the playback.
*/
private async updatePlaybackStatus(paused: boolean): Promise<void> {
await this.rest.update({
guildId: this.guildId,
data: { paused }
});
this.set("paused", paused);
this.set("playing", !paused);
}
/**
* Stop the playback and optionally clear player data.
* @returns True if stopped successfully.
*/
public async stop(destroy?: boolean): Promise<boolean> {
if (!this.queue.size) {
await this.rest.update({
guildId: this.guildId,
data: { track: { encoded: null } }
});
}
destroy ? this.destroy() : this.queue.clear();
return true;
}
/**
* Skip to the next track in the queue.
* @returns True if the next track was successfully played.
*/
public async skip(): Promise<boolean> {
/*
@Author: PiscesXD
Track shuffling logic
*/
if (this.queue.size && this.data.shuffled) {
let currentQueue: string[] = this.queue.db.get(
`queue.${this.guildId}`
);
const randomIndex = Math.floor(Math.random() * currentQueue.length);
const shuffledTrack = currentQueue.splice(randomIndex, 1)[0];
currentQueue.unshift(shuffledTrack);
this.queue.db.set(`queue.${this.guildId}`, currentQueue);
this.play();
return;
}
if (this.queue.size) {
this.play();
return false;
} else {
this.stop();
return true;
}
}
/**
* Set the volume level for the player.
* @param percent - Volume percentage (0 to Infinity).
* @returns The new volume level.
* @throws Error if the volume is not a valid number or player is not playing.
*/
public async setVolume(percent: number): Promise<number> {
if (typeof percent == "undefined" || typeof percent !== "number") {
throw new Error(
'[ @Moonlink/Player ]: option "percent" is empty or different from a number'
);
}
if (!this.playing) {
throw new Error(
"[ @Moonlink/Player ]: cannot change volume while the player is not playing"
);
}
await this.rest.update({
guildId: this.guildId,
data: { volume: percent }
});
this.set("volume", percent);
return percent;
}
/**
* Set the loop mode for the player.
* @param mode - Loop mode (0 for no loop, 1 for single track, 2 for entire queue).
* @returns The new loop mode.
* @throws Error if the mode is not a valid number or out of range.
*/
public setLoop(mode: number | null): number | null {
if (
typeof mode !== "number" ||
(mode !== null && (mode < 0 || mode > 2))
) {
throw new Error(
'[ @Moonlink/Player ]: the option "mode" is different from a number or the option does not exist'
);
}
this.set("loop", mode);
return mode;
}
/**
* Destroy the player, disconnecting it and removing player data.
* @returns True if the player was successfully destroyed.
*/
public async destroy(): Promise<boolean> {
if (this.connected) this.disconnect();
await this.rest.destroy(this.guildId);
this.queue.clear();
let players = this.map.get("players");
delete players[this.guildId];
this.map.set("players", players);
return true;
}
/**
* Private method to validate a number parameter.
* @param param - The number parameter to validate.
* @param paramName - The name of the parameter.
* @throws Error if the parameter is not a valid number.
*/
private validateNumberParam(param: number, paramName: string): void {
if (typeof param !== "number") {
throw new Error(
`[ @Moonlink/Player ]: option "${paramName}" is empty or different from a number`
);
}
}
/**
* Seek to a specific position in the current track.
* @param position - The position to seek to.
* @returns The new position after seeking.
* @throws Error if the position is greater than the track duration or seek is not allowed.
*/
public async seek(position: number): Promise<number | null> {
this.validateNumberParam(position, "position");
if (position >= this.current.duration) {
throw new Error(
`[ @Moonlink/Player ]: parameter "position" is greater than the duration of the current track`
);
}
if (!this.current.isSeekable && this.current.isStream) {
throw new Error(
`[ @Moonlink/Player ]: seek function cannot be applied on live video or cannot be applied in "isSeekable"`
);
}
await this.rest.update({
guildId: this.guildId,
data: { position }
});
return position;
}
/**
* Skip to a specific position in the queue.
* @param position - The position to skip to.
* @returns True if the position exists and the skip was successful.
* @throws Error if the queue is empty, or the indicated position does not exist.
*/
public async skipTo(position: number): Promise<boolean | void> {
this.validateNumberParam(position, "position");
if (!this.queue.size) {
throw new Error(
`[ @Moonlink/Player ]: the queue is empty to use this function`
);
}
let queue: string[] = this.queue.db.get(`queue.${this.guildId}`);
if (!queue[position - 1]) {
throw new Error(
`[ @Moonlink/Player ]: the indicated position does not exist, make security in your code to avoid errors`
);
}
let data: any = queue.splice(position - 1, 1)[0];
let currents: any = this.map.get("current") || {};
currents[this.guildId] = data;
this.map.set("current", currents);
this.queue.db.set(`queue.${this.guildId}`, queue);
await this.rest.update({
guildId: this.guildId,
data: {
track: { encoded: data.encoded },
volume: 80
}
});
return true;
}
/**
* Shuffle the tracks in the queue.
* @returns True if the shuffle was successful.
* @throws Error if the queue is empty.
*/
public shuffle(mode?: boolean | null): boolean | null {
/*
@Author: PiscesXD
Track shuffling logic
*/
if (!this.queue.size) {
throw new Error(
`[ @Moonlink/Player ]: The "shuffle" method doesn't work if there are no tracks in the queue`
);
return false;
}
mode ? mode : (mode = !this.shuffled);
this.set("shuffled", mode);
this.shuffled = mode;
return mode;
}
}