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

cmd.PlayerCommand   B

Complexity

Conditions 7

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nop 1
dl 0
loc 20
rs 8
c 0
b 0
f 0
1
package cmd
2
3
import (
4
	"fmt"
5
6
	"github.com/FlameInTheDark/dtbot/bot"
7
)
8
9
// PlayerCommand Player handler
10
func PlayerCommand(ctx bot.Context) {
11
	sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
12
	if len(ctx.Args) == 0 {
13
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_no_args"))
14
		return
15
	}
16
	switch ctx.Args[0] {
17
	case "play":
18
		playerPlay(sess, &ctx)
19
	case "list":
20
		playerList(&ctx)
21
	case "station":
22
		playerStation(sess, &ctx)
23
	case "stop":
24
		ctx.MetricsCommand("player", "stop")
25
		if sess == nil {
26
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
27
			return
28
		}
29
		sess.Stop()
30
	}
31
}
32
33
func playerPlay(sess *bot.Session, ctx *bot.Context) {
34
	ctx.MetricsCommand("player", "play")
35
	if sess == nil {
36
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
37
		return
38
	}
39
	if len(ctx.Args) > 1 {
40
		go sess.Player.Start(sess, ctx.Args[1], func(msg string) {
41
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg)
42
		}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
43
44
	}
45
}
46
47
func playerList(ctx *bot.Context) {
48
	stations := ctx.DB.GetRadioStations()
49
	if len(stations) > 0 {
50
		var response string
51
		if len(stations) > 20 {
52
			for _, s := range stations[:20] {
53
				response += fmt.Sprintf("[%v] - %v\n", s.Key, s.Name)
54
			}
55
		} else {
56
			for _, s := range stations {
57
				response += fmt.Sprintf("[%v] - %v\n", s.Key, s.Name)
58
			}
59
		}
60
		ctx.ReplyEmbed(ctx.Loc("player"), response)
61
	} else {
62
		ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found"))
63
	}
64
}
65
66
func playerStation(sess *bot.Session, ctx *bot.Context) {
67
	if sess == nil {
68
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
69
		return
70
	}
71
	if len(ctx.Args) > 1 {
72
		station, err := ctx.DB.GetRadioStationByKey(ctx.Args[1])
73
		if err != nil {
74
			ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found"))
75
			return
76
		}
77
		go sess.Player.Start(sess, station.URL, func(msg string) {
78
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg)
79
		}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
80
	}
81
}