Passed
Push — master ( d1e92e...00093f )
by Viktor
01:36
created

cmd.playerCategories   A

Complexity

Conditions 5

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nop 1
dl 0
loc 12
rs 9.3333
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
	var stations []bot.RadioStation
49
	if len(ctx.Args) > 1 {
50
		stations = ctx.DB.GetRadioStations(ctx.Args[1])
51
	} else {
52
		stations = ctx.DB.GetRadioStations("")
53
	}
54
55
	var category = make(map[string][]*bot.RadioStation)
56
57
	for i, s := range stations {
58
		category[s.Category] = append(category[s.Category], &stations[i])
59
	}
60
61
	if len(stations) > 0 {
62
		var embed = bot.NewEmbed(ctx.Loc("player"))
63
		for c, st := range category {
64
			var response string
65
			if len(st) > 20 {
66
				for _, s := range st[:20] {
67
					response += fmt.Sprintf("[%v] - %v\n", s.Key, s.Name)
68
				}
69
			} else {
70
				for _, s := range st {
71
					response += fmt.Sprintf("[%v] - %v\n", s.Key, s.Name)
72
				}
73
			}
74
			embed.Field(c, response, false).Color(ctx.GuildConf().EmbedColor)
75
		}
76
		embed.Send(ctx)
77
	} else {
78
		ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found"))
79
	}
80
}
81
82
func playerCategories(ctx *bot.Context) {
83
	stations := ctx.DB.GetRadioStations("")
84
	var categories = make(map[string]bool)
85
	var reply = ctx.Loc("stations_categories")
86
	for _, st := range stations {
87
		categories[st.Category] = true
88
	}
89
90
	for c, _ := range categories {
0 ignored issues
show
introduced by
should omit 2nd value from range; this loop is equivalent to for c := range ...
Loading history...
91
		reply += "`" + c + "`, "
92
	}
93
	ctx.ReplyEmbed(ctx.Loc("player"), reply)
94
}
95
96
func playerStation(sess *bot.Session, ctx *bot.Context) {
97
	if sess == nil {
98
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
99
		return
100
	}
101
	if len(ctx.Args) > 1 {
102
		station, err := ctx.DB.GetRadioStationByKey(ctx.Args[1])
103
		if err != nil {
104
			ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found"))
105
			return
106
		}
107
		go sess.Player.Start(sess, station.URL, func(msg string) {
108
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg)
109
		}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
110
	}
111
}
112