1 | package cmd |
||
2 | |||
3 | import ( |
||
4 | "fmt" |
||
5 | |||
6 | "github.com/FlameInTheDark/dtbot/bot" |
||
7 | ) |
||
8 | |||
9 | // RadioCommand Player handler |
||
10 | func RadioCommand(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 | playerPlay(sess, &ctx) |
||
19 | case "list": |
||
20 | playerList(&ctx) |
||
21 | case "station": |
||
22 | playerStation(sess, &ctx) |
||
23 | case "genres": |
||
24 | playerCategories(&ctx) |
||
25 | case "stop": |
||
26 | ctx.MetricsCommand("radio", "stop") |
||
27 | if sess == nil { |
||
28 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice")) |
||
29 | return |
||
30 | } |
||
31 | sess.Stop() |
||
32 | } |
||
33 | } |
||
34 | |||
35 | func playerPlay(sess *bot.Session, ctx *bot.Context) { |
||
36 | ctx.MetricsCommand("radio", "play") |
||
37 | if sess == nil { |
||
38 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice")) |
||
39 | return |
||
40 | } |
||
41 | if ctx.Arg(1) == "attachment" { |
||
42 | go sess.Player.Start(sess, ctx.Message.Attachments[0].URL, func(msg string) { |
||
43 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg) |
||
44 | }, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume) |
||
45 | } else if len(ctx.Args) > 1 { |
||
46 | go sess.Player.Start(sess, ctx.Args[1], func(msg string) { |
||
47 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg) |
||
48 | }, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume) |
||
49 | } |
||
50 | } |
||
51 | |||
52 | func playerList(ctx *bot.Context) { |
||
53 | ctx.MetricsCommand("radio", "list") |
||
54 | var stations []bot.RadioStation |
||
55 | if len(ctx.Args) > 1 { |
||
56 | stations = ctx.DB.GetRadioStations(ctx.Args[1]) |
||
57 | } else { |
||
58 | stations = ctx.DB.GetRadioStations("") |
||
59 | } |
||
60 | |||
61 | var category = make(map[string][]*bot.RadioStation) |
||
62 | |||
63 | for i, s := range stations { |
||
64 | category[s.Category] = append(category[s.Category], &stations[i]) |
||
65 | } |
||
66 | |||
67 | if len(stations) > 0 { |
||
68 | var embed = bot.NewEmbed(ctx.Loc("player")) |
||
69 | for c, st := range category { |
||
70 | var response string |
||
71 | if len(st) > 20 { |
||
72 | for _, s := range st[:20] { |
||
73 | response += fmt.Sprintf("[%v] - %v\n", s.Key, s.Name) |
||
74 | } |
||
75 | } else { |
||
76 | for _, s := range st { |
||
77 | response += fmt.Sprintf("[%v] - %v\n", s.Key, s.Name) |
||
78 | } |
||
79 | } |
||
80 | embed.Field(c, response, false).Color(ctx.GuildConf().EmbedColor) |
||
81 | } |
||
82 | embed.Send(ctx) |
||
83 | } else { |
||
84 | ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found")) |
||
85 | } |
||
86 | } |
||
87 | |||
88 | func playerCategories(ctx *bot.Context) { |
||
89 | ctx.MetricsCommand("radio", "categories") |
||
90 | stations := ctx.DB.GetRadioStations("") |
||
91 | var categories = make(map[string]bool) |
||
92 | var reply = ctx.Loc("stations_categories") |
||
93 | for _, st := range stations { |
||
94 | categories[st.Category] = true |
||
95 | } |
||
96 | |||
97 | for c, _ := range categories { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
98 | reply += "`" + c + "`, " |
||
99 | } |
||
100 | ctx.ReplyEmbed(ctx.Loc("player"), reply) |
||
101 | } |
||
102 | |||
103 | func playerStation(sess *bot.Session, ctx *bot.Context) { |
||
104 | ctx.MetricsCommand("radio", "station") |
||
105 | if sess == nil { |
||
106 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice")) |
||
107 | return |
||
108 | } |
||
109 | if len(ctx.Args) > 1 { |
||
110 | station, err := ctx.DB.GetRadioStationByKey(ctx.Args[1]) |
||
111 | if err != nil { |
||
112 | ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found")) |
||
113 | return |
||
114 | } |
||
115 | go sess.Player.Start(sess, station.URL, func(msg string) { |
||
116 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg) |
||
117 | }, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume) |
||
118 | } |
||
119 | } |
||
120 |