Skip to content

Commit

Permalink
fixed player position not updating, added starting player on specific…
Browse files Browse the repository at this point in the history
… node and added Player.Connected
  • Loading branch information
topi314 committed Feb 13, 2022
1 parent 1761f4b commit 4bd1cfd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
3 changes: 1 addition & 2 deletions disgolink/_example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"regexp"
"strconv"
"syscall"
"time"

"github.com/DisgoOrg/disgolink/disgolink"
"github.com/DisgoOrg/snowflake"
Expand Down Expand Up @@ -93,5 +92,5 @@ func registerNodes() {
Secure: secure,
ResumingKey: os.Getenv("lavalink_resuming_key"),
})
_ = dgolink.BestNode().ConfigureResuming("test", 20*time.Second)
_ = dgolink.BestNode().ConfigureResuming("test", 20)
}
2 changes: 1 addition & 1 deletion disgolink/_example/music_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (p *MusicPlayer) OnTrackEnd(player lavalink.Player, track lavalink.AudioTra
}
}
func (p *MusicPlayer) OnTrackException(player lavalink.Player, track lavalink.AudioTrack, exception lavalink.FriendlyException) {
_, _ = p.channel.CreateMessage(discord.NewMessageCreateBuilder().SetContentf("AudioTrack exception: `%s`, `%s`, `%+v`", track.Info().Title, exception).Build())
_, _ = p.channel.CreateMessage(discord.NewMessageCreateBuilder().SetContentf("AudioTrack exception: `%s`, `%+v`", track.Info().Title, exception).Build())
}
func (p *MusicPlayer) OnTrackStuck(player lavalink.Player, track lavalink.AudioTrack, thresholdMs int) {
_, _ = p.channel.CreateMessage(discord.NewMessageCreateBuilder().SetContentf("track stuck: `%s`, %d", track.Info().Title, thresholdMs).Build())
Expand Down
11 changes: 10 additions & 1 deletion lavalink/lavalink.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Lavalink interface {
DecodeTrack(track string) (AudioTrack, error)

Player(guildID snowflake.Snowflake) Player
PlayerOnNode(name string, guildID snowflake.Snowflake) Player
ExistingPlayer(guildID snowflake.Snowflake) Player
Players() map[snowflake.Snowflake]Player

Expand Down Expand Up @@ -199,12 +200,20 @@ func (l *lavalinkImpl) DecodeTrack(str string) (AudioTrack, error) {
}

func (l *lavalinkImpl) Player(guildID snowflake.Snowflake) Player {
return l.PlayerOnNode("", guildID)
}

func (l *lavalinkImpl) PlayerOnNode(name string, guildID snowflake.Snowflake) Player {
l.playersMu.Lock()
defer l.playersMu.Unlock()
if player, ok := l.players[guildID]; ok {
return player
}
player := NewPlayer(l.BestNode(), guildID)
node := l.Node(name)
if node == nil {
node = l.BestNode()
}
player := NewPlayer(node, guildID)
for _, pl := range l.config.Plugins {
if plugin, ok := pl.(PluginEventHandler); ok {
plugin.OnNewPlayer(player)
Expand Down
2 changes: 1 addition & 1 deletion lavalink/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (n *nodeImpl) listen() {
}
_, data, err := n.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
n.lavalink.Logger().Errorf("error while reading from lavalink websocket. error: %s", err)
n.Close()
if err = n.reconnect(); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion lavalink/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Player interface {
Pause(paused bool) error
Paused() bool
Position() Duration
Connected() bool
Seek(position Duration) error
Volume() int
SetVolume(volume int) error
Expand Down Expand Up @@ -183,7 +184,7 @@ func (p *DefaultPlayer) Position() Duration {
if p.track == nil {
return 0
}
if p.paused {
if !p.paused {
timeDiff := Duration(time.Since(p.state.Time).Milliseconds())
if p.state.Position+timeDiff > p.track.Info().Length {
return p.track.Info().Length
Expand All @@ -193,6 +194,10 @@ func (p *DefaultPlayer) Position() Duration {
return p.state.Position
}

func (p *DefaultPlayer) Connected() bool {
return p.state.Connected
}

func (p *DefaultPlayer) Seek(position Duration) error {
if err := p.Node().Send(SeekCommand{
GuildID: p.guildID,
Expand Down

0 comments on commit 4bd1cfd

Please sign in to comment.