This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
242 lines (217 loc) · 4.12 KB
/
server.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"errors"
"sync"
"github.com/bwmarrin/discordgo"
)
const (
//Stopped is the state value of the bot when stopped
Stopped = iota
//Playing is the state value of the bot when playing
Playing
//Paused is the state value of the bot when paused
Paused
)
const (
NoRepeat = iota
Repeat
RepeatOne
)
//Server is a structure containing all information about a connection to a guild
type Server struct {
sync.Mutex
vc *discordgo.VoiceConnection
reloadChan chan bool
state int8
playing int
repeat int8
random bool
playlist []string
guildID string
}
//NewServer create a server structure
func NewServer(guildID string) (s *Server) {
s = &Server{}
s.guildID = guildID
return
}
func (s *Server) GuildID() string {
return s.guildID
}
func (s *Server) Vc() *discordgo.VoiceConnection {
s.Lock()
defer s.Unlock()
return s.vc
}
func (s *Server) Playlist() []string {
s.Lock()
defer s.Unlock()
return s.playlist
}
func (s *Server) Playing() int {
s.Lock()
defer s.Unlock()
return s.playing
}
func (s *Server) Repeat() int8 {
s.Lock()
defer s.Unlock()
return s.repeat
}
func (s *Server) Random() bool {
s.Lock()
defer s.Unlock()
return s.random
}
func (s *Server) State() int8 {
s.Lock()
defer s.Unlock()
return s.state
}
//PlaylistAdd add toAdd to the playlist if it exist in the music list
func (s *Server) PlaylistAdd(toAdd string) error {
for _, key := range soundList {
if key == toAdd {
s.Lock()
defer s.Unlock()
s.playlist = append(s.playlist, toAdd)
return nil
}
}
return errors.New("Music not found")
}
func (s *Server) PlaylistRemove(pos int) (string, error) {
if pos < 0 || pos > len(serverList) {
return "", errors.New(string(pos) + " is out of the playlist")
}
s.Lock()
defer s.Unlock()
name := s.playlist[pos]
s.playlist = append(s.playlist[:pos], s.playlist[pos+1:]...)
if s.playing == pos {
if s.state != Stopped {
s.state = Stopped
s.reloadChan <- true
}
s.playing = 0
} else if s.playing > pos {
s.playing--
}
return name, nil
}
func (s *Server) Previous() error {
s.Lock()
defer s.Unlock()
s.playing--
if s.playing < 0 {
s.playing = len(s.playlist) - 1
}
if s.state != Stopped {
s.state = Playing
s.reloadChan <- true
return nil
}
return nil
}
func (s *Server) Next() error {
s.Lock()
defer s.Unlock()
s.playing++
if s.playing >= len(s.playlist) {
s.playing = 0
}
if s.state != Stopped {
s.state = Playing
s.reloadChan <- true
return nil
}
return nil
}
func (s *Server) Select(i int) error {
s.Lock()
defer s.Unlock()
if i < 0 && i >= len(s.playlist) {
return errors.New("Invalid selection")
}
s.playing = i
if s.state != Stopped {
s.state = Playing
s.reloadChan <- true
}
return nil
}
func (s *Server) SetRepeat(repeat int8) {
s.Lock()
defer s.Unlock()
s.repeat = repeat
}
func (s *Server) SetRandom(random bool) {
s.Lock()
defer s.Unlock()
s.random = random
}
func (s *Server) Play() error {
s.Lock()
defer s.Unlock()
if s.state == Playing {
return nil
}
if len(s.playlist) <= 0 {
return errors.New("Empty playlist")
}
if s.vc == nil {
return errors.New("Not in a channel")
}
if s.state == Paused {
s.state = Playing
s.reloadChan <- false
} else if s.state == Stopped && s.reloadChan == nil {
s.state = Playing
s.reloadChan = make(chan bool, 0)
go s.player()
}
return nil
}
func (s *Server) Pause() error {
s.Lock()
defer s.Unlock()
if s.state == Stopped {
return errors.New("Cannot pause when stopped")
}
if s.state == Paused {
return errors.New("Already paused")
}
s.state = Paused
s.reloadChan <- false
return nil
}
func (s *Server) Stop() error {
s.Lock()
defer s.Unlock()
if s.state == Stopped {
return errors.New("Already stopped")
}
s.state = Stopped
s.reloadChan <- true
return nil
}
func (s *Server) SetVoiceConnection(vc *discordgo.VoiceConnection) {
s.Lock()
defer s.Unlock()
s.vc = vc
if s.state != Stopped && s.reloadChan != nil {
s.reloadChan <- false
}
}
func (s *Server) Disconnect() error {
s.Lock()
defer s.Unlock()
if s.vc != nil {
err := s.vc.Disconnect()
s.vc = nil
if err != nil {
return err
}
}
return nil
}