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]")
errInvalidVersion = errors.New("This library requires Lavalink >= 3")
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

View File

@ -62,6 +62,9 @@ const (
opDestroy = "destroy"
opPlayerUpdate = "playerUpdate"
opEvent = "event"
eventTrackEnd = "TrackEndEvent"
eventTrackException = "TrackExceptionEvent"
eventTrackStuck = "TrackStuckEvent"
)
type message struct {

29
node.go
View File

@ -102,9 +102,28 @@ func (node *Node) onEvent(msgType int, msg []byte) error {
switch m.Op {
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:
// 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:
return errUnknownPayload
}
@ -113,7 +132,10 @@ func (node *Node) onEvent(msgType int, msg []byte) error {
}
// 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{
Op: opVoiceUpdate,
GuildID: guildID,
@ -132,6 +154,7 @@ func (node *Node) CreatePlayer(guildID string, sessionID string, event VoiceServ
guildID: guildID,
manager: node.manager,
node: node,
handler: handler,
}
node.manager.players[guildID] = player
return player, nil

View File

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