This repository has been archived by the owner on Mar 11, 2022. It is now read-only.
/
voicequeue.go
148 lines (134 loc) · 4.34 KB
/
voicequeue.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
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
package main
import (
"fmt"
"time"
"github.com/bwmarrin/discordgo"
)
func (voice *Voice) QueueAdd(entry *QueueEntry) {
//Add the new queue entry
voice.Entries = append(voice.Entries, entry)
}
func (voice *Voice) QueueRemove(entry int) {
if voice.Shuffle {
//Remove the underlying queue entry
voice.queueRemoveNormal(voice.ShuffledPointers[entry])
//Remove the pointer from the shuffled queue entries
voice.queueRemovePointer(entry)
} else {
//Remove the queue entry
voice.queueRemoveNormal(entry)
}
}
func (voice *Voice) queueRemoveNormal(entry int) {
voice.Entries = append(voice.Entries[:entry], voice.Entries[entry+1:]...)
}
func (voice *Voice) queueRemovePointer(entry int) {
voice.ShuffledPointers = append(voice.ShuffledPointers[:entry], voice.ShuffledPointers[entry+1:]...)
}
func (voice *Voice) QueueRemoveRange(start, end int) {
if len(voice.Entries) == 0 {
return
}
if start < 0 {
start = 0
}
if end > len(voice.Entries) {
end = len(voice.Entries)
}
for entry := end; entry < start; entry-- {
voice.QueueRemove(entry)
}
}
func (voice *Voice) QueueClear() {
voice.Entries = nil
voice.ShuffledPointers = nil
}
func (voice *Voice) QueueGet(entry int) *QueueEntry {
if len(voice.Entries) < entry {
return nil
}
if voice.Shuffle {
return voice.Entries[voice.ShuffledPointers[entry]]
}
return voice.Entries[entry]
}
func (voice *Voice) QueueGetNext() *QueueEntry {
if len(voice.Entries) == 0 {
return nil
}
return voice.Entries[0]
}
// QueueEntry stores the data about a queue entry
type QueueEntry struct {
Metadata *Metadata //Queue entry metadata
ServiceName string //Name of service used for this queue entry
ServiceColor int //Color of service used for this queue entry
Requester *discordgo.User
}
func (voice *Voice) GetNowPlayingEmbed(entry *QueueEntry) *discordgo.MessageEmbed {
return voice.getQueueEmbed(entry, 1)
}
func (voice *Voice) GetNowPlayingDurationEmbed(entry *QueueEntry) *discordgo.MessageEmbed {
return voice.getQueueEmbed(entry, 2)
}
func (voice *Voice) GetAddedEmbed(entry *QueueEntry) *discordgo.MessageEmbed {
return voice.getQueueEmbed(entry, 3)
}
func (voice *Voice) getQueueEmbed(entry *QueueEntry, embedType int) *discordgo.MessageEmbed {
track := fmt.Sprintf("[%s](%s)", entry.Metadata.Title, entry.Metadata.DisplayURL)
if len(entry.Metadata.Artists) > 0 {
track += fmt.Sprintf(" by [%s](%s)", entry.Metadata.Artists[0].Name, entry.Metadata.Artists[0].URL)
if len(entry.Metadata.Artists) > 1 {
track += fmt.Sprintf(" ft. [%s](%s)", entry.Metadata.Artists[1].Name, entry.Metadata.Artists[1].URL)
if len(entry.Metadata.Artists) > 2 {
for i, artist := range entry.Metadata.Artists[2:] {
if len(entry.Metadata.Artists) == 3 {
track += " and "
} else {
track += ", "
if (i + 3) == len(entry.Metadata.Artists) {
track += " and "
}
}
track += fmt.Sprintf("[%s](%s)", artist.Name, artist.URL)
}
}
}
}
duration := secondsToHuman(entry.Metadata.Duration)
embed := NewEmbed()
switch embedType {
case 1:
embed.AddField("Now Playing from "+entry.ServiceName, track)
embed.AddField("Duration", duration)
case 2:
embed.AddField("Now Playing from "+entry.ServiceName, track)
embed.AddField("Time", fmt.Sprintf("%s / %s", secondsToHuman(voice.NowPlaying.Position.Seconds()), duration))
case 3:
embed.AddField("Added to Queue from "+entry.ServiceName, track)
embed.AddField("Duration", duration)
}
embed.SetColor(entry.ServiceColor)
embed.SetThumbnail(entry.Metadata.ArtworkURL)
return embed.MessageEmbed
}
//VoiceNowPlaying contains data about the now playing queue entry
type VoiceNowPlaying struct {
Entry *QueueEntry //The underlying queue entry
Position time.Duration //The current position in the audio stream
}
// Metadata stores the metadata of a queue entry
type Metadata struct {
Artists []MetadataArtist //List of artists for this queue entry
Title string //Entry title
DisplayURL string //Entry page URL to display to users
StreamURL string //Entry URL for streaming
Duration float64 //Entry duration
ArtworkURL string //Entry artwork URL
ThumbnailURL string //Entry artwork thumbnail URL
}
// MetadataArtist stores the data about an artist
type MetadataArtist struct {
Name string //Artist name
URL string //Artist page URL
}