-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
89 lines (75 loc) · 1.78 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
82
83
84
85
86
87
88
89
package main
import (
"math/rand"
"time"
"github.com/disgoorg/disgolink/v3/lavalink"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type QueueType string
const (
QueueTypeNoRepeat QueueType = "no_repeat"
QueueTypeRepeatTrack QueueType = "repeat_track"
QueueTypeRepeatQueue QueueType = "repeat_queue"
)
func (q QueueType) String() string {
switch q {
case QueueTypeNoRepeat:
return "No Repeat"
case QueueTypeRepeatTrack:
return "Repeat Track"
case QueueTypeRepeatQueue:
return "Repeat Queue"
default:
return "unknown"
}
}
type Queue struct {
Length lavalink.Duration
Tracks []lavalink.Track
Type QueueType
}
func (q *Queue) RecalculateDuration() {
q.Length = 0
for _, track := range q.Tracks {
q.Length += track.Info.Length
}
}
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(tracks ...lavalink.Track) {
q.Tracks = append(q.Tracks, tracks...)
q.RecalculateDuration()
}
func (q *Queue) Next() (lavalink.Track, bool) {
return q.Skip(1)
}
func (q *Queue) Skip(amount int) (lavalink.Track, bool) {
if len(q.Tracks) == 0 {
return lavalink.Track{}, false
}
if amount > len(q.Tracks) {
amount = len(q.Tracks)
}
var skippedTrack lavalink.Track
skippedTrack, q.Tracks = q.Tracks[amount-1], q.Tracks[amount:]
q.RecalculateDuration()
return skippedTrack, true
}
func (q *Queue) Remove(index int) (removedTrack lavalink.Track, ok bool) {
if len(q.Tracks) == 0 || index < 0 || index >= len(q.Tracks) {
return lavalink.Track{}, false
}
removedTrack = q.Tracks[index]
q.Tracks = append(q.Tracks[:index], q.Tracks[index+1:]...)
q.RecalculateDuration()
return removedTrack, true
}
func (q *Queue) Clear() {
q.Tracks = make([]lavalink.Track, 0)
q.Length = 0
}