gavalink/node.go

235 lines
4.8 KiB
Go
Raw Permalink Normal View History

2018-08-24 01:24:20 +00:00
package gavalink
import (
"encoding/json"
"fmt"
2019-01-13 23:37:38 +00:00
"github.com/valyala/fastjson"
2018-08-24 01:24:20 +00:00
"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
2019-10-13 01:13:23 +00:00
client *http.Client
2018-08-24 01:24:20 +00:00
}
func (node *Node) open() error {
header := http.Header{}
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
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)
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
if err != nil {
return err
}
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
vstr := resp.Header.Get("Lavalink-Major-Version")
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
v, err := strconv.Atoi(vstr)
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
if err != nil {
return err
}
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
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
}
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
_ = node.wsConn.Close()
}
func (node *Node) listen() {
2019-01-13 23:37:38 +00:00
var p fastjson.Parser
2018-08-24 01:24:20 +00:00
for {
msgType, msg, err := node.wsConn.ReadMessage()
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
if err != nil {
Log.Println(err)
// try to reconnect
oerr := node.open()
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
if oerr != nil {
Log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr)
node.manager.removeNode(node)
return
}
2019-10-13 01:13:23 +00:00
2018-08-24 01:24:20 +00:00
Log.Println("node", node.config.WebSocket, "reconnected")
return
}
2019-01-13 23:37:38 +00:00
if msgType != websocket.TextMessage {
continue
}
v, err := p.ParseBytes(msg)
2018-08-24 01:24:20 +00:00
if err != nil {
2019-01-13 23:37:38 +00:00
continue
2018-08-24 01:24:20 +00:00
}
2019-01-13 23:37:38 +00:00
node.onEvent(v)
2018-08-24 01:24:20 +00:00
}
2019-01-13 23:37:38 +00:00
}
2018-08-24 01:24:20 +00:00
2019-01-13 23:37:38 +00:00
func (node *Node) onEvent(v *fastjson.Value) error {
op := jsonStringValue(v, "op")
2018-08-24 01:24:20 +00:00
2019-01-13 23:37:38 +00:00
switch op {
2018-08-24 01:24:20 +00:00
case opPlayerUpdate:
2019-01-13 23:37:38 +00:00
player, err := node.manager.GetPlayer(jsonStringValue(v, "guildId"))
2018-08-24 01:24:20 +00:00
if err != nil {
return err
}
2019-01-13 23:37:38 +00:00
player.time = v.GetInt("state", "time")
player.position = v.GetInt("state", "position")
2018-08-24 01:24:20 +00:00
case opEvent:
2019-01-13 23:37:38 +00:00
player, err := node.manager.GetPlayer(jsonStringValue(v, "guildId"))
2018-08-24 01:24:20 +00:00
if err != nil {
return err
}
2019-01-13 23:37:38 +00:00
track := jsonStringValue(v, "track")
switch jsonStringValue(v, "type") {
2018-08-24 01:24:20 +00:00
case eventTrackEnd:
player.track = ""
2019-01-13 23:37:38 +00:00
err = player.handler.OnTrackEnd(player, track, jsonStringValue(v, "reason"))
2018-08-24 01:24:20 +00:00
case eventTrackException:
2019-01-13 23:37:38 +00:00
err = player.handler.OnTrackException(player, track, jsonStringValue(v, "reason"))
2018-08-24 01:24:20 +00:00
case eventTrackStuck:
2019-01-13 23:37:38 +00:00
err = player.handler.OnTrackStuck(player, track, v.GetInt("thresholdMs"))
2018-08-24 01:24:20 +00:00
}
return err
2019-02-04 00:31:53 +00:00
case opVoiceProcessed:
player, err := node.manager.GetPlayer(jsonStringValue(v, "guildId"))
if err != nil {
return err
}
track := jsonStringValue(v, "track")
data := &VoiceProcessingData{
2019-10-13 01:13:23 +00:00
URL: fmt.Sprintf("%s/audio/%s", node.config.REST, track),
2019-02-04 00:31:53 +00:00
File: track,
}
return player.handler.OnVoiceProcessed(player, data, v.GetBool("hotword"), v.GetBool("override"))
2018-08-24 01:24:20 +00:00
case opStats:
2019-01-13 23:37:38 +00:00
node.load = float32(v.GetFloat64("cpu", "lavalinkLoad"))
2018-08-24 01:24:20 +00:00
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) {
2019-01-13 23:37:38 +00:00
msg := voiceUpdateMessage{
2018-08-24 01:24:20 +00:00
Op: opVoiceUpdate,
GuildID: guildID,
SessionID: sessionID,
Event: &event,
}
2019-01-13 23:37:38 +00:00
err := node.writeMessage(msg)
2018-08-24 01:24:20 +00:00
if err != nil {
return nil, err
}
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
player := &Player{
guildID: guildID,
manager: node.manager,
node: node,
handler: handler,
2018-08-26 02:24:56 +00:00
vol: 100,
2018-08-24 01:24:20 +00:00
}
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
node.manager.players[guildID] = player
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
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)
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
req, err := http.NewRequest(http.MethodGet, url, nil)
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
if err != nil {
return nil, err
}
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
req.Header.Set("Authorization", node.config.Password)
2019-10-13 01:13:23 +00:00
resp, err := node.client.Do(req)
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
if err != nil {
return nil, err
}
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
tracks := new(Tracks)
2019-01-13 23:37:38 +00:00
if err := json.NewDecoder(resp.Body).Decode(&tracks); err != nil {
2018-08-24 01:24:20 +00:00
return nil, err
}
2019-01-13 23:37:38 +00:00
2018-08-24 01:24:20 +00:00
return tracks, nil
}
2019-01-13 23:37:38 +00:00
func (node *Node) writeMessage(v interface{}) error {
return node.wsConn.WriteJSON(v)
}