Issues (115)

cmd/voicecommand.go (1 issue)

Severity
1
package cmd
2
3
import (
4
	"fmt"
5
	"github.com/FlameInTheDark/dtbot/bot"
6
	"github.com/globalsign/mgo/bson"
7
	"log"
8
	"strconv"
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
		voiceJoin(sess, &ctx)
20
	case "leave":
21
		voiceLeave(sess, &ctx)
22
	case "volume":
23
		voiceVolume(sess, &ctx)
24
	}
25
}
26
27
func voiceJoin(sess *bot.Session, ctx *bot.Context) {
28
	ctx.MetricsCommand("voice", "join")
29
	log.Println("used")
30
	if ctx.Sessions.GetByGuild(ctx.Guild.ID) != nil {
31
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_connected"))
32
		return
33
	}
34
	log.Println("guild founded")
35
	vc := ctx.GetVoiceChannel()
36
	if vc == nil {
37
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
38
		return
39
	}
40
	log.Println("get voice channel")
41
	s := ctx.Sessions.GetByGuild(ctx.Guild.ID)
42
	if s != nil {
43
		if !s.IsOk() {
44
			log.Println("removed failed session")
45
			ctx.Sessions.Leave(ctx.Discord, *sess)
46
		}
47
	}
48
49
	sess, err := ctx.Sessions.Join(ctx.Discord, ctx.Guild.ID, vc.ID, bot.JoinProperties{
50
		Muted:    false,
51
		Deafened: true,
52
	}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
53
	if err != nil {
54
		log.Println("Voice join error: ", err)
55
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_error"))
56
		if sess != nil {
57
			ctx.Sessions.Leave(ctx.Discord, *sess)
58
		}
59
		return
60
	}
61
	log.Println("join session")
62
	ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_joined"), sess.ChannelID))
63
}
64
65
func voiceLeave(sess *bot.Session, ctx *bot.Context) {
66
	ctx.MetricsCommand("voice", "leave")
67
	if sess == nil {
68
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice"))
69
		return
70
	}
71
	ctx.Sessions.Leave(ctx.Discord, *sess)
72
	ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_left"), sess.ChannelID))
73
}
74
75
func voiceVolume(sess *bot.Session, ctx *bot.Context) {
76
	if len(ctx.Args) > 1 {
77
		vol, err := strconv.ParseFloat(ctx.Args[1], 32)
78
		if err != nil {
79
			ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("player_wrong_volume"))
80
			return
81
		}
82
		ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume = float32(vol * 0.01)
83
		_ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"voicevolume": float32(vol * 0.01)}})
84
		ctx.ReplyEmbed(ctx.Loc("player"), fmt.Sprintf(ctx.Loc("player_volume_changed"), ctx.Args[1]))
0 ignored issues
show
can't check non-constant format in call to Sprintf
Loading history...
85
		sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
86
		if sess != nil {
87
			sess.Volume = float32(vol * 0.01)
88
		}
89
	}
90
}
91