Passed
Push — master ( 2cfd2b...c1aba5 )
by Viktor
01:32
created

cmd/voicecommand.go   A

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 42
dl 0
loc 51
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C cmd.VoiceCommand 0 40 10
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
	switch ctx.Args[0] {
15
	case "join":
16
		ctx.MetricsCommand("voice", "join")
17
		if ctx.Sessions.GetByGuild(ctx.Guild.ID) != nil {
18
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_connected"))
19
			return
20
		}
21
		vc := ctx.GetVoiceChannel()
22
		if vc == nil {
23
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
24
			return
25
		}
26
		sess, err := ctx.Sessions.Join(ctx.Discord, ctx.Guild.ID, vc.ID, bot.JoinProperties{
27
			Muted:    false,
28
			Deafened: true,
29
		}, ctx.Guilds[ctx.Guild.ID].VoiceVolume)
30
		if err != nil {
31
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_error"))
32
			return
33
		}
34
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_joined"), sess.ChannelID))
35
	case "leave":
36
		ctx.MetricsCommand("voice", "leave")
37
		if sess == nil {
38
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
39
			return
40
		}
41
		ctx.Sessions.Leave(ctx.Discord, *sess)
42
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_left"), sess.ChannelID))
43
	case "volume":
44
		if len(ctx.Args) > 1 {
45
			vol, err := strconv.ParseFloat(ctx.Args[1], 32)
46
			if err != nil {
47
				ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("player_wrong_volume"))
48
				return
49
			} else {
0 ignored issues
show
introduced by
if block ends with a return statement, so drop this else and outdent its block
Loading history...
50
				ctx.Guilds[ctx.Guild.ID].VoiceVolume = float32(vol / 100)
51
				_ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"voicevolume": float32(vol / 100)}})
52
			}
53
		}
54
	}
55
}
56