Passed
Push — master ( 016bd9...7ba52e )
by Viktor
01:42
created

cmd.TwitchCommand   F

Complexity

Conditions 14

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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

How to fix   Complexity   

Complexity

Complex classes like cmd.TwitchCommand 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
	"github.com/FlameInTheDark/dtbot/bot"
6
)
7
8
// TwitchCommand manipulates twitch announcer
9
func TwitchCommand(ctx bot.Context) {
10
	ctx.MetricsCommand("twitch")
11
	if ctx.GetRoles().ExistsName("bot.admin") || ctx.IsAdmin() {
12
		if len(ctx.Args) == 0 {
13
			return
14
		}
15
		switch ctx.Args[0] {
16
		case "add":
17
			if len(ctx.Args) > 1 {
18
				username, err := ctx.Twitch.AddStreamer(ctx.Guild.ID, ctx.Message.ChannelID, ctx.Args[1])
19
				if err != nil {
20
					ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_add_error"))
21
				} else {
22
					ctx.ReplyEmbed("Twitch", fmt.Sprintf(ctx.Loc("twitch_added"), username))
0 ignored issues
show
introduced by
can't check non-constant format in call to Sprintf
Loading history...
23
				}
24
			}
25
		case "remove":
26
			if len(ctx.Args) > 1 {
27
				err := ctx.Twitch.RemoveStreamer(ctx.Args[1], ctx.Guild.ID)
28
				if err != nil {
29
					ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_remove_error"))
30
				} else {
31
					ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_removed"))
32
				}
33
			}
34
		case "count":
35
			if ctx.IsAdmin() {
36
				count := 0
37
				for _,g := range ctx.Twitch.Guilds {
38
					count += len(g.Streams)
39
				}
40
				ctx.ReplyEmbed("Twitch", fmt.Sprintf("Streamers: %v", count))
41
			}
42
		}
43
	} else {
44
		ctx.ReplyEmbed("Twitch", ctx.Loc("admin_require"))
45
	}
46
}