-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
92 lines (74 loc) · 1.82 KB
/
manager.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
package main
import (
"github.com/TheTipo01/roberto/queue"
"github.com/bwmarrin/discordgo"
"sync/atomic"
)
// NewServer creates a new server manager
func NewServer(guildID string) *Server {
return &Server{
skip: make(chan struct{}),
guildID: guildID,
customCommands: make(map[string]string),
queue: queue.NewQueue(),
started: atomic.Bool{},
clear: atomic.Bool{},
}
}
// AddSong adds a song to the queue
func (m *Server) AddSong(priority bool, el ...queue.Element) {
if priority {
m.queue.AddElementsPriority(el...)
} else {
m.queue.AddElements(el...)
}
if m.started.CompareAndSwap(false, true) {
go m.play()
}
}
func (m *Server) play() {
msg := make(chan *discordgo.Message)
for el := m.queue.GetFirstElement(); el != nil && !m.clear.Load(); el = m.queue.GetFirstElement() {
// Send "Now playing" message
go func() {
msg <- sendEmbed(s, NewEmbed().SetTitle(s.State.User.Username).
AddField(el.Type, el.Content).
SetColor(0x7289DA).MessageEmbed, el.TextChannel)
}()
if el.BeforePlay != nil {
el.BeforePlay()
}
playSound(m.guildID, el)
if el.AfterPlay != nil {
el.AfterPlay()
}
// Delete it after it has been played
go func() {
if message := <-msg; message != nil {
_ = s.ChannelMessageDelete(message.ChannelID, message.ID)
}
}()
m.queue.RemoveFirstElement()
}
m.started.Store(false)
go quitVC(m.guildID)
}
// IsPlaying returns whether the bot is playing
func (m *Server) IsPlaying() bool {
return m.started.Load() && !m.queue.IsEmpty()
}
// Clear clears the queue
func (m *Server) Clear() {
if m.IsPlaying() {
m.clear.Store(true)
m.skip <- struct{}{}
m.clear.Store(false)
q := m.queue.GetAllQueue()
m.queue.Clear()
for _, el := range q {
if el.Closer != nil {
_ = el.Closer.Close()
}
}
}
}