-
Notifications
You must be signed in to change notification settings - Fork 1
/
play.go
161 lines (147 loc) · 5.76 KB
/
play.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
package commands
import (
"fmt"
"html"
"log"
"net/url"
"os"
"strconv"
"strings"
"github.com/bottleneckco/discord-radio/ctx"
"github.com/bottleneckco/discord-radio/models"
"github.com/andersfylling/disgord"
"google.golang.org/api/youtube/v3"
)
var tempSearchResultsCache = make(map[disgord.Snowflake][]*youtube.SearchResult)
func play(s disgord.Session, m *disgord.MessageCreate) {
guildSession := safeGetGuildSession(m.Message.GuildID)
var isNotInVoiceChannel = guildSession.VoiceConnection == nil
if isNotInVoiceChannel {
voiceChannelInit(s, m)
}
var b strings.Builder
// Terminate if user has a pending search, forgets and uses /play again.
if items, ok := tempSearchResultsCache[m.Message.Author.ID]; ok {
b.WriteString(fmt.Sprintf("%s, you **already have** a pending search query:\n", m.Message.Author.Mention()))
for index, item := range items {
b.WriteString(fmt.Sprintf("`%d.` **%s** `%s`\n", index+1, item.Snippet.Title, item.Snippet.ChannelTitle))
}
b.WriteString("\nreply with a single number, no command needed. Anything else will cancel the search.")
sentMsg, err := s.SendMsg(ctx.Ctx, m.Message.ChannelID, b.String())
if err == nil {
go deleteMessageDelayed(s, sentMsg)
}
return
}
if url, err := url.ParseRequestURI(m.Message.Content); err == nil {
// URL
var videoIDs []string
if len(url.Query().Get("list")) != 0 {
// Playlist URL
playlistResponse, err := youtubeService.PlaylistItems.List("contentDetails").PlaylistId(url.Query().Get("list")).MaxResults(50).Do()
if err != nil {
s.SendMsg(ctx.Ctx, m.Message.ChannelID, fmt.Sprintf("Error occurred: %s", err))
return
}
for _, playlistItem := range playlistResponse.Items {
videoIDs = append(videoIDs, playlistItem.ContentDetails.VideoId)
}
} else if len(url.Query().Get("v")) != 0 {
// Plain video URL
videoIDs = append(videoIDs, url.Query().Get("v"))
} else {
s.SendMsg(ctx.Ctx, m.Message.ChannelID, "Unsupported URL")
return
}
youtubeListings, err := youtubeService.Videos.List("snippet").Id(strings.Join(videoIDs, ",")).Do()
if err != nil {
s.SendMsg(ctx.Ctx, m.Message.ChannelID, fmt.Sprintf("Error occurred: %s", err))
return
}
guildSession.RWMutex.Lock()
for _, youtubeListing := range youtubeListings.Items {
guildSession.Queue = append(guildSession.Queue, models.QueueItem{
Title: youtubeListing.Snippet.Title,
ChannelTitle: youtubeListing.Snippet.ChannelTitle,
Author: m.Message.Author.Username,
VideoID: youtubeListing.Id,
Thumbnail: youtubeListing.Snippet.Thumbnails.Default.Url,
})
}
guildSession.RWMutex.Unlock()
s.SendMsg(ctx.Ctx, m.Message.ChannelID, fmt.Sprintf("%s enqueued %d videos`\n", m.Message.Author.Mention(), len(videoIDs)))
} else {
maxResults, _ := strconv.ParseInt(os.Getenv("BOT_NUM_RESULTS"), 10, 64)
call := youtubeService.Search.List("snippet").Q(m.Message.Content).MaxResults(maxResults)
response, err := call.Do()
if err != nil {
s.SendMsg(ctx.Ctx, m.Message.ChannelID, fmt.Sprintf("Error occurred: %s", err))
return
}
log.Printf("[PLAY] Found %d results", len(response.Items))
b.WriteString(fmt.Sprintf("%s, here are your search results:\n", m.Message.Author.Mention()))
for index, item := range response.Items {
b.WriteString(fmt.Sprintf("`%d.` **%s** `%s`\n", index+1, html.UnescapeString(item.Snippet.Title), item.Snippet.ChannelTitle))
}
b.WriteString("\nreply with a single number, no command needed. Anything else will cancel the search.")
// Cache for response
tempSearchResultsCache[m.Message.Author.ID] = response.Items
var awaitFuncCtrl = &disgord.Ctrl{}
s.On(disgord.EvtMessageCreate, func(ss disgord.Session, mm *disgord.MessageCreate) {
if m.Message.Author.ID != mm.Message.Author.ID {
return
}
if strings.HasPrefix(mm.Message.Content, os.Getenv("BOT_COMMAND_PREFIX")) {
return
}
choice, err := strconv.ParseInt(mm.Message.Content, 10, 64)
if err == nil && (choice-1 < 0 || choice-1 >= int64(len(tempSearchResultsCache[mm.Message.Author.ID]))) {
// Valid integer but out of bounds
ss.SendMsg(ctx.Ctx, mm.Message.ChannelID, fmt.Sprintf("%s Invalid choice, try again", mm.Message.Author.Mention()))
return
} else if err == nil {
chosenItem := tempSearchResultsCache[mm.Message.Author.ID][choice-1]
guildSession.RWMutex.Lock()
guildSession.Queue = append(guildSession.Queue, models.QueueItem{
Title: html.UnescapeString(chosenItem.Snippet.Title),
ChannelTitle: chosenItem.Snippet.ChannelTitle,
Author: mm.Message.Author.Username,
VideoID: chosenItem.Id.VideoId,
Thumbnail: chosenItem.Snippet.Thumbnails.Default.Url,
})
guildSession.RWMutex.Unlock()
avatarURL, _ := m.Message.Author.AvatarURL(32, false)
ss.SendMsg(ctx.Ctx, mm.Message.ChannelID, &disgord.CreateMessageParams{
Embed: &disgord.Embed{
Author: &disgord.EmbedAuthor{
Name: "Added to queue",
IconURL: avatarURL,
},
Title: html.UnescapeString(chosenItem.Snippet.Title),
Thumbnail: &disgord.EmbedThumbnail{
URL: chosenItem.Snippet.Thumbnails.Default.Url,
},
URL: fmt.Sprintf("https://www.youtube.com/watch?v=%s", chosenItem.Id.VideoId),
Fields: []*disgord.EmbedField{
&disgord.EmbedField{
Name: "Channel",
Value: chosenItem.Snippet.ChannelTitle,
},
},
},
})
} else {
ss.SendMsg(ctx.Ctx, mm.Message.ChannelID, fmt.Sprintf("%s Search cancelled", mm.Message.Author.Mention()))
}
delete(tempSearchResultsCache, mm.Message.Author.ID)
awaitFuncCtrl.Runs = 0
if isNotInVoiceChannel {
go guildSession.Loop()
}
}, awaitFuncCtrl)
}
sentMsg, err := s.SendMsg(ctx.Ctx, m.Message.ChannelID, b.String())
if err == nil {
go deleteMessageDelayed(s, sentMsg)
}
}