Passed
Push — master ( a32b8b...b8f7a7 )
by Viktor
02:03 queued 11s
created

cmd.youtubeSkip   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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