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
}
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
}

2
go.mod
View File

@ -5,3 +5,5 @@ require (
github.com/gorilla/websocket v1.4.0
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,
manager: lavalink,
}
err := n.open()
if err != nil {
return err
}
nodes[i] = n
}

View File

@ -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"`
}

14
node.go
View File

@ -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

View File

@ -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)