feature: feature complete lavalink
This commit is contained in:
parent
9b3baebda1
commit
b1983105f3
|
@ -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
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
3
model.go
3
model.go
|
@ -62,6 +62,9 @@ const (
|
||||||
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
29
node.go
|
@ -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
|
||||||
|
|
|
@ -10,8 +10,11 @@ import (
|
||||||
// Player is a Lavalink player
|
// Player is a Lavalink player
|
||||||
type Player struct {
|
type Player struct {
|
||||||
guildID string
|
guildID string
|
||||||
|
time int
|
||||||
|
position int
|
||||||
manager *Lavalink
|
manager *Lavalink
|
||||||
node *Node
|
node *Node
|
||||||
|
handler *EventHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play will play the given track completely
|
// Play will play the given track completely
|
||||||
|
|
Loading…
Reference in New Issue