Support for userListen op
This commit is contained in:
parent
51e56c0e2a
commit
8e8ab7b8b4
5
event.go
5
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
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
78
model.go
78
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
|
||||
}
|
||||
|
|
15
node.go
15
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:
|
||||
|
|
12
player.go
12
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.
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue