Skip to content

Commit

Permalink
add support for track user data (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 authored Dec 3, 2023
1 parent 7482a52 commit 1106c2d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
58 changes: 42 additions & 16 deletions lavalink/player_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ func DefaultPlayerUpdate() *PlayerUpdate {
return &PlayerUpdate{}
}

type PlayerUpdateTrack struct {
Encoded *json.Nullable[string] `json:"encoded,omitempty"`
Identifier *string `json:"identifier,omitempty"`
UserData any `json:"userData,omitempty"`
}

type PlayerUpdate struct {
EncodedTrack *json.Nullable[string] `json:"encodedTrack,omitempty"`
Identifier *string `json:"identifier,omitempty"`
Position *Duration `json:"position,omitempty"`
EndTime *Duration `json:"endTime,omitempty"`
Volume *int `json:"volume,omitempty"`
Paused *bool `json:"paused,omitempty"`
Voice *VoiceState `json:"voice,omitempty"`
Filters *Filters `json:"filters,omitempty"`
NoReplace bool `json:"-"`
Track *PlayerUpdateTrack `json:"track,omitempty"`
Position *Duration `json:"position,omitempty"`
EndTime *Duration `json:"endTime,omitempty"`
Volume *int `json:"volume,omitempty"`
Paused *bool `json:"paused,omitempty"`
Voice *VoiceState `json:"voice,omitempty"`
Filters *Filters `json:"filters,omitempty"`
NoReplace bool `json:"-"`
}

type PlayerUpdateOpt func(update *PlayerUpdate)
Expand All @@ -26,31 +31,52 @@ func (u *PlayerUpdate) Apply(opts []PlayerUpdateOpt) {
}
}

func WithNoReplace(noReplace bool) PlayerUpdateOpt {
return func(update *PlayerUpdate) {
update.NoReplace = noReplace
}
}

func WithTrack(track Track) PlayerUpdateOpt {
return WithEncodedTrack(track.Encoded)
return func(update *PlayerUpdate) {
WithEncodedTrack(track.Encoded)(update)
WithTrackUserData(track.UserData)(update)
}
}

func WithEncodedTrack(encodedTrack string) PlayerUpdateOpt {
return func(update *PlayerUpdate) {
update.EncodedTrack = json.NewNullablePtr(encodedTrack)
if update.Track == nil {
update.Track = &PlayerUpdateTrack{}
}
update.Track.Encoded = json.NewNullablePtr(encodedTrack)
}
}

func WithNullTrack() PlayerUpdateOpt {
return func(update *PlayerUpdate) {
update.EncodedTrack = json.NullPtr[string]()
if update.Track == nil {
update.Track = &PlayerUpdateTrack{}
}
update.Track.Encoded = json.NullPtr[string]()
}
}

func WithNoReplace(noReplace bool) PlayerUpdateOpt {
func WithTrackIdentifier(identifier string) PlayerUpdateOpt {
return func(update *PlayerUpdate) {
update.NoReplace = noReplace
if update.Track == nil {
update.Track = &PlayerUpdateTrack{}
}
update.Track.Identifier = &identifier
}
}

func WithIdentifier(identifier string) PlayerUpdateOpt {
func WithTrackUserData(userData any) PlayerUpdateOpt {
return func(update *PlayerUpdate) {
update.Identifier = &identifier
if update.Track == nil {
update.Track = &PlayerUpdateTrack{}
}
update.Track.UserData = userData
}
}

Expand Down
2 changes: 1 addition & 1 deletion lavalink/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lavalink

type Playlist struct {
Info PlaylistInfo `json:"info"`
PluginInfo PluginInfo `json:"pluginInfo"`
PluginInfo RawData `json:"pluginInfo"`
Tracks []Track `json:"tracks"`
}

Expand Down
10 changes: 5 additions & 5 deletions lavalink/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ type Plugin struct {
Version string `json:"version"`
}

type PluginInfo json.RawMessage
type RawData json.RawMessage

func (p PluginInfo) String() string {
func (p RawData) String() string {
return string(p)
}

func (p PluginInfo) Unmarshal(v any) error {
func (p RawData) Unmarshal(v any) error {
return json.Unmarshal(p, v)
}

func (p *PluginInfo) UnmarshalJSON(data []byte) error {
func (p *RawData) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, (*json.RawMessage)(p))
}

func (p PluginInfo) MarshalJSON() ([]byte, error) {
func (p RawData) MarshalJSON() ([]byte, error) {
return json.Marshal(json.RawMessage(p))
}
18 changes: 15 additions & 3 deletions lavalink/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"

"github.com/disgoorg/json"
)
Expand All @@ -14,9 +15,20 @@ var (
)

type Track struct {
Encoded string `json:"encoded"`
Info TrackInfo `json:"info"`
PluginInfo PluginInfo `json:"pluginInfo"`
Encoded string `json:"encoded"`
Info TrackInfo `json:"info"`
PluginInfo RawData `json:"pluginInfo"`
UserData RawData `json:"userData"`
}

// WithUserData returns a copy of the Track with the given userData.
func (t Track) WithUserData(userData any) (Track, error) {
userDataRaw, err := json.Marshal(userData)
if err != nil {
return t, fmt.Errorf("failed to marshal userData: %w", err)
}
t.UserData = userDataRaw
return t, nil
}

func (Track) loadResultData() {}
Expand Down

0 comments on commit 1106c2d

Please sign in to comment.