From b1983105f3a1ca0b1b9a61766996b7d781518cb3 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Wed, 22 Aug 2018 18:58:01 -0400 Subject: [PATCH] feature: feature complete lavalink --- event.go | 27 +++++++++++++++++++++++++++ lavalink.go | 1 + model.go | 21 ++++++++++++--------- node.go | 29 ++++++++++++++++++++++++++--- player.go | 9 ++++++--- 5 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 event.go diff --git a/event.go b/event.go new file mode 100644 index 0000000..ec373f3 --- /dev/null +++ b/event.go @@ -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 +} diff --git a/lavalink.go b/lavalink.go index 0015124..5ec2633 100644 --- a/lavalink.go +++ b/lavalink.go @@ -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 diff --git a/model.go b/model.go index e881001..5214f9b 100644 --- a/model.go +++ b/model.go @@ -53,15 +53,18 @@ type TrackInfo struct { } const ( - opVoiceUpdate = "voiceUpdate" - opPlay = "play" - opStop = "stop" - opPause = "pause" - opSeek = "seek" - opVolume = "volume" - opDestroy = "destroy" - opPlayerUpdate = "playerUpdate" - opEvent = "event" + opVoiceUpdate = "voiceUpdate" + opPlay = "play" + opStop = "stop" + opPause = "pause" + opSeek = "seek" + opVolume = "volume" + opDestroy = "destroy" + opPlayerUpdate = "playerUpdate" + opEvent = "event" + eventTrackEnd = "TrackEndEvent" + eventTrackException = "TrackExceptionEvent" + eventTrackStuck = "TrackStuckEvent" ) type message struct { diff --git a/node.go b/node.go index 79048be..85adf7b 100644 --- a/node.go +++ b/node.go @@ -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 diff --git a/player.go b/player.go index 5e62cb5..b30a0c3 100644 --- a/player.go +++ b/player.go @@ -9,9 +9,12 @@ import ( // Player is a Lavalink player type Player struct { - guildID string - manager *Lavalink - node *Node + guildID string + time int + position int + manager *Lavalink + node *Node + handler *EventHandler } // Play will play the given track completely