Completed
Push — master ( 95a2a0...997931 )
by Viktor
02:11
created

cmd.PlayerCommand   F

Complexity

Conditions 19

Size

Total Lines 52
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 41
nop 1
dl 0
loc 52
rs 0.5999
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like cmd.PlayerCommand 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
		ctx.MetricsCommand("player", "play")
19
		if sess == nil {
20
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
21
			return
22
		}
23
		if len(ctx.Args) > 1 {
24
			go sess.Player.Start(sess, ctx.Args[1], func(msg string) {
25
				ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg)
26
			}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
27
28
		}
29
	case "list":
30
		stations := ctx.DB.GetRadioStations()
31
		if len(stations) > 0 {
32
			var response string
33
			if len(stations) > 20 {
34
				for _, s := range stations[:20] {
35
					response += fmt.Sprintf("[%v] - %v", s.Key, s.Name)
36
				}
37
			} else {
38
				for _,s := range stations {
39
					response += fmt.Sprintf("[%v] - %v", s.Key, s.Name)
40
				}
41
			}
42
			ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_list") + "\n" + response)
43
		}
44
	case "station":
45
		if sess == nil {
46
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
47
			return
48
		}
49
		if len(ctx.Args) > 1 {
50
			station, err :=ctx.DB.GetRadioStationByKey(ctx.Args[1])
51
			if err != nil {
52
				ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found"))
53
				return
54
			}
55
			go sess.Player.Start(sess, station.URL, func(msg string) {
56
				ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg)
57
			}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
58
		}
59
	case "stop":
60
		ctx.MetricsCommand("player", "stop")
61
		sess.Stop()
62
	}
63
}
64