diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0287d47..0000000 --- a/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -language: go -go: - - "1.10" -install: go get -t ./... -script: go test ./... diff --git a/event.go b/event.go index 1b888ac..ead8ee2 100644 --- a/event.go +++ b/event.go @@ -27,6 +27,6 @@ func (d DummyEventHandler) OnTrackStuck(player *Player, track string, threshold return nil } -func (d DummyEventHandler) OnVoiceProcessed(player *Player, file string, hotword, override bool) error { +func (d DummyEventHandler) OnVoiceProcessed(player *Player, data *VoiceProcessingData, hotword, override bool) error { return nil } diff --git a/go.mod b/go.mod index 64d9af0..d8f3feb 100644 --- a/go.mod +++ b/go.mod @@ -5,3 +5,5 @@ require ( github.com/gorilla/websocket v1.4.0 github.com/valyala/fastjson v1.4.1 ) + +go 1.13 diff --git a/lavalink.go b/lavalink.go index c50e46d..0ccae4e 100644 --- a/lavalink.go +++ b/lavalink.go @@ -52,10 +52,13 @@ func (lavalink *Lavalink) AddNodes(nodeConfigs ...NodeConfig) error { config: c, manager: lavalink, } + err := n.open() + if err != nil { return err } + nodes[i] = n } diff --git a/messages.go b/messages.go index 58e137a..adf100d 100644 --- a/messages.go +++ b/messages.go @@ -39,8 +39,14 @@ type voiceUpdateMessage struct { } type listenMessage struct { - Op string `jwon:"op"` - GuildID string `json:"guildId,omitempty"` - UserID string `json:"userId,omitempty"` - Listen bool `json:"listen"` + Op string `json:"op"` + GuildID string `json:"guildId,omitempty"` + UserID string `json:"userId,omitempty"` + Listen bool `json:"listen"` +} + +type userMessage struct { + Op string `json:"op"` + GuildID string `json:"guildId,omitempty"` + UserID string `json:"userId,omitempty"` } diff --git a/node.go b/node.go index 8232e02..d9cf3fa 100644 --- a/node.go +++ b/node.go @@ -32,23 +32,30 @@ type Node struct { load float32 manager *Lavalink wsConn *websocket.Conn + client *http.Client } 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-Major-Version") + v, err := strconv.Atoi(vstr) + if err != nil { return err } + if v < 3 { return errInvalidVersion } @@ -66,6 +73,7 @@ func (node *Node) stop() { if node.wsConn == nil { return } + _ = node.wsConn.Close() } @@ -79,11 +87,13 @@ func (node *Node) listen() { 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 } @@ -143,7 +153,7 @@ func (node *Node) onEvent(v *fastjson.Value) error { track := jsonStringValue(v, "track") data := &VoiceProcessingData{ - URL: fmt.Sprintf("%s/audio/%s", node.config.REST, track), + URL: fmt.Sprintf("%s/audio/%s", node.config.REST, track), File: track, } @@ -204,7 +214,7 @@ func (node *Node) LoadTracks(query string) (*Tracks, error) { req.Header.Set("Authorization", node.config.Password) - resp, err := http.DefaultClient.Do(req) + resp, err := node.client.Do(req) if err != nil { return nil, err diff --git a/player.go b/player.go index 7228ae4..66e3673 100644 --- a/player.go +++ b/player.go @@ -126,10 +126,32 @@ func (player *Player) GetVolume() int { // Listen will override the listening settings for the user func (player *Player) Listen(userId string, override bool) error { msg := listenMessage{ - Op: opUserListen, + Op: opUserListen, GuildID: player.guildID, - UserID: userId, - Listen: override, + UserID: userId, + Listen: override, + } + + return player.node.wsConn.WriteJSON(msg) +} + +// Join will notify the player that a user has joined the channel +func (player *Player) UserJoin(userId string) error { + msg := userMessage{ + Op: opUserJoin, + GuildID: player.guildID, + UserID: userId, + } + + return player.node.wsConn.WriteJSON(msg) +} + +// Leave will notify the player that a user has left the channel +func (player *Player) UserLeave(userId string) error { + msg := userMessage{ + Op: opUserLeave, + GuildID: player.guildID, + UserID: userId, } return player.node.wsConn.WriteJSON(msg)