1 | package cmd |
||
2 | |||
3 | import ( |
||
4 | "fmt" |
||
5 | "strings" |
||
6 | |||
7 | "github.com/FlameInTheDark/dtbot/bot" |
||
8 | "github.com/bwmarrin/discordgo" |
||
9 | ) |
||
10 | |||
11 | // YoutubeCommand youtube handler |
||
12 | func YoutubeCommand(ctx bot.Context) { |
||
13 | sess := ctx.Sessions.GetByGuild(ctx.Guild.ID) |
||
14 | if len(ctx.Args) == 0 { |
||
15 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_no_args")) |
||
16 | return |
||
17 | } |
||
18 | switch ctx.Args[0] { |
||
19 | case "play": |
||
20 | youtubePlay(sess, &ctx) |
||
21 | case "stop": |
||
22 | youtubeStop(sess, &ctx) |
||
23 | case "skip": |
||
24 | youtubeSkip(sess, &ctx) |
||
25 | case "add": |
||
26 | youtubeAdd(sess, &ctx) |
||
27 | case "list": |
||
28 | youtubeList(sess, &ctx) |
||
29 | case "clear": |
||
30 | youtubeClear(sess, &ctx) |
||
31 | } |
||
32 | } |
||
33 | |||
34 | func youtubePlay(sess *bot.Session, ctx *bot.Context) { |
||
35 | ctx.MetricsCommand("youtube_command", "play") |
||
36 | if sess == nil { |
||
37 | vc := ctx.GetVoiceChannel() |
||
38 | if vc == nil { |
||
39 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice")) |
||
40 | return |
||
41 | } |
||
42 | nsess, serr := ctx.Sessions.Join(ctx.Discord, ctx.Guild.ID, vc.ID, bot.JoinProperties{ |
||
43 | Muted: false, |
||
44 | Deafened: true, |
||
45 | }, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume) |
||
46 | if serr != nil { |
||
47 | //ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_error") + " : " + serr.Error()) |
||
48 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("player error: %v", serr.Error())) |
||
49 | return |
||
50 | } |
||
51 | sess = nsess |
||
52 | } |
||
53 | queue := sess.Queue |
||
54 | if !queue.HasNext() { |
||
55 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_queue_is_empty")) |
||
56 | return |
||
57 | } |
||
58 | msg := ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_starting")) |
||
59 | shortPlay(ctx, sess, msg) |
||
60 | } |
||
61 | |||
62 | func youtubeStop(sess *bot.Session, ctx *bot.Context) { |
||
63 | ctx.MetricsCommand("youtube_command", "stop") |
||
64 | if sess == nil { |
||
65 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("player_not_in_voice")) |
||
66 | return |
||
67 | } |
||
68 | if sess.Queue.HasNext() { |
||
69 | sess.Queue.Clear() |
||
70 | } |
||
71 | sess.Stop() |
||
72 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_stopped")) |
||
73 | } |
||
74 | |||
75 | func youtubeSkip(sess *bot.Session, ctx *bot.Context) { |
||
76 | ctx.MetricsCommand("youtube_command", "skip") |
||
77 | if sess == nil { |
||
78 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("player_not_in_voice")) |
||
79 | return |
||
80 | } |
||
81 | sess.Stop() |
||
82 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_skipped")) |
||
83 | } |
||
84 | |||
85 | func youtubeAdd(sess *bot.Session, ctx *bot.Context) { |
||
86 | ctx.MetricsCommand("youtube_command", "add") |
||
87 | newargs := ctx.Args[1:] |
||
88 | if len(newargs) == 0 { |
||
89 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_args_missing")) |
||
90 | return |
||
91 | } |
||
92 | if sess == nil { |
||
93 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("player_not_in_voice")) |
||
94 | return |
||
95 | } |
||
96 | msg := ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_adding_song")) |
||
97 | for _, arg := range newargs { |
||
98 | t, inp, err := ctx.Youtube.Get(arg) |
||
99 | if err != nil { |
||
100 | //ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("error"), true) |
||
101 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting input: %v", err.Error())) |
||
102 | return |
||
103 | } |
||
104 | |||
105 | switch t { |
||
106 | case bot.ERROR_TYPE: |
||
107 | //ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("error"), true) |
||
108 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error type: %v", t)) |
||
109 | return |
||
110 | case bot.VIDEO_TYPE: |
||
111 | { |
||
112 | video, err := ctx.Youtube.Video(*inp) |
||
113 | if err != nil { |
||
114 | //ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("error"), true) |
||
115 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video1 (VIDEO_TYPE): %v", err.Error())) |
||
116 | return |
||
117 | } |
||
118 | song := bot.NewSong(video.Media, video.Title, arg) |
||
119 | sess.Queue.Add(song) |
||
120 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), fmt.Sprintf(ctx.Loc("youtube_added_format"), song.Title), true) |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
121 | break |
||
122 | } |
||
123 | case bot.PLAYLIST_TYPE: |
||
124 | { |
||
125 | videos, err := ctx.Youtube.Playlist(*inp) |
||
126 | if err != nil { |
||
127 | //ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("error"), true) |
||
128 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting playlist: %v", err.Error())) |
||
129 | return |
||
130 | } |
||
131 | for _, v := range *videos { |
||
132 | id := v.Id |
||
133 | _, i, err := ctx.Youtube.Get(id) |
||
134 | if err != nil { |
||
135 | //ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("error"), true) |
||
136 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video2: %v", err.Error())) |
||
137 | continue |
||
138 | } |
||
139 | video, err := ctx.Youtube.Video(*i) |
||
140 | if err != nil { |
||
141 | //ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("error"), true) |
||
142 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video3: %v", err.Error())) |
||
143 | return |
||
144 | } |
||
145 | song := bot.NewSong(video.Media, video.Title, arg) |
||
146 | sess.Queue.Add(song) |
||
147 | } |
||
148 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_added"), true) |
||
149 | break |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | func youtubeList(sess *bot.Session, ctx *bot.Context) { |
||
156 | ctx.MetricsCommand("youtube_command", "list") |
||
157 | if sess == nil { |
||
158 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("player_not_in_voice")) |
||
159 | return |
||
160 | } |
||
161 | if !sess.Queue.HasNext() { |
||
162 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_queue_is_empty")) |
||
163 | return |
||
164 | } |
||
165 | var songsNames []string |
||
166 | var count = 0 |
||
167 | var countMore = 0 |
||
168 | for _, val := range sess.Queue.Get() { |
||
169 | count++ |
||
170 | if count <= 10 { |
||
171 | songsNames = append(songsNames, fmt.Sprintf("[%v]: %v", count, val.Title)) |
||
172 | } else { |
||
173 | countMore++ |
||
174 | } |
||
175 | } |
||
176 | if countMore > 0 { |
||
177 | songsNames = append(songsNames, fmt.Sprintf(ctx.Loc("youtube_list_more_format"), countMore)) |
||
0 ignored issues
–
show
|
|||
178 | } |
||
179 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), fmt.Sprintf(ctx.Loc("youtube_list_format"), strings.Join(songsNames, "\n"))) |
||
0 ignored issues
–
show
|
|||
180 | } |
||
181 | |||
182 | func youtubeClear(sess *bot.Session, ctx *bot.Context) { |
||
183 | ctx.MetricsCommand("youtube_command", "clear") |
||
184 | if sess == nil { |
||
185 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("player_not_in_voice")) |
||
186 | return |
||
187 | } |
||
188 | if !sess.Queue.HasNext() { |
||
189 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_queue_is_empty")) |
||
190 | return |
||
191 | } |
||
192 | sess.Queue.Clear() |
||
193 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_queue_cleared")) |
||
194 | } |
||
195 | |||
196 | func shortPlay(ctx *bot.Context, sess *bot.Session, msg *discordgo.Message) { |
||
197 | queue := sess.Queue |
||
198 | if !queue.HasNext() { |
||
199 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_queue_is_empty")) |
||
200 | return |
||
201 | } |
||
202 | queue.Start(sess, func(relp string) { |
||
203 | switch relp { |
||
204 | case "stop": |
||
205 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_stopped"), true) |
||
206 | case "finish": |
||
207 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_finished"), true) |
||
208 | default: |
||
209 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), fmt.Sprintf("%v: %v", ctx.Loc("youtube_now_playing"), relp), true) |
||
210 | } |
||
211 | }) |
||
212 | } |
||
213 | |||
214 | // YoutubeShortCommand handle short command for playing song from youtube |
||
215 | func YoutubeShortCommand(ctx bot.Context) { |
||
216 | ctx.MetricsCommand("youtube_command", "short") |
||
217 | sess := ctx.Sessions.GetByGuild(ctx.Guild.ID) |
||
218 | newargs := ctx.Args |
||
219 | if len(newargs) == 0 { |
||
220 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_args_missing")) |
||
221 | return |
||
222 | } |
||
223 | if sess == nil { |
||
224 | vc := ctx.GetVoiceChannel() |
||
225 | if vc == nil { |
||
226 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice")) |
||
227 | return |
||
228 | } |
||
229 | nsess, serr := ctx.Sessions.Join(ctx.Discord, ctx.Guild.ID, vc.ID, bot.JoinProperties{ |
||
230 | Muted: false, |
||
231 | Deafened: true, |
||
232 | }, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume) |
||
233 | sess = nsess |
||
234 | if serr != nil { |
||
235 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("session error: %v", serr.Error())) |
||
236 | //ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_error")) |
||
237 | return |
||
238 | } |
||
239 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_joined"), sess.ChannelID)) |
||
240 | } |
||
241 | msg := ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_adding_song")) |
||
242 | for _, arg := range newargs { |
||
243 | t, inp, err := ctx.Youtube.Get(arg) |
||
244 | |||
245 | if err != nil { |
||
246 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting input: %v", err.Error())) |
||
247 | return |
||
248 | } |
||
249 | |||
250 | switch t { |
||
251 | case bot.ERROR_TYPE: |
||
252 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error type: %v", t)) |
||
253 | fmt.Println("error type", t) |
||
254 | return |
||
255 | case bot.VIDEO_TYPE: |
||
256 | { |
||
257 | video, err := ctx.Youtube.Video(*inp) |
||
258 | if err != nil { |
||
259 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video1: %v", err.Error())) |
||
260 | return |
||
261 | } |
||
262 | song := bot.NewSong(video.Media, video.Title, arg) |
||
263 | sess.Queue.Add(song) |
||
264 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), fmt.Sprintf(ctx.Loc("youtube_added_format"), song.Title), true) |
||
0 ignored issues
–
show
|
|||
265 | shortPlay(&ctx, sess, msg) |
||
266 | break |
||
267 | } |
||
268 | case bot.PLAYLIST_TYPE: |
||
269 | { |
||
270 | videos, err := ctx.Youtube.Playlist(*inp) |
||
271 | if err != nil { |
||
272 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting playlist: %v", err.Error())) |
||
273 | return |
||
274 | } |
||
275 | var isPlaying bool |
||
276 | for _, v := range *videos { |
||
277 | id := v.Id |
||
278 | _, i, err := ctx.Youtube.Get(id) |
||
279 | if err != nil { |
||
280 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video2: %v", err.Error())) |
||
281 | continue |
||
282 | } |
||
283 | video, err := ctx.Youtube.Video(*i) |
||
284 | if err != nil { |
||
285 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video3: %v", err.Error())) |
||
286 | return |
||
287 | } |
||
288 | song := bot.NewSong(video.Media, video.Title, arg) |
||
289 | sess.Queue.Add(song) |
||
290 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), fmt.Sprintf(ctx.Loc("youtube_added_format"), song.Title), true) |
||
0 ignored issues
–
show
|
|||
291 | if !isPlaying { |
||
292 | shortPlay(&ctx, sess, msg) |
||
293 | isPlaying = true |
||
294 | } |
||
295 | } |
||
296 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_added"), true) |
||
297 | break |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 |