feature: feature complete lavalink

This commit is contained in:
Christopher F 2018-08-22 18:58:01 -04:00
parent 9b3baebda1
commit b1983105f3
5 changed files with 72 additions and 15 deletions

27
event.go Normal file
View File

@ -0,0 +1,27 @@
package gavalink
// EventHandler defines events that Lavalink may send to a player
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
}
// DummyEventHandler provides an empty event handler for users who
// wish to drop events outright. This is not recommended.
type DummyEventHandler struct{}
// OnTrackEnd is raised when a track ends
func (d DummyEventHandler) OnTrackEnd(player *Player, track string, reason string) error {
return nil
}
// OnTrackException is raised when a track throws an exception
func (d DummyEventHandler) OnTrackException(player *Player, track string, reason string) error {
return nil
}
// OnTrackStuck is raised when a track gets stuck
func (d DummyEventHandler) OnTrackStuck(player *Player, track string, threshold int) error {
return nil
}

View File

@ -22,6 +22,7 @@ var (
errVolumeOutOfRange = errors.New("Volume is out of range, must be within [0, 1000]") errVolumeOutOfRange = errors.New("Volume is out of range, must be within [0, 1000]")
errInvalidVersion = errors.New("This library requires Lavalink >= 3") errInvalidVersion = errors.New("This library requires Lavalink >= 3")
errUnknownPayload = errors.New("Lavalink sent an unknown payload") errUnknownPayload = errors.New("Lavalink sent an unknown payload")
errNilHandler = errors.New("You must provide an event handler. Use gavalink.DummyEventHandler if you wish to ignore events")
) )
// NewLavalink creates a new Lavalink manager // NewLavalink creates a new Lavalink manager

View File

@ -53,15 +53,18 @@ type TrackInfo struct {
} }
const ( const (
opVoiceUpdate = "voiceUpdate" opVoiceUpdate = "voiceUpdate"
opPlay = "play" opPlay = "play"
opStop = "stop" opStop = "stop"
opPause = "pause" opPause = "pause"
opSeek = "seek" opSeek = "seek"
opVolume = "volume" opVolume = "volume"
opDestroy = "destroy" opDestroy = "destroy"
opPlayerUpdate = "playerUpdate" opPlayerUpdate = "playerUpdate"
opEvent = "event" opEvent = "event"
eventTrackEnd = "TrackEndEvent"
eventTrackException = "TrackExceptionEvent"
eventTrackStuck = "TrackStuckEvent"
) )
type message struct { type message struct {

29
node.go
View File

@ -102,9 +102,28 @@ func (node *Node) onEvent(msgType int, msg []byte) error {
switch m.Op { switch m.Op {
case opPlayerUpdate: case opPlayerUpdate:
// todo player, err := node.manager.GetPlayer(m.GuildID)
if err != nil {
return err
}
player.time = m.State.Time
player.position = m.State.Position
case opEvent: case opEvent:
// todo player, err := node.manager.GetPlayer(m.GuildID)
if err != nil {
return err
}
switch m.Type {
case eventTrackEnd:
err = (*player.handler).OnTrackEnd(player, m.Track, m.Reason)
case eventTrackException:
err = (*player.handler).OnTrackException(player, m.Track, m.Reason)
case eventTrackStuck:
err = (*player.handler).OnTrackStuck(player, m.Track, m.ThresholdMs)
}
return err
default: default:
return errUnknownPayload return errUnknownPayload
} }
@ -113,7 +132,10 @@ func (node *Node) onEvent(msgType int, msg []byte) error {
} }
// CreatePlayer creates an audio player on this node // CreatePlayer creates an audio player on this node
func (node *Node) CreatePlayer(guildID string, sessionID string, event VoiceServerUpdate) (*Player, error) { func (node *Node) CreatePlayer(guildID string, sessionID string, event VoiceServerUpdate, handler *EventHandler) (*Player, error) {
if handler == nil {
return nil, errNilHandler
}
msg := message{ msg := message{
Op: opVoiceUpdate, Op: opVoiceUpdate,
GuildID: guildID, GuildID: guildID,
@ -132,6 +154,7 @@ func (node *Node) CreatePlayer(guildID string, sessionID string, event VoiceServ
guildID: guildID, guildID: guildID,
manager: node.manager, manager: node.manager,
node: node, node: node,
handler: handler,
} }
node.manager.players[guildID] = player node.manager.players[guildID] = player
return player, nil return player, nil

View File

@ -9,9 +9,12 @@ import (
// Player is a Lavalink player // Player is a Lavalink player
type Player struct { type Player struct {
guildID string guildID string
manager *Lavalink time int
node *Node position int
manager *Lavalink
node *Node
handler *EventHandler
} }
// Play will play the given track completely // Play will play the given track completely