Passed
Push — master ( d25772...3fe379 )
by Viktor
01:54
created

cmd.VoiceCommand   D

Complexity

Conditions 12

Size

Total Lines 47
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 41
nop 1
dl 0
loc 47
rs 4.8
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like cmd.VoiceCommand 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/globalsign/mgo/bson"
6
	"strconv"
7
8
	"github.com/FlameInTheDark/dtbot/bot"
9
)
10
11
// VoiceCommand voice handler
12
func VoiceCommand(ctx bot.Context) {
13
	sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
14
	if len(ctx.Args) < 1 {
15
		return
16
	}
17
	switch ctx.Args[0] {
18
	case "join":
19
		ctx.MetricsCommand("voice", "join")
20
		if ctx.Sessions.GetByGuild(ctx.Guild.ID) != nil {
21
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_connected"))
22
			return
23
		}
24
		vc := ctx.GetVoiceChannel()
25
		if vc == nil {
26
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
27
			return
28
		}
29
		sess, err := ctx.Sessions.Join(ctx.Discord, ctx.Guild.ID, vc.ID, bot.JoinProperties{
30
			Muted:    false,
31
			Deafened: true,
32
		}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
33
		if err != nil {
34
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_error"))
35
			return
36
		}
37
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_joined"), sess.ChannelID))
38
	case "leave":
39
		ctx.MetricsCommand("voice", "leave")
40
		if sess == nil {
41
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
42
			return
43
		}
44
		ctx.Sessions.Leave(ctx.Discord, *sess)
45
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_left"), sess.ChannelID))
46
	case "volume":
47
		if len(ctx.Args) > 1 {
48
			vol, err := strconv.ParseFloat(ctx.Args[1], 32)
49
			if err != nil {
50
				ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("player_wrong_volume"))
51
				return
52
			}
53
			ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume = float32(vol * 0.01)
54
			_ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"voicevolume": float32(vol * 0.01)}})
55
			ctx.ReplyEmbed(ctx.Loc("player"), fmt.Sprintf(ctx.Loc("player_volume_changed"), ctx.Args[1]))
0 ignored issues
show
introduced by
can't check non-constant format in call to Sprintf
Loading history...
56
			sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
57
			if sess != nil {
58
				sess.Volume = float32(vol * 0.01)
59
			}
60
		}
61
	}
62
}
63