Passed
Push — master ( 938fc0...799ac2 )
by Viktor
01:30
created

cmd.playerList   D

Complexity

Conditions 12

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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