From 8e8ab7b8b4bb2b4e3dc6b845af4742ac22081c74 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 3 Feb 2019 19:31:53 -0500 Subject: [PATCH] Support for userListen op --- event.go | 5 ++++ messages.go | 7 +++++ model.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ node.go | 15 +++++++++++ player.go | 12 +++++++++ 5 files changed, 117 insertions(+) diff --git a/event.go b/event.go index 350ce6d..1b888ac 100644 --- a/event.go +++ b/event.go @@ -5,6 +5,7 @@ type EventHandler interface { OnTrackEnd(player *Player, track string, reason string) error OnTrackException(player *Player, track string, reason string) 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 @@ -25,3 +26,7 @@ func (d DummyEventHandler) OnTrackException(player *Player, track string, reason func (d DummyEventHandler) OnTrackStuck(player *Player, track string, threshold int) error { return nil } + +func (d DummyEventHandler) OnVoiceProcessed(player *Player, file string, hotword, override bool) error { + return nil +} diff --git a/messages.go b/messages.go index 2036b7a..58e137a 100644 --- a/messages.go +++ b/messages.go @@ -37,3 +37,10 @@ type voiceUpdateMessage struct { SessionID string `json:"sessionId,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"` +} diff --git a/model.go b/model.go index ac3322a..b4fb9f7 100644 --- a/model.go +++ b/model.go @@ -1,5 +1,11 @@ package gavalink +import ( + "io" + "net/http" + "os" +) + const ( // TrackLoaded is a Tracks Type for a succesful single track load TrackLoaded = "TRACK_LOADED" @@ -54,6 +60,10 @@ type TrackInfo struct { const ( opVoiceUpdate = "voiceUpdate" + opVoiceProcessed = "voiceProcessed" + opUserJoin = "userJoin" + opUserLeave = "userLeave" + opUserListen = "userListen" opPlay = "play" opStop = "stop" opPause = "pause" @@ -63,6 +73,7 @@ const ( opPlayerUpdate = "playerUpdate" opEvent = "event" opStats = "stats" + eventTrackStart = "TrackStartEvent" eventTrackEnd = "TrackEndEvent" eventTrackException = "TrackExceptionEvent" eventTrackStuck = "TrackStuckEvent" @@ -74,3 +85,70 @@ type VoiceServerUpdate struct { Endpoint string `json:"endpoint"` 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 +} diff --git a/node.go b/node.go index e00e55b..8232e02 100644 --- a/node.go +++ b/node.go @@ -133,6 +133,21 @@ func (node *Node) onEvent(v *fastjson.Value) error { } 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: node.load = float32(v.GetFloat64("cpu", "lavalinkLoad")) default: diff --git a/player.go b/player.go index cf7480f..7228ae4 100644 --- a/player.go +++ b/player.go @@ -123,6 +123,18 @@ func (player *Player) GetVolume() int { 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 // this player. //