forked from MotiCAT/TuneNekoSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.ts
79 lines (61 loc) · 1.49 KB
/
queue.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
import { Snowflake } from 'discord.js';
export class Queue {
private _store: string[];
public loop: 'none' | 'queue' | 'track';
constructor() {
this._store = [];
this.loop = 'none';
}
public get store(): string[] {
return this._store;
}
public get currentSong(): string {
return this._store[0];
}
public get length(): number {
return this._store.length;
}
public addSong(url: string): void {
this._store.push(url);
}
public removeSong(index: number): void {
this._store.splice(index, 1);
}
public shift(): string | undefined {
return this._store.shift();
}
public shuffle(): void {
this._store.sort(() => Math.random() - 0.5);
}
public loopQueue(): void {
const first = this._store.shift();
if (first) this._store.push(first);
}
public loopTrack(): void {
const first = this._store.shift();
if (first) this._store.unshift(first);
}
public setLoop(loop: 'none' | 'queue' | 'track'): string {
this.loop = loop;
return loop;
}
}
class QueueManager {
private _queues: Map<Snowflake, Queue>;
constructor() {
this._queues = new Map();
}
public get queues(): Map<Snowflake, Queue> {
return this._queues;
}
public getQueue(serverId: Snowflake): Queue | undefined {
return this._queues.get(serverId);
}
public setQueue(serverId: Snowflake, queue: Queue): void {
this._queues.set(serverId, queue);
}
public deleteQueue(serverId: Snowflake): boolean {
return this._queues.delete(serverId);
}
}
export const queueManager = new QueueManager();