-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
81 lines (67 loc) · 1.41 KB
/
queue.go
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
package main
import (
"math/rand"
"time"
"github.com/disgoorg/disgolink/v3/lavalink"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type QueueType string
const (
QueueTypeNormal QueueType = "normal"
QueueTypeRepeatTrack QueueType = "repeat_track"
QueueTypeRepeatQueue QueueType = "repeat_queue"
)
func (q QueueType) String() string {
switch q {
case QueueTypeNormal:
return "Normal"
case QueueTypeRepeatTrack:
return "Repeat Track"
case QueueTypeRepeatQueue:
return "Repeat Queue"
default:
return "unknown"
}
}
type Queue struct {
Tracks []lavalink.Track
Type QueueType
}
func (q *Queue) Shuffle() {
rand.Shuffle(len(q.Tracks), func(i, j int) {
q.Tracks[i], q.Tracks[j] = q.Tracks[j], q.Tracks[i]
})
}
func (q *Queue) Add(track ...lavalink.Track) {
q.Tracks = append(q.Tracks, track...)
}
func (q *Queue) Next() (lavalink.Track, bool) {
if len(q.Tracks) == 0 {
return lavalink.Track{}, false
}
track := q.Tracks[0]
q.Tracks = q.Tracks[1:]
return track, true
}
func (q *Queue) Clear() {
q.Tracks = make([]lavalink.Track, 0)
}
type QueueManager struct {
queues map[string]*Queue
}
func (q *QueueManager) Get(guildID string) *Queue {
queue, ok := q.queues[guildID]
if !ok {
queue = &Queue{
Tracks: make([]lavalink.Track, 0),
Type: QueueTypeNormal,
}
q.queues[guildID] = queue
}
return queue
}
func (q *QueueManager) Delete(guildID string) {
delete(q.queues, guildID)
}