forked from AzuraCast/azurabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
203 lines (160 loc) · 5.66 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"github.com/bwmarrin/discordgo"
"log"
"strings"
"time"
"strconv"
"math"
)
func (b *Bot) HelpReporter(m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "sent command 'help'")
help := "```go\n`Standard Commands List`\n```\n" +
"**`" + b.config.DiscordPrefix + "help`** -> show help commands.\n" +
"**`" + b.config.DiscordPrefix + "join`** -> join a voice channel.\n" +
"**`" + b.config.DiscordPrefix + "leave`** -> leave a voice channel.\n" +
"**`" + b.config.DiscordPrefix + "play [station_short_name]`** -> play the specified station.\n" +
"**`" + b.config.DiscordPrefix + "stop`** -> stop the player.\n" +
"**`" + b.config.DiscordPrefix + "np`** -> show what's now playing.\n" +
"**`" + b.config.DiscordPrefix + "vol [1-100]`** -> set the player volume.\n" +
"```go\n`Owner Commands List`\n```\n" +
"**`" + b.config.DiscordPrefix + "ignore`** -> ignore commands of a channel.\n" +
"**`" + b.config.DiscordPrefix + "unignore`** -> unignore commands of a channel.\n"
b.ChMessageSend(m.ChannelID, help)
}
func (b *Bot) JoinReporter(v *VoiceInstance, m *discordgo.MessageCreate, s *discordgo.Session) {
log.Println("INFO:", m.Author.Username, "sent command 'join'")
voiceChannelID := b.SearchVoiceChannel(m.Author.ID)
if voiceChannelID == "" {
log.Println("ERROR: Voice channel id not found.")
b.ChMessageSend(m.ChannelID, "<@"+m.Author.ID+">, join the voice channel you would like the bot to join first.")
return
}
if v != nil {
log.Println("INFO: Voice instance already created.")
} else {
guildID := b.SearchGuild(m.ChannelID)
b.mutex.Lock()
v = new(VoiceInstance)
b.voiceInstances[guildID] = v
v.guildID = guildID
v.session = s
b.mutex.Unlock()
}
var err error
v.voice, err = b.dg.ChannelVoiceJoin(v.guildID, voiceChannelID, false, true)
if err != nil {
v.Stop()
log.Println("ERROR: Error joining voice channel: ", err)
return
}
v.voice.Speaking(false)
log.Println("INFO: New Voice instance created")
b.ChMessageSend(m.ChannelID, "Voice channel joined. Ready to play the radio!")
}
func (b *Bot) LeaveReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "sent command 'leave'")
if v == nil {
log.Println("INFO: The bot is not in a voice channel.")
return
}
v.Stop()
time.Sleep(200 * time.Millisecond)
v.voice.Disconnect()
log.Println("INFO: Voice channel destroyed")
b.mutex.Lock()
delete(b.voiceInstances, v.guildID)
b.mutex.Unlock()
b.dg.UpdateStatus(0, b.config.DiscordStatus)
b.ChMessageSend(m.ChannelID, "Voice channel left.")
}
func (b *Bot) PlayReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "sent command 'play'")
if v == nil {
log.Println("INFO: The bot is not in a voice channel.")
b.ChMessageSend(m.ChannelID, "Join a voice channel before playing music.")
return
}
if len(strings.Fields(m.Content)) > 1 {
err := b.azuracast.GetNowPlaying(v, strings.Fields(m.Content)[1])
if err != nil {
b.ChMessageSend(m.ChannelID, "Error: Could not retrieve station information.")
log.Println("ERROR: AzuraCast API call returned", err.Error())
return
}
} else if v.station == nil {
b.ChMessageSend(m.ChannelID, "You must specify a station ID number or shortcode after the command the first time you play.")
return
}
radio := PkgRadio{
data: v.station.ListenURL,
v: v,
}
go func() {
b.radioSignal <- radio
}()
b.ChMessageSend(m.ChannelID, "Starting playback of "+v.station.Name+"!")
}
// StopReporter
func (b *Bot) StopReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "sent command 'stop'")
if v == nil {
log.Println("INFO: The bot is not joined in a voice channel")
return
}
v.Stop()
b.dg.UpdateStatus(0, b.config.DiscordStatus)
log.Println("INFO: The bot stopped playing audio")
b.ChMessageSend(m.ChannelID, "Stopping radio playback...")
}
// Return Now Playing information
func (b *Bot) NowPlayingReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "sent command 'np'")
if v == nil {
log.Println("INFO: The bot is not in a voice channel.")
b.ChMessageSend(m.ChannelID, "The bot is not currently active.")
return
}
err := b.azuracast.UpdateNowPlaying(v)
if err != nil {
b.ChMessageSend(m.ChannelID, "Error: Could not retrieve now playing information.")
log.Println("ERROR: AzuraCast API call returned", err.Error())
return
}
b.ChMessageSend(m.ChannelID, "**Now Playing on "+v.station.Name+":** "+v.np.Title+" by "+v.np.Artist)
}
func (b *Bot) VolumeReporter(v *VoiceInstance, m *discordgo.MessageCreate) {
log.Println("INFO:", m.Author.Username, "sent command 'vol'")
if v == nil {
log.Println("INFO: The bot is not in a voice channel.")
b.ChMessageSend(m.ChannelID, "Join a voice channel before setting the volume.")
return
}
if len(strings.Fields(m.Content)) > 1 {
volPercent, err := strconv.Atoi(strings.Fields(m.Content)[1])
if volPercent > 100 {
volPercent = 100
} else if volPercent < 1 {
volPercent = 1
}
if err != nil {
b.ChMessageSend(m.ChannelID, "Error: Could not set volume.")
log.Println("ERROR: Volume parse error", err.Error())
}
v.volume = int(math.Ceil(float64(volPercent) * 255.0/100.0))
log.Println("INFO: Volume set to ", v.volume, " from user input ", volPercent)
b.ChMessageSend(m.ChannelID, "Volume updated to "+strconv.Itoa(volPercent)+"%.")
} else {
v.volume = 0
b.ChMessageSend(m.ChannelID, "Volume reset to default.")
}
if v.is_playing && v.station != nil {
radio := PkgRadio{
data: v.station.ListenURL,
v: v,
}
go func() {
b.radioSignal <- radio
}()
}
}