-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.go
198 lines (177 loc) · 4.34 KB
/
node.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
package gavalink
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/gorilla/websocket"
)
// NodeConfig configures a Lavalink Node
type NodeConfig struct {
// REST is the host where Lavalink's REST server runs
//
// This value is expected without a trailing slash, e.g. like
// `http://localhost:2333`
REST string
// WebSocket is the host where Lavalink's WebSocket server runs
//
// This value is expected without a trailing slash, e.g. like
// `http://localhost:8012`
WebSocket string
// Password is the expected Authorization header for the Node
Password string
}
// Node wraps a Lavalink Node
type Node struct {
config NodeConfig
load float32
manager *Lavalink
wsConn *websocket.Conn
}
func (node *Node) open() error {
header := http.Header{}
header.Set("Authorization", node.config.Password)
header.Set("Num-Shards", node.manager.shards)
header.Set("User-Id", node.manager.userID)
ws, resp, err := websocket.DefaultDialer.Dial(node.config.WebSocket, header)
if err != nil {
return err
}
vstr := resp.Header.Get("Lavalink-Api-Version")
v, err := strconv.Atoi(vstr)
if err != nil {
return err
}
if v < 3 {
return errInvalidVersion
}
node.wsConn = ws
go node.listen()
Log.Println("node", node.config.WebSocket, "opened")
return nil
}
func (node *Node) stop() {
// someone already stopped this
if node.wsConn == nil {
return
}
_ = node.wsConn.Close()
}
func (node *Node) listen() {
for {
msgType, msg, err := node.wsConn.ReadMessage()
if err != nil {
Log.Println(err)
// try to reconnect
oerr := node.open()
if oerr != nil {
Log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr)
node.manager.removeNode(node)
return
}
Log.Println("node", node.config.WebSocket, "reconnected")
return
}
err = node.onEvent(msgType, msg)
// TODO: better error handling?
if err != nil {
Log.Println(err)
}
}
}
func (node *Node) onEvent(msgType int, msg []byte) error {
if msgType != websocket.TextMessage {
return errUnknownPayload
}
m := message{}
err := json.Unmarshal(msg, &m)
if err != nil {
return err
}
switch m.Op {
case opPlayerUpdate:
player, err := node.manager.GetPlayer(m.GuildID)
if err != nil {
return err
}
player.time = m.State.Time
player.position = m.State.Position
case opEvent:
player, err := node.manager.GetPlayer(m.GuildID)
if err != nil {
return err
}
switch m.Type {
case eventTrackEnd:
player.track = ""
err = player.handler.OnTrackEnd(player, m.Track, m.Reason)
case eventTrackException:
err = player.handler.OnTrackException(player, m.Track, m.Reason)
case eventTrackStuck:
err = player.handler.OnTrackStuck(player, m.Track, m.ThresholdMs)
}
return err
case opStats:
node.load = m.StatCPU.Load
default:
return errUnknownPayload
}
return nil
}
// CreatePlayer creates an audio player on this node
func (node *Node) CreatePlayer(guildID string, sessionID string, event VoiceServerUpdate, handler EventHandler) (*Player, error) {
msg := message{
Op: opVoiceUpdate,
GuildID: guildID,
SessionID: sessionID,
Event: &event,
}
data, err := json.Marshal(msg)
if err != nil {
return nil, err
}
err = node.wsConn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return nil, err
}
player := &Player{
guildID: guildID,
manager: node.manager,
node: node,
handler: handler,
vol: 100,
}
node.manager.players[guildID] = player
return player, nil
}
// LoadTracks queries lavalink to return a Tracks object
//
// query should be a valid Lavaplayer query, including but not limited to:
// - A direct media URI
// - A direct Youtube /watch URI
// - A search query, prefixed with ytsearch: or scsearch:
//
// See the Lavaplayer Source Code for all valid options.
func (node *Node) LoadTracks(query string) (*Tracks, error) {
url := fmt.Sprintf("%s/loadtracks?identifier=%s", node.config.REST, query)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", node.config.Password)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
tracks := new(Tracks)
err = json.Unmarshal(data, &tracks)
if err != nil {
return nil, err
}
return tracks, nil
}