diff --git a/lavalink/player_update.go b/lavalink/player_update.go index b6c87c7..619a213 100644 --- a/lavalink/player_update.go +++ b/lavalink/player_update.go @@ -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) @@ -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 } } diff --git a/lavalink/playlist.go b/lavalink/playlist.go index fb8f1bc..020c004 100644 --- a/lavalink/playlist.go +++ b/lavalink/playlist.go @@ -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"` } diff --git a/lavalink/plugin.go b/lavalink/plugin.go index d5d1584..088031d 100644 --- a/lavalink/plugin.go +++ b/lavalink/plugin.go @@ -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)) } diff --git a/lavalink/track.go b/lavalink/track.go index 139e279..e8d33ea 100644 --- a/lavalink/track.go +++ b/lavalink/track.go @@ -4,6 +4,7 @@ import ( "database/sql" "database/sql/driver" "errors" + "fmt" "github.com/disgoorg/json" ) @@ -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() {}