Support for userListen op

This commit is contained in:
Tyler 2019-02-03 19:31:53 -05:00
parent 51e56c0e2a
commit 8e8ab7b8b4
5 changed files with 117 additions and 0 deletions

View File

@ -5,6 +5,7 @@ type EventHandler interface {
OnTrackEnd(player *Player, track string, reason string) error OnTrackEnd(player *Player, track string, reason string) error
OnTrackException(player *Player, track string, reason string) error OnTrackException(player *Player, track string, reason string) error
OnTrackStuck(player *Player, track string, threshold int) error OnTrackStuck(player *Player, track string, threshold int) error
OnVoiceProcessed(player *Player, data *VoiceProcessingData, hotword, override bool) error
} }
// DummyEventHandler provides an empty event handler for users who // DummyEventHandler provides an empty event handler for users who
@ -25,3 +26,7 @@ func (d DummyEventHandler) OnTrackException(player *Player, track string, reason
func (d DummyEventHandler) OnTrackStuck(player *Player, track string, threshold int) error { func (d DummyEventHandler) OnTrackStuck(player *Player, track string, threshold int) error {
return nil return nil
} }
func (d DummyEventHandler) OnVoiceProcessed(player *Player, file string, hotword, override bool) error {
return nil
}

View File

@ -37,3 +37,10 @@ type voiceUpdateMessage struct {
SessionID string `json:"sessionId,omitempty"` SessionID string `json:"sessionId,omitempty"`
Event *VoiceServerUpdate `json:"event,omitempty"` Event *VoiceServerUpdate `json:"event,omitempty"`
} }
type listenMessage struct {
Op string `jwon:"op"`
GuildID string `json:"guildId,omitempty"`
UserID string `json:"userId,omitempty"`
Listen bool `json:"listen"`
}

View File

@ -1,5 +1,11 @@
package gavalink package gavalink
import (
"io"
"net/http"
"os"
)
const ( const (
// TrackLoaded is a Tracks Type for a succesful single track load // TrackLoaded is a Tracks Type for a succesful single track load
TrackLoaded = "TRACK_LOADED" TrackLoaded = "TRACK_LOADED"
@ -54,6 +60,10 @@ type TrackInfo struct {
const ( const (
opVoiceUpdate = "voiceUpdate" opVoiceUpdate = "voiceUpdate"
opVoiceProcessed = "voiceProcessed"
opUserJoin = "userJoin"
opUserLeave = "userLeave"
opUserListen = "userListen"
opPlay = "play" opPlay = "play"
opStop = "stop" opStop = "stop"
opPause = "pause" opPause = "pause"
@ -63,6 +73,7 @@ const (
opPlayerUpdate = "playerUpdate" opPlayerUpdate = "playerUpdate"
opEvent = "event" opEvent = "event"
opStats = "stats" opStats = "stats"
eventTrackStart = "TrackStartEvent"
eventTrackEnd = "TrackEndEvent" eventTrackEnd = "TrackEndEvent"
eventTrackException = "TrackExceptionEvent" eventTrackException = "TrackExceptionEvent"
eventTrackStuck = "TrackStuckEvent" eventTrackStuck = "TrackStuckEvent"
@ -74,3 +85,70 @@ type VoiceServerUpdate struct {
Endpoint string `json:"endpoint"` Endpoint string `json:"endpoint"`
Token string `json:"token"` Token string `json:"token"`
} }
// VoiceProcessingData is an event containing methods to easily download captured voice data
type VoiceProcessingData struct {
io.ReadCloser
Client *http.Client
URL string
File string
res *http.Response
}
func (v *VoiceProcessingData) open() error {
res, err := v.Client.Get(v.URL)
if err != nil {
return err
}
v.res = res
}
func (v *VoiceProcessingData) Close() error {
if v.res != nil {
return v.res.Body.Close()
}
return nil
}
func (v *VoiceProcessingData) Read(buf []byte) (n int, err error) {
if v.res == nil {
err = v.open()
if err != nil {
return
}
}
n, err = v.res.Body.Read(buf)
return
}
func (v *VoiceProcessingData) SaveTo(file string) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
if v.res == nil {
err = v.open()
if err != nil {
return err
}
}
defer v.res.Body.Close()
_, err = io.Copy(f, v.res.Body)
return err
}

15
node.go
View File

@ -133,6 +133,21 @@ func (node *Node) onEvent(v *fastjson.Value) error {
} }
return err return err
case opVoiceProcessed:
player, err := node.manager.GetPlayer(jsonStringValue(v, "guildId"))
if err != nil {
return err
}
track := jsonStringValue(v, "track")
data := &VoiceProcessingData{
URL: fmt.Sprintf("%s/audio/%s", node.config.REST, track),
File: track,
}
return player.handler.OnVoiceProcessed(player, data, v.GetBool("hotword"), v.GetBool("override"))
case opStats: case opStats:
node.load = float32(v.GetFloat64("cpu", "lavalinkLoad")) node.load = float32(v.GetFloat64("cpu", "lavalinkLoad"))
default: default:

View File

@ -123,6 +123,18 @@ func (player *Player) GetVolume() int {
return player.vol return player.vol
} }
// Listen will override the listening settings for the user
func (player *Player) Listen(userId string, override bool) error {
msg := listenMessage{
Op: opUserListen,
GuildID: player.guildID,
UserID: userId,
Listen: override,
}
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.
// //