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

Commit f593c59

Browse files
committed
Added `build_track to node/client.
`build_track` takes in a tracks unique base64 encoded identifier and builds and returns a `Track` object.
1 parent 5b50f98 commit f593c59

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

wavelink/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__author__ = 'EvieePy'
33
__license__ = 'MIT'
44
__copyright__ = 'Copyright 2019-2020 (c) PythonistaGuild'
5-
__version__ = '0.5.03'
5+
__version__ = '0.6.0'
66

77
from .client import Client
88
from .errors import *

wavelink/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,35 @@ async def get_tracks(self, query: str) -> Optional[list]:
108108

109109
return await node.get_tracks(query)
110110

111+
async def build_track(self, identifier: str):
112+
"""|coro|
113+
114+
Build a track object with a valid track identifier.
115+
116+
Parameters
117+
------------
118+
identifier: str
119+
The tracks unique Base64 encoded identifier. This is usually retrieved from various lavalink events.
120+
121+
Returns
122+
---------
123+
:class:`wavelink.player.Track`
124+
The track built from a Base64 identifier.
125+
126+
Raises
127+
--------
128+
ZeroConnectedNodes
129+
There are no :class:`wavelink.node.Node`s currently connected.
130+
BuildTrackError
131+
Decoding and building the track failed.
132+
"""
133+
node = self.get_best_node()
134+
135+
if node is None:
136+
raise ZeroConnectedNodes
137+
138+
return await node.build_track(identifier)
139+
111140
def _get_players(self) -> dict:
112141
players = []
113142

wavelink/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ class ZeroConnectedNodes(WavelinkException):
4040

4141
class AuthorizationFailure(WavelinkException):
4242
"""Exception raised when an invalid password is provided toa node."""
43+
44+
45+
class BuildTrackError(WavelinkException):
46+
"""Exception raised when a track is failed to be decoded and re-built."""

wavelink/node.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,37 @@ async def get_tracks(self, query: str) -> Union[list, TrackPlaylist, None]:
146146

147147
return tracks
148148

149+
async def build_track(self, identifier: str) -> Track:
150+
"""|coro|
151+
152+
Build a track object with a valid track identifier.
153+
154+
Parameters
155+
------------
156+
identifier: str
157+
The tracks unique Base64 encoded identifier. This is usually retrieved from various lavalink events.
158+
159+
Returns
160+
---------
161+
:class:`wavelink.player.Track`
162+
The track built from a Base64 identifier.
163+
164+
Raises
165+
--------
166+
BuildTrackError
167+
Decoding and building the track failed.
168+
"""
169+
async with self.session.get(f'{self.rest_uri}/decodetrack?track={identifier}',
170+
headers={'Authorization': self.password}) as resp:
171+
data = await resp.json()
172+
173+
if not resp.status == 200:
174+
raise BuildTrackError(f'Failed to build track. Status: {data["status"]}, Error: {data["error"]}.'
175+
f'Check the identfier is correct and try again.')
176+
177+
track = Track(id_=identifier, info=data)
178+
return track
179+
149180
def get_player(self, guild_id: int) -> Optional[Player]:
150181
"""Retrieve a player object associated with the Node.
151182

0 commit comments

Comments
 (0)