This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
commands.go
399 lines (378 loc) · 14 KB
/
commands.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"strconv"
"strings"
"time"
)
// HelpReporter
func HelpReporter(m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'help'")
help := "```go\n`Standard Commands List`\n```\n" +
"**`" + o.DiscordPrefix + "help`** or **`" + o.DiscordPrefix + "h`** -> show help commands.\n" +
"**`" + o.DiscordPrefix + "join`** or **`" + o.DiscordPrefix + "j`** -> the bot join in to voice channel.\n" +
"**`" + o.DiscordPrefix + "leave`** or **`" + o.DiscordPrefix + "l`** -> the bot leave the voice channel.\n" +
"**`" + o.DiscordPrefix + "play`** -> play and add a one song in the queue.\n" +
"**`" + o.DiscordPrefix + "radio`** -> play a URL radio.\n" +
"**`" + o.DiscordPrefix + "stop`** -> stop the player and remove the queue.\n" +
"**`" + o.DiscordPrefix + "skip`** -> skip the actual song and play the next song of the queue.\n" +
"**`" + o.DiscordPrefix + "pause`** -> pause the player.\n" +
"**`" + o.DiscordPrefix + "resume`** -> resume the player.\n" +
"**`" + o.DiscordPrefix + "time`** -> show the time remaining of song.\n" +
"**`" + o.DiscordPrefix + "queue list`** -> show the list of song in the queue.\n" +
"**`" + o.DiscordPrefix + "queue remove `** -> remove a song of queue indexed for a ***number***, an ***@User*** or the ***last*** song, i.e. ***" + o.DiscordPrefix + "queue remove 2***\n" +
"**`" + o.DiscordPrefix + "queue clean`** -> clean all queue.\n" +
"**`" + o.DiscordPrefix + "youtube`** -> search from youtube.\n\n" +
"```go\n`Owner Commands List`\n```\n" +
"**`" + o.DiscordPrefix + "ignore`** -> ignore commands of a channel.\n" +
"**`" + o.DiscordPrefix + "unignore`** -> unignore commands of a channel.\n"
ChMessageSend(m.ChannelID, help)
//ChMessageSendEmbed(m.ChannelID, "Help", help)
}
// JoinReporter
func JoinReporter(v *VoiceInstance, m *discordgo.MessageCreate, s *discordgo.Session) {
log.Println("INFO:", m.Author.Username, "send 'join'")
voiceChannelID := SearchVoiceChannel(m.Author.ID)
if voiceChannelID == "" {
log.Println("ERROR: Voice channel id not found.")
ChMessageSend(m.ChannelID, "[**Music**] <@"+m.Author.ID+"> You need to join a voice channel!")
return
}
if v != nil {
log.Println("INFO: Voice Instance already created.")
} else {
guildID := SearchGuild(m.ChannelID)
// create new voice instance
mutex.Lock()
v = new(VoiceInstance)
voiceInstances[guildID] = v
v.guildID = guildID
v.session = s
mutex.Unlock()
//v.InitVoice()
}
var err error
v.voice, err = dg.ChannelVoiceJoin(v.guildID, voiceChannelID, false, false)
if err != nil {
v.Stop()
log.Println("ERROR: Error to join in a voice channel: ", err)
return
}
v.voice.Speaking(false)
log.Println("INFO: New Voice Instance created")
ChMessageSend(m.ChannelID, "[**Music**] I've joined a voice channel!")
}
// LeaveReporter
func LeaveReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'leave'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
return
}
v.Stop()
time.Sleep(200 * time.Millisecond)
v.voice.Disconnect()
log.Println("INFO: Voice channel destroyed")
mutex.Lock()
delete(voiceInstances, v.guildID)
mutex.Unlock()
dg.UpdateStatus(0, o.DiscordStatus)
ChMessageSend(m.ChannelID, "[**Music**] I left the voice channel!")
}
// PlayReporter
func PlayReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'play'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
ChMessageSend(m.ChannelID, "[**Music**] I need join in a voice channel!")
return
}
if len(strings.Fields(m.Content)) < 2 {
ChMessageSend(m.ChannelID, "[**Music**] You need specify a name or URL.")
return
}
// if the user is not a voice channel not accept the command
voiceChannelID := SearchVoiceChannel(m.Author.ID)
if v.voice.ChannelID != voiceChannelID {
ChMessageSend(m.ChannelID, "[**Music**] <@"+m.Author.ID+"> You need to join in my voice channel for send play!")
return
}
// send play my_song_youtube
command := strings.SplitAfter(m.Content, strings.Fields(m.Content)[0])
query := strings.TrimSpace(command[1])
song, err := YoutubeFind(query, v, m)
if err != nil || song.data.ID == "" {
log.Println("ERROR: Youtube search: ", err)
ChMessageSend(m.ChannelID, "[**Music**] I can't found this song!")
return
}
//***`"+ song.data.User +"`***
ChMessageSend(m.ChannelID, "[**Music**] **`"+song.data.User+"`** has added , **`"+
song.data.Title+"`** to the queue. **`("+song.data.Duration+")` `["+strconv.Itoa(len(v.queue))+"]`**")
go func() {
songSignal <- song
}()
}
// ReadioReporter
func RadioReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'radio'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
ChMessageSend(m.ChannelID, "[**Music**] I need join in a voice channel!")
return
}
if len(strings.Fields(m.Content)) < 2 {
ChMessageSend(m.ChannelID, "[**Music**] You need to specify a url!")
return
}
radio := PkgRadio{"", v}
radio.data = strings.Fields(m.Content)[1]
go func() {
radioSignal <- radio
}()
ChMessageSend(m.ChannelID, "[**Music**] **`"+m.Author.Username+"`** I'm playing a radio now!")
}
// StopReporter
func StopReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'stop'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
ChMessageSend(m.ChannelID, "[**Music**] I need join in a voice channel!")
return
}
voiceChannelID := SearchVoiceChannel(m.Author.ID)
if v.voice.ChannelID != voiceChannelID {
ChMessageSend(m.ChannelID, "[**Music**] <@"+m.Author.ID+"> You need to join in my voice channel for send stop!")
return
}
v.Stop()
dg.UpdateStatus(0, o.DiscordStatus)
log.Println("INFO: The bot stop play audio")
ChMessageSend(m.ChannelID, "[**Music**] I'm stoped now!")
}
// PauseReporter
func PauseReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'pause'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
return
}
if !v.speaking {
ChMessageSend(m.ChannelID, "[**Music**] I'm not playing nothing!")
return
}
if !v.pause {
v.Pause()
ChMessageSend(m.ChannelID, "[**Music**] I'm `PAUSED` now!")
}
}
// ResumeReporter
func ResumeReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'resume'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
ChMessageSend(m.ChannelID, "[**Music**] I need join in a voice channel!")
return
}
if !v.speaking {
ChMessageSend(m.ChannelID, "[**Music**] I'm not playing nothing!")
return
}
if v.pause {
v.Resume()
ChMessageSend(m.ChannelID, "[**Music**] I'm `RESUMED` now!")
}
}
// TimeReporter
func TimeReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'time'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
ChMessageSend(m.ChannelID, "[**Music**] I need join in a voice channel!")
return
}
if v.speaking == true && v.radioFlag == false {
var duration TimeDuration
var message string
if v.stream != nil {
d := v.stream.PlaybackPosition()
duration.Second = int(d.Seconds())
t := AddTimeDuration(duration)
if len(strings.Split(v.nowPlaying.Duration, ":")) == 2 {
message = fmt.Sprintf("[**Music**] The playback time of **`%s`** is **`(%d:%02d)`** of **`(%s)`** - **`%s`**",
v.nowPlaying.Title, t.Minute, t.Second, v.nowPlaying.Duration, v.nowPlaying.User)
} else if len(strings.Split(v.nowPlaying.Duration, ":")) == 3 {
message = fmt.Sprintf("[**Music**] The playback time of **`%s`** is **`(%d:%02d:%02d)`** of **`(%s)`** - **`%s`**",
v.nowPlaying.Title, t.Hour, t.Minute, t.Second, v.nowPlaying.Duration, v.nowPlaying.User)
} else if len(strings.Split(v.nowPlaying.Duration, ":")) == 4 {
message = fmt.Sprintf("[**Music**] The playback time of **`%s`** is **`(%d:%02d:%02d:%02d)`** of **`(%s)`** - **`%s`**",
v.nowPlaying.Title, t.Day, t.Hour, t.Minute, t.Second, v.nowPlaying.Duration, v.nowPlaying.User)
}
ChMessageSend(m.ChannelID, message)
return
}
}
}
// QueueReporter
func QueueReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'queue'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
ChMessageSend(m.ChannelID, "[**Music**] I need join in a voice channel!")
return
}
if len(v.queue) == 0 {
log.Println("INFO: The queue is empty.")
ChMessageSend(m.ChannelID, "[**Music**] The song queue is empty!")
return
}
if len(strings.Fields(m.Content)) < 2 {
ChMessageSend(m.ChannelID, "[**Music**] You need specify a `sub-command`!")
return
}
if strings.HasSuffix(m.Content, "queue clean") {
log.Println("INFO:", m.Author.Username, "send 'queue clean'")
v.QueueClean()
ChMessageSend(m.ChannelID, "[**Music**] Queue cleaned")
return
}
if strings.Contains(m.Content, "queue remove") {
voiceChannelID := SearchVoiceChannel(m.Author.ID)
if v.voice.ChannelID != voiceChannelID {
ChMessageSend(m.ChannelID, "[**Music**] <@"+m.Author.ID+"> You need to join in my voice channel to remove of queue!")
return
}
log.Println("INFO:", m.Author.Username, "send 'queue remove'")
if len(strings.Fields(m.Content)) != 3 {
ChMessageSend(m.ChannelID, "[**Music**] You need define a `number`, an `@User` or `last` command")
return
}
// is a number?
if k, err := strconv.Atoi(strings.Fields(m.Content)[2]); err == nil {
if k < len(v.queue) && k != 0 {
song := v.queue[k]
v.QueueRemoveIndex(k)
ChMessageSend(m.ChannelID, "[**Music**] The songs **`["+strconv.Itoa(k)+"]` - `"+song.Title+"`** was removed of queue!")
return
} else {
ChMessageSend(m.ChannelID, "[**Music**] The songs **`["+strconv.Itoa(k)+"]`** not exist!")
return
}
}
// is an user?
if len(m.Mentions) != 0 {
v.QueueRemoveUser(m.Mentions[0].Username)
ChMessageSend(m.ChannelID, "[**Music**] The songs indexed by **`"+m.Mentions[0].Username+"`** was removed of queue!")
return
}
// the `last` song?
if strings.HasSuffix(m.Content, "queue remove last") {
log.Println("INFO:", m.Author.Username, "send 'queue remove last'")
if len(v.queue) > 1 {
v.QueueRemoveLast()
ChMessageSend(m.ChannelID, "[**Music**] The last songs indexed was removed of queue!")
return
}
ChMessageSend(m.ChannelID, "[**Music**] No more songs in the queue!")
return
}
}
// queue list
if strings.HasSuffix(m.Content, "queue list") {
log.Println("INFO:", m.Author.Username, "send 'queue list'")
message := "[**Music**] My songs are:\n\nNow Playing: **`" + v.nowPlaying.Title + "` - `(" +
v.nowPlaying.Duration + ")` - " + v.nowPlaying.User + "**\n"
queue := v.queue[1:]
if len(queue) != 0 {
var duration TimeDuration
for i, q := range queue {
message = message + "\n**`[" + strconv.Itoa(i+1) + "]` - `" + q.Title + "` - `(" + q.Duration + ")` - " + q.User + "**"
d := strings.Split(q.Duration, ":")
switch len(d) {
case 2:
// mm:ss
ss, _ := strconv.Atoi(d[1])
duration.Second = duration.Second + ss
mm, _ := strconv.Atoi(d[0])
duration.Minute = duration.Minute + mm
case 3:
// hh:mm:ss
ss, _ := strconv.Atoi(d[2])
duration.Second = duration.Second + ss
mm, _ := strconv.Atoi(d[1])
duration.Minute = duration.Minute + mm
hh, _ := strconv.Atoi(d[0])
duration.Hour = duration.Hour + hh
case 4:
// dd:hh:mm:ss
ss, _ := strconv.Atoi(d[3])
duration.Second = duration.Second + ss
mm, _ := strconv.Atoi(d[2])
duration.Minute = duration.Minute + mm
hh, _ := strconv.Atoi(d[1])
duration.Hour = duration.Hour + hh
dd, _ := strconv.Atoi(d[0])
duration.Day = duration.Day + dd
}
}
t := AddTimeDuration(duration)
message = message + "\n\nThe total duration: **`" +
strconv.Itoa(t.Day) + "d` `" +
strconv.Itoa(t.Hour) + "h` `" +
strconv.Itoa(t.Minute) + "m` `" +
strconv.Itoa(t.Second) + "s`**"
}
ChMessageSend(m.ChannelID, message)
return
}
}
// SkipReporter
func SkipReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'skip'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
ChMessageSend(m.ChannelID, "[**Music**] I need join in a voice channel!")
return
}
if len(v.queue) == 0 {
log.Println("INFO: The queue is empty.")
ChMessageSend(m.ChannelID, "[**Music**] Currently there's no music playing, add some? ;)")
return
}
if v.Skip() {
ChMessageSend(m.ChannelID, "[**Music**] I'm `PAUSED`, please `resume` first.")
}
}
// YoutubeReporter
func YoutubeReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'youtube'")
command := strings.SplitAfter(m.Content, strings.Fields(m.Content)[0])
query := strings.TrimSpace(command[1])
song, err := YoutubeFind(query, v, m)
if err != nil || song.data.ID == "" {
log.Println("ERROR: Youtube search: ", err)
ChMessageSend(m.ChannelID, "[**Music**] I can't found this song!")
return
}
ChMessageSendHold(m.ChannelID, "[**Music**] **`"+song.data.User+"`**, Youtube URL: https://www.youtube.com/watch?v="+song.data.VidID)
}
// Not used for now
// StatusReporter
func StatusReporter(m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'status'")
if len(strings.Fields(m.Content)) < 2 {
ChMessageSend(m.ChannelID, "[**Music**] You need to specify a status!")
return
}
command := strings.SplitAfter(m.Content, "status")
status := strings.TrimSpace(command[1])
dg.UpdateStatus(0, status)
ChMessageSend(m.ChannelID, "[**Music**] Status: `"+status+"`")
}
// StatusCleanReporter
func StatusCleanReporter(m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "send 'statusclean'")
dg.UpdateStatus(0, "")
}