-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
94 lines (75 loc) · 1.85 KB
/
manager.go
File metadata and controls
94 lines (75 loc) · 1.85 KB
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
package main
import (
"sync/atomic"
"github.com/TheTipo01/roberto/queue"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2"
)
// NewServer creates a new server manager
func NewServer(guildID snowflake.ID) *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 *discord.Message)
for el := m.queue.GetFirstElement(); el != nil && !m.clear.Load(); el = m.queue.GetFirstElement() {
// Send "Now playing" message
go func() {
msg <- sendEmbed(s, discord.NewEmbed().WithTitle(BotName).
AddField(el.Type, el.Content, false).
WithColor(0x7289DA), 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.Rest.DeleteMessage(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()
}
}
}
}