Passed
Push — master ( f7aab9...4f517e )
by Viktor
01:53
created

cmd.TwitchCommand   C

Complexity

Conditions 10

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 22
nop 1
dl 0
loc 28
rs 5.9999
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
	"github.com/FlameInTheDark/dtbot/bot"
5
)
6
7
// TwitchCommand manipulates twitch announcer
8
func TwitchCommand(ctx bot.Context) {
9
	ctx.MetricsCommand("bot")
10
	if ctx.GetRoles().ExistsName("bot.admin") || ctx.IsAdmin() {
11
		if len(ctx.Args) == 0 {
12
			return
13
		}
14
		switch ctx.Args[0] {
15
		case "add":
16
			if len(ctx.Args) > 1 {
17
				err := ctx.Twitch.AddStreamer(ctx.Guild.ID, ctx.Message.ChannelID, ctx.Args[1])
18
				if err != nil {
19
					ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_add_error"))
20
				} else {
21
					ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_added"))
22
				}
23
			}
24
		case "remove":
25
			if len(ctx.Args) > 1 {
26
				err := ctx.Twitch.RemoveStreamer(ctx.Guild.ID, ctx.Args[1])
27
				if err != nil {
28
					ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_remove_error"))
29
				} else {
30
					ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_removed"))
31
				}
32
			}
33
		}
34
	} else {
35
		ctx.ReplyEmbed("Twitch", ctx.Loc("admin_require"))
36
	}
37
}