Passed
Push — master ( 72abbe...938fc0 )
by Viktor
01:49
created

cmd.playerList   F

Complexity

Conditions 14

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 24
nop 1
dl 0
loc 35
rs 3.6
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like cmd.playerList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 response string
63
		if len(stations) > 20 {
64
			for c, st := range category {
65
				response += c
66
				for _, s := range st[:20] {
67
					response += fmt.Sprintf("[%v] - %v\n - [%v]", s.Key, s.Name, s.Category)
68
				}
69
			}
70
71
		} else {
72
			for c, st := range category {
73
				response += c
74
				for _, s := range st {
75
					response += fmt.Sprintf("[%v] - %v\n - [%v]", s.Key, s.Name, s.Category)
76
				}
77
			}
78
		}
79
		ctx.ReplyEmbed(ctx.Loc("player"), response)
80
	} else {
81
		ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found"))
82
	}
83
}
84
85
func playerStation(sess *bot.Session, ctx *bot.Context) {
86
	if sess == nil {
87
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
88
		return
89
	}
90
	if len(ctx.Args) > 1 {
91
		station, err := ctx.DB.GetRadioStationByKey(ctx.Args[1])
92
		if err != nil {
93
			ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found"))
94
			return
95
		}
96
		go sess.Player.Start(sess, station.URL, func(msg string) {
97
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg)
98
		}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
99
	}
100
}
101