Passed
Push — master ( a32b8b...b8f7a7 )
by Viktor
02:03 queued 11s
created

cmd.VoiceCommand   A

Complexity

Conditions 5

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nop 1
dl 0
loc 12
rs 9.3333
c 0
b 0
f 0
1
package cmd
2
3
import (
4
	"fmt"
5
	"github.com/FlameInTheDark/dtbot/bot"
6
	"github.com/globalsign/mgo/bson"
7
	"strconv"
8
)
9
10
// VoiceCommand voice handler
11
func VoiceCommand(ctx bot.Context) {
12
	sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
13
	if len(ctx.Args) < 1 {
14
		return
15
	}
16
	switch ctx.Args[0] {
17
	case "join":
18
		voiceJoin(sess, &ctx)
19
	case "leave":
20
		voiceLeave(sess, &ctx)
21
	case "volume":
22
		voiceVolume(sess, &ctx)
23
	}
24
}
25
26
func voiceJoin(sess *bot.Session, ctx *bot.Context) {
27
	ctx.MetricsCommand("voice", "join")
28
	if ctx.Sessions.GetByGuild(ctx.Guild.ID) != nil {
29
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_connected"))
30
		return
31
	}
32
	vc := ctx.GetVoiceChannel()
33
	if vc == nil {
34
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
35
		return
36
	}
37
	sess, err := ctx.Sessions.Join(ctx.Discord, ctx.Guild.ID, vc.ID, bot.JoinProperties{
38
		Muted:    false,
39
		Deafened: true,
40
	}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
41
	if err != nil {
42
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_error"))
43
		return
44
	}
45
	ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_joined"), sess.ChannelID))
46
}
47
48
func voiceLeave(sess *bot.Session, ctx *bot.Context) {
49
	ctx.MetricsCommand("voice", "leave")
50
	if sess == nil {
51
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
52
		return
53
	}
54
	ctx.Sessions.Leave(ctx.Discord, *sess)
55
	ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_left"), sess.ChannelID))
56
}
57
58
func voiceVolume(sess *bot.Session, ctx *bot.Context) {
59
	if len(ctx.Args) > 1 {
60
		vol, err := strconv.ParseFloat(ctx.Args[1], 32)
61
		if err != nil {
62
			ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("player_wrong_volume"))
63
			return
64
		}
65
		ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume = float32(vol * 0.01)
66
		_ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"voicevolume": float32(vol * 0.01)}})
67
		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...
68
		sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
69
		if sess != nil {
70
			sess.Volume = float32(vol * 0.01)
71
		}
72
	}
73
}