Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit f41a571

Browse files
committed
Change to Playlist.extras property
Keep track_extras method as is and implement Playlist.extras to mirror Track.extras. Applies to all tracks in Playlist.
1 parent 0f7c768 commit f41a571

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

wavelink/tracks.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def __contains__(self, item: Playable) -> bool:
555555
def pop(self, index: int = -1) -> Playable:
556556
return self.tracks.pop(index)
557557

558-
def track_extras(self, *, extras: bool = False, **attrs: object) -> None:
558+
def track_extras(self, **attrs: object) -> None:
559559
"""Method which sets attributes to all :class:`Playable` in this playlist, with the provided keyword arguments.
560560
561561
This is useful when you need to attach state to your :class:`Playable`, E.g. create a requester attribute.
@@ -583,10 +583,56 @@ def track_extras(self, *, extras: bool = False, **attrs: object) -> None:
583583
print(track.requester)
584584
"""
585585
for track in self.tracks:
586-
target = track.extras if extras else track
587586
for name, value in attrs.items():
588-
setattr(target, name, value)
587+
setattr(track, name, value)
589588

589+
@property
590+
def extras(self) -> ExtrasNamespace:
591+
"""Property returning a :class:`~wavelink.ExtrasNamespace` of extras for this :class:`Playlist`.
592+
593+
You can set this property with a :class:`dict` of valid :class:`str` keys to any valid ``JSON`` value,
594+
or a :class:`~wavelink.ExtrasNamespace`.
595+
596+
If a dict is passed, it will be converted into an :class:`~wavelink.ExtrasNamespace`,
597+
which can be converted back to a dict with dict(...). Additionally, you can also use list or tuple on
598+
:class:`~wavelink.ExtrasNamespace`.
599+
600+
The extras dict will be sent to Lavalink as the ``userData`` field for each track in the playlist.
601+
602+
603+
.. warning::
604+
605+
This is only available when using Lavalink 4+ (**Non BETA**) versions.
606+
607+
608+
Examples
609+
--------
610+
611+
.. code:: python
612+
613+
playlist: wavelink.Playable = wavelink.Playable.search("QUERY")
614+
playlist.extras = {"requester_id": 1234567890}
615+
616+
# later...
617+
print(playlist.extras.requester_id)
618+
# or
619+
print(dict(playlist.extras)["requester_id"])
620+
621+
622+
.. versionadded:: 3.2.0
623+
"""
624+
return self._extras
625+
626+
@extras.setter
627+
def extras(self, __value: ExtrasNamespace | dict[str, Any]) -> None:
628+
if isinstance(__value, ExtrasNamespace):
629+
self._extras = __value
630+
else:
631+
self._extras = ExtrasNamespace(__value)
632+
633+
for track in self.tracks:
634+
for name, value in dict(self._extras).items():
635+
setattr(track, name, value)
590636

591637
class PlaylistInfo:
592638
"""The wavelink PlaylistInfo container class.

0 commit comments

Comments
 (0)