Add user join/leave events

This commit is contained in:
Tyler 2019-10-12 21:13:23 -04:00
parent 8b7ed3414d
commit a6ec9bedd2
7 changed files with 53 additions and 15 deletions

View File

@ -1,5 +0,0 @@
language: go
go:
- "1.10"
install: go get -t ./...
script: go test ./...

View File

@ -27,6 +27,6 @@ func (d DummyEventHandler) OnTrackStuck(player *Player, track string, threshold
return nil 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 return nil
} }

2
go.mod
View File

@ -5,3 +5,5 @@ require (
github.com/gorilla/websocket v1.4.0 github.com/gorilla/websocket v1.4.0
github.com/valyala/fastjson v1.4.1 github.com/valyala/fastjson v1.4.1
) )
go 1.13

View File

@ -52,10 +52,13 @@ func (lavalink *Lavalink) AddNodes(nodeConfigs ...NodeConfig) error {
config: c, config: c,
manager: lavalink, manager: lavalink,
} }
err := n.open() err := n.open()
if err != nil { if err != nil {
return err return err
} }
nodes[i] = n nodes[i] = n
} }

View File

@ -39,8 +39,14 @@ type voiceUpdateMessage struct {
} }
type listenMessage struct { type listenMessage struct {
Op string `jwon:"op"` Op string `json:"op"`
GuildID string `json:"guildId,omitempty"` GuildID string `json:"guildId,omitempty"`
UserID string `json:"userId,omitempty"` UserID string `json:"userId,omitempty"`
Listen bool `json:"listen"` Listen bool `json:"listen"`
} }
type userMessage struct {
Op string `json:"op"`
GuildID string `json:"guildId,omitempty"`
UserID string `json:"userId,omitempty"`
}

12
node.go
View File

@ -32,23 +32,30 @@ type Node struct {
load float32 load float32
manager *Lavalink manager *Lavalink
wsConn *websocket.Conn wsConn *websocket.Conn
client *http.Client
} }
func (node *Node) open() error { func (node *Node) open() error {
header := http.Header{} header := http.Header{}
header.Set("Authorization", node.config.Password) header.Set("Authorization", node.config.Password)
header.Set("Num-Shards", node.manager.shards) header.Set("Num-Shards", node.manager.shards)
header.Set("User-Id", node.manager.userID) header.Set("User-Id", node.manager.userID)
ws, resp, err := websocket.DefaultDialer.Dial(node.config.WebSocket, header) ws, resp, err := websocket.DefaultDialer.Dial(node.config.WebSocket, header)
if err != nil { if err != nil {
return err return err
} }
vstr := resp.Header.Get("Lavalink-Major-Version") vstr := resp.Header.Get("Lavalink-Major-Version")
v, err := strconv.Atoi(vstr) v, err := strconv.Atoi(vstr)
if err != nil { if err != nil {
return err return err
} }
if v < 3 { if v < 3 {
return errInvalidVersion return errInvalidVersion
} }
@ -66,6 +73,7 @@ func (node *Node) stop() {
if node.wsConn == nil { if node.wsConn == nil {
return return
} }
_ = node.wsConn.Close() _ = node.wsConn.Close()
} }
@ -79,11 +87,13 @@ func (node *Node) listen() {
Log.Println(err) Log.Println(err)
// try to reconnect // try to reconnect
oerr := node.open() oerr := node.open()
if oerr != nil { if oerr != nil {
Log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr) Log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr)
node.manager.removeNode(node) node.manager.removeNode(node)
return return
} }
Log.Println("node", node.config.WebSocket, "reconnected") Log.Println("node", node.config.WebSocket, "reconnected")
return return
} }
@ -204,7 +214,7 @@ func (node *Node) LoadTracks(query string) (*Tracks, error) {
req.Header.Set("Authorization", node.config.Password) req.Header.Set("Authorization", node.config.Password)
resp, err := http.DefaultClient.Do(req) resp, err := node.client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -135,6 +135,28 @@ func (player *Player) Listen(userId string, override bool) error {
return player.node.wsConn.WriteJSON(msg) 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)
}
// Forward will forward a new VOICE_SERVER_UPDATE to a Lavalink node for // Forward will forward a new VOICE_SERVER_UPDATE to a Lavalink node for
// this player. // this player.
// //