Completed
Push — master ( 95a2a0...997931 )
by Viktor
02:11
created

cmd/playercommand.go   A

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 45
dl 0
loc 61
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F cmd.PlayerCommand 0 52 19
1
package cmd
2
3
import (
4
	"fmt"
5
6
	"github.com/FlameInTheDark/dtbot/bot"
7
)
8
9
// PlayerCommand Player handler
10
func PlayerCommand(ctx bot.Context) {
11
	sess := ctx.Sessions.GetByGuild(ctx.Guild.ID)
12
	if len(ctx.Args) == 0 {
13
		ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_no_args"))
14
		return
15
	}
16
	switch ctx.Args[0] {
17
	case "play":
18
		ctx.MetricsCommand("player", "play")
19
		if sess == nil {
20
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
21
			return
22
		}
23
		if len(ctx.Args) > 1 {
24
			go sess.Player.Start(sess, ctx.Args[1], func(msg string) {
25
				ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg)
26
			}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
27
28
		}
29
	case "list":
30
		stations := ctx.DB.GetRadioStations()
31
		if len(stations) > 0 {
32
			var response string
33
			if len(stations) > 20 {
34
				for _, s := range stations[:20] {
35
					response += fmt.Sprintf("[%v] - %v", s.Key, s.Name)
36
				}
37
			} else {
38
				for _,s := range stations {
39
					response += fmt.Sprintf("[%v] - %v", s.Key, s.Name)
40
				}
41
			}
42
			ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_list") + "\n" + response)
43
		}
44
	case "station":
45
		if sess == nil {
46
			ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice"))
47
			return
48
		}
49
		if len(ctx.Args) > 1 {
50
			station, err :=ctx.DB.GetRadioStationByKey(ctx.Args[1])
51
			if err != nil {
52
				ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found"))
53
				return
54
			}
55
			go sess.Player.Start(sess, station.URL, func(msg string) {
56
				ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg)
57
			}, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume)
58
		}
59
	case "stop":
60
		ctx.MetricsCommand("player", "stop")
61
		sess.Stop()
62
	}
63
}
64