Completed
Push — master ( 705db7...49bc4d )
by Viktor
02:19
created

cmd.voiceJoin   B

Complexity

Conditions 5

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nop 2
dl 0
loc 23
rs 8.9833
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
		if sess != nil {
44
			ctx.Sessions.Leave(ctx.Discord, *sess)
45
		}
46
		return
47
	}
48
	ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_joined"), sess.ChannelID))
49
}
50
51
func voiceLeave(sess *bot.Session, ctx *bot.Context) {
52
	ctx.MetricsCommand("voice", "leave")
53
	if sess == nil {
54
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
55
		return
56
	}
57
	ctx.Sessions.Leave(ctx.Discord, *sess)
58
	ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_left"), sess.ChannelID))
59
}
60
61
func voiceVolume(sess *bot.Session, ctx *bot.Context) {
62
	if len(ctx.Args) > 1 {
63
		vol, err := strconv.ParseFloat(ctx.Args[1], 32)
64
		if err != nil {
65
			ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("player_wrong_volume"))
66
			return
67
		}
68
		ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume = float32(vol * 0.01)
69
		_ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"voicevolume": float32(vol * 0.01)}})
70
		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...
71
		sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
72
		if sess != nil {
73
			sess.Volume = float32(vol * 0.01)
74
		}
75
	}
76
}
77