cmd.DebugCommand   F
last analyzed

Complexity

Conditions 14

Size

Total Lines 43
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

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

How to fix   Complexity   

Complexity

Complex classes like cmd.DebugCommand 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
	"strings"
6
	"time"
7
8
	"github.com/FlameInTheDark/dtbot/bot"
9
)
10
11
// DebugCommand special bot commands handler
12
func DebugCommand(ctx bot.Context) {
13
	if ctx.IsServerAdmin() {
14
		ctx.MetricsCommand("debug", "admin")
15
		if len(ctx.Args) == 0 {
16
			return
17
		}
18
		switch ctx.Args[0] {
19
		case "roles":
20
			var roles []string
21
			for _, val := range ctx.GetRoles().Roles {
22
				roles = append(roles, val.Name)
23
			}
24
			ctx.ReplyEmbedPM("Debug", strings.Join(roles, ", "))
25
		case "time":
26
			ctx.ReplyEmbedPM("Debug", time.Now().String())
27
		case "session":
28
			sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
29
			if sess != nil {
30
				ctx.ReplyEmbed("Debug", sess.ChannelID)
31
			} else {
32
				ctx.ReplyEmbed("Debug", "Session is nil")
33
			}
34
		case "voice":
35
			var resp string
36
			resp += fmt.Sprintf("Voice connections: %v\n", len(ctx.Discord.VoiceConnections))
37
			for i, c := range ctx.Discord.VoiceConnections {
38
				resp += i + " | G: " + c.GuildID + " | C: " + c.ChannelID + "\n"
39
			}
40
			ctx.ReplyEmbed("Debug", resp)
41
		case "leavevoice":
42
			if v, ok := ctx.Discord.VoiceConnections[ctx.Args[1]]; ok {
43
				err := v.Disconnect()
44
				if err != nil {
45
					ctx.ReplyEmbed("Debug", "Voice: "+err.Error())
46
				}
47
			} else {
48
				ctx.ReplyEmbed("Debug", "Voice connection not found")
49
			}
50
		case "volume":
51
			ctx.ReplyEmbed("Debug", fmt.Sprintf("Voice volume is %.2f", ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume))
52
		}
53
	} else {
54
		ctx.ReplyEmbedPM("Debug", "Not a Admin")
55
	}
56
}
57