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]")
|
||||
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
|
||||
|
|
3
model.go
3
model.go
|
@ -62,6 +62,9 @@ const (
|
|||
opDestroy = "destroy"
|
||||
opPlayerUpdate = "playerUpdate"
|
||||
opEvent = "event"
|
||||
eventTrackEnd = "TrackEndEvent"
|
||||
eventTrackException = "TrackExceptionEvent"
|
||||
eventTrackStuck = "TrackStuckEvent"
|
||||
)
|
||||
|
||||
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 {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue