-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracks.go
99 lines (83 loc) · 1.7 KB
/
tracks.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
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"sync"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgolink/v3/lavalink"
)
type Tracks struct {
store []lavalink.Track
mu sync.Mutex
}
func (t *Tracks) First() lavalink.Track {
t.mu.Lock()
track := t.store[0]
t.mu.Unlock()
return track
}
func (t *Tracks) All() []lavalink.Track {
t.mu.Lock()
tracks := t.store
t.mu.Unlock()
return tracks
}
func (t *Tracks) Few(n int) []lavalink.Track {
t.mu.Lock()
if n > len(t.store) {
n = len(t.store)
}
tracks := t.store[:n]
t.mu.Unlock()
return tracks
}
func (t *Tracks) Get(index int) lavalink.Track {
t.mu.Lock()
track := t.store[index]
t.mu.Unlock()
return track
}
func (t *Tracks) GetTrackEmbed(track lavalink.Track) discord.Embed {
info := track.Info
user := UserInfo{}
err := track.UserData.Unmarshal(&user)
if err != nil {
return discord.NewEmbedBuilder().SetDescription(err.Error()).Build()
}
embed := discord.NewEmbedBuilder().
SetTitle(info.Title).
SetURL(*info.URI).
SetThumbnail(*info.ArtworkURL).
SetAuthor(info.Author, "", "").
SetFooter(fmt.Sprintf("Requested by %s", user.Username), user.Avatar).
Build()
return embed
}
func (t *Tracks) Push(track lavalink.Track) {
t.mu.Lock()
t.store = append(t.store, track)
t.mu.Unlock()
}
func (t *Tracks) Pop() lavalink.Track {
t.mu.Lock()
track := t.store[0]
t.store = t.store[1:]
t.mu.Unlock()
return track
}
func (t *Tracks) Replace(index int, track lavalink.Track) {
t.mu.Lock()
t.store[index] = track
t.mu.Unlock()
}
func (t *Tracks) Len() int {
t.mu.Lock()
length := len(t.store)
t.mu.Unlock()
return length
}
func (t *Tracks) Empty() bool {
t.mu.Lock()
empty := len(t.store) == 0
t.mu.Unlock()
return empty
}