-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
285 lines (274 loc) · 9.46 KB
/
command.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
package discord
import (
"fmt"
"strings"
"time"
"github.com/andersfylling/disgord"
disgordiface "github.com/aplombomb/boombot/discord/ifaces"
yt "github.com/aplombomb/boombot/youtube"
youtubeiface "github.com/aplombomb/boombot/youtube/ifaces"
)
// CommandEventClient contains the data for all command processing
type CommandEventClient struct {
data *disgord.Message
disgordClient disgordiface.DisgordClientAPI
ytps youtubeiface.YoutubePlaylistServiceAPI
ytss youtubeiface.YoutubeSearchAPI
queue disgordiface.QueueClientAPI
}
// NewCommandEventClient returns a pointer to a new CommandEventClient
func NewCommandEventClient(data *disgord.Message, disgordClient disgordiface.DisgordClientAPI, ytps youtubeiface.YoutubePlaylistServiceAPI, ytss youtubeiface.YoutubeSearchAPI, qc disgordiface.QueueClientAPI) *CommandEventClient {
return &CommandEventClient{
data: data,
disgordClient: disgordClient,
ytps: ytps,
ytss: ytss,
queue: qc,
}
}
// Delegate evaluates commands and sends them to be processed
func (cec *CommandEventClient) Delegate() {
mec := NewMessageEventClient(cec.data, cec.disgordClient)
cmd, args := cec.DisectCommand()
switch cmd {
case "help", "h", "?", "wtf":
hcc := NewHelpCommandClient(cec.data, cec.disgordClient)
hcc.SendHelpMsg()
case "next":
cec.queue.TriggerNext()
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
case "shuffle":
cec.queue.TriggerShuffle()
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
case "stop":
cec.queue.TriggerStop()
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
case "play":
// CHECK IF USER IS IN VOICE CHAT
if cec.queue.ReturnVoiceCacheEntry(cec.data.Author.ID) == 0 {
_, err := mec.SendEmbedMsgReply(disgord.Embed{
Title: "**Request Rejected**",
Description: "You need to be in a voice channel to make a request",
Timestamp: cec.data.Timestamp,
Footer: &disgord.EmbedFooter{
Text: fmt.Sprintf("%+v is not in a voice channel", cec.data.Author.Username),
},
Color: 0xeec400,
},
)
if err != nil {
fmt.Printf("\nError sending request rejected message: %+v\n", err)
}
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
return
}
// CHECK IF USER SUPPLIED ARGUMENT
if len(args) == 0 {
_, err := mec.SendEmbedMsgReply(disgord.Embed{
Title: "**Empty Request**",
Description: "You used the play command, but didn't provide an argument",
Timestamp: disgord.Time{Time: time.Now()},
Footer: &disgord.EmbedFooter{
Text: fmt.Sprintf("%+v only provided the play command", cec.data.Author.Username),
},
Color: 0xeec400,
})
if err != nil {
fmt.Printf("\nError sending embed message: %+v\n", err)
}
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
return
}
// PARSE ARGUMENT
parsedArgs, isURL, err := cec.ParseYoutubeArg(args)
if err != nil {
fmt.Println("\nError parsing play argument: ", err)
}
switch isURL {
case true:
fmt.Println("\nArgs: ", parsedArgs)
// Check for youtu.be links, extract the ID and append to standard URL for queue processing
if strings.Contains(parsedArgs[0], "youtu.be") {
if cec.queue.ReturnVoiceCacheEntry(cec.data.Author.ID) != 0 {
fields := strings.Split(parsedArgs[0], "youtu.be/")
// Format the argument to work with the queue's url processor
requestURL := fmt.Sprintf("https://www.youtube.com/watch?v=%+v", fields[1])
_, err := mec.SendEmbedMsgReply(disgord.Embed{
Title: "**Request Accepted**",
Description: "Your request has been submitted and will be played soon",
Timestamp: cec.data.Timestamp,
Footer: &disgord.EmbedFooter{
Text: fmt.Sprintf("%+v added a song to their queue", cec.data.Author.Username),
},
Color: 0xeec400,
},
)
if err != nil {
fmt.Printf("\nError sending request accepted message: %+v\n", err)
}
cec.queue.UpdateUserQueueState(cec.data.ChannelID, cec.data.Author.ID, requestURL)
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
return
}
}
// Check if URL is a playlist
if strings.Contains(parsedArgs[0], "list=") {
ytplc := yt.NewYoutubePlaylistClient(cec.ytps)
urls, err := ytplc.GetPlaylist(parsedArgs[0])
if err != nil {
fmt.Printf("\nError getting playlist URLs: %+v\n", err)
}
if len(urls) != 0 {
_, err := mec.SendEmbedMsgReply(disgord.Embed{
Title: "**Playlist Accepted**",
Description: fmt.Sprintf("%+v entries have been added", len(urls)),
Footer: &disgord.EmbedFooter{
Text: fmt.Sprintf("*%s's playlist added to queue*", cec.data.Author.Username),
},
Timestamp: cec.data.Timestamp,
Color: 0xeec400,
},
)
if err != nil {
fmt.Printf("\nError: %+v\n", err)
}
cec.queue.UpdateUserQueueStateBulk(cec.data.ChannelID, cec.data.Author.ID, urls)
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
return
}
} else {
if cec.queue.ReturnVoiceCacheEntry(cec.data.Author.ID) != 0 {
_, err := mec.SendEmbedMsgReply(disgord.Embed{
Title: "**Request Accepted**",
Description: "_Song added_",
Footer: &disgord.EmbedFooter{
Text: fmt.Sprintf("*%s's request added to queue*", cec.data.Author.Username),
},
Timestamp: cec.data.Timestamp,
Color: 0xeec400,
},
)
if err != nil {
fmt.Printf("\nError sending song accepted message: %+v\n", err)
}
cec.queue.UpdateUserQueueState(cec.data.ChannelID, cec.data.Author.ID, parsedArgs[0])
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
return
}
}
case false:
fmt.Sprintf("\n==============================\n%+v searched for '%+v'\n==============================", cec.data.Author.Username, cec.data.Content)
argString := strings.Join(parsedArgs, " ")
slc := cec.ytss.Q(argString)
resp, err := slc.Do()
if err != nil {
fmt.Println("\nError searching, ", err)
}
if len(resp.Items) != 0 {
fmt.Println("\nITEMS: ", resp.Items)
vidID := resp.Items[0].Id.VideoId
url := fmt.Sprintf("\nhttps://www.youtube.com/watch?v=%+v", vidID)
cec.queue.UpdateUserQueueState(cec.data.ChannelID, cec.data.Author.ID, url)
fmt.Println("\nURL from search: ", resp.Items[0].Snippet.Title)
avatarURL, err := cec.data.Author.AvatarURL(64, true)
if err != nil {
fmt.Println("\n", err)
}
_, err = mec.SendEmbedMsgReply(disgord.Embed{
Title: resp.Items[0].Snippet.Title,
Thumbnail: &disgord.EmbedThumbnail{
URL: avatarURL,
Height: 64,
Width: 64,
},
Image: &disgord.EmbedImage{
URL: resp.Items[0].Snippet.Thumbnails.Default.Url,
Height: 128,
Width: 128,
},
Footer: &disgord.EmbedFooter{
Text: fmt.Sprintf("Added by %s", cec.data.Author.Username),
},
Timestamp: cec.data.Timestamp,
Color: 0xeec400,
},
)
if err != nil {
fmt.Printf("\nError sending searched song message: %+v\n", err)
}
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
return
}
_, err = mec.SendEmbedMsgReply(disgord.Embed{
Title: "**No Results**",
Description: "No results found",
Footer: &disgord.EmbedFooter{
Text: fmt.Sprintf("%s's taste in music is too exotic", cec.data.Author.Username),
},
Timestamp: cec.data.Timestamp,
Color: 0xeec400,
},
)
if err != nil {
fmt.Printf("\nError sending no search results found messageg: %+v\n", err)
}
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
return
}
case "purge":
uq := cec.queue.ReturnUserQueue()
_, err := mec.SendEmbedMsgReply(disgord.Embed{
Title: "**Queue Purged**",
Description: fmt.Sprintf("%+v entries have been purged", len(uq)),
Footer: &disgord.EmbedFooter{
Text: fmt.Sprintf("Purged by %s", cec.data.Author.Username),
},
Timestamp: cec.data.Timestamp,
Color: 0xeec400,
},
)
if err != nil {
fmt.Printf("\nError sending purge message: %+v\n", err)
}
go deleteMessage(cec.data, 1*time.Second, cec.disgordClient)
delete(cec.queue.ReturnUserQueue(), cec.queue.ReturnNowPlayingID())
default:
uc := NewUnknownCommandClient(cec.data, cec.disgordClient)
uc.RespondToChannel()
}
}
// DisectCommand returns the used command and all extraneous arguments
func (cec *CommandEventClient) DisectCommand() (string, []string) {
var command string
var args []string
if len(cec.data.Content) > 0 {
command = strings.ToLower(strings.Fields(cec.data.Content)[0])
if len(cec.data.Content) > 1 {
args = strings.Fields(cec.data.Content)[1:]
}
}
return command, args
}
// ParseYoutubeArg handles argument parsing for the play command
func (cec *CommandEventClient) ParseYoutubeArg(args []string) ([]string, bool, error) {
parsedArg := []string{}
isURL := false
if strings.Contains(args[0], "https://www.youtu") != false {
isURL = true
parsedArg = args
return parsedArg, isURL, nil
}
parsedArg = args
return parsedArg, isURL, nil
}
// Print the ID and title of each result in a list as well as a name that
// identifies the list. For example, print the word section name "Videos"
// above a list of video search results, followed by the video ID and title
// of each matching video.
func printIDs(sectionName string, matches map[string]string) {
fmt.Printf("%v:\n", sectionName)
for id, title := range matches {
fmt.Printf("[%v] %v\n", id, title)
}
fmt.Printf("\n\n")
}