Supported Lavalink v4 beta3

This commit is contained in:
Choco
2023-09-08 13:59:57 +08:00
parent 40e1d343ea
commit 0ba96704f0
7 changed files with 44 additions and 31 deletions

View File

@@ -59,10 +59,10 @@ class Listeners(commands.Cog):
await player.do_next()
@commands.Cog.listener()
async def on_voicelink_track_exception(self, player: voicelink.Player, track, _):
async def on_voicelink_track_exception(self, player: voicelink.Player, track, error: dict):
try:
player._track_is_stuck = True
await player.context.send(f"{_} Please wait for 5 seconds.", delete_after=10)
await player.context.send(f"{error['message']}! The next song will begin in the next 5 seconds.", delete_after=10)
except:
pass

View File

@@ -1,6 +1,6 @@
"""MIT License
Copyright (c) 2023 Vocard Development
Copyright (c) 2023 - present Vocard Development
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -21,7 +21,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
__version__ = "1.3"
__version__ = "1.4"
__author__ = 'Vocard Development, Choco'
__license__ = "MIT"
__copyright__ = "Copyright 2023 (c) Vocard Development, Choco"

View File

@@ -1,6 +1,6 @@
"""MIT License
Copyright (c) 2023 Vocard Development
Copyright (c) 2023 - present Vocard Development
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -108,12 +108,11 @@ class TrackExceptionEvent(VoicelinkEvent):
def __init__(self, data: dict, player):
self.player = player
self.track = self.player._ending_track
if data.get('error'):
# User is running Lavalink <= 3.3
self.exception: str = data["error"]
else:
# User is running Lavalink >=3.4
self.exception: str = data["exception"]
self.exception: dict = data.get("exception", {
"severity": "",
"message": "",
"cause": ""
})
# on_voicelink_track_exception(player, track, error)
self.handler_args = self.player, self.track, self.exception

View File

@@ -180,7 +180,8 @@ class TrackDecoder:
"identifier": body_reader.read_utf(),
"is_stream": body_reader.read_bool(),
"uri": body_reader.read_optional_utf(),
"thumbnail": None if version != 0 else body_reader.read_optional_utf(),
"thumbnail": None if version not in [0, 3] else body_reader.read_optional_utf(),
"isrc": None if version != 3 else body_reader.read_optional_utf(),
"sourceName": body_reader.read_utf(),
"position": body_reader.read_long()
}

View File

@@ -42,7 +42,7 @@ class Track:
"""
__slots__ = (
"track_id",
"_track_id",
"info",
"identifier",
"title",
@@ -72,7 +72,7 @@ class Track:
search_type: SearchType = SearchType.ytsearch,
spotify_track = None,
):
self.track_id: str = track_id
self._track_id: Optional[str] = track_id
self.info: dict = info
self.identifier: str = info.get("identifier")
@@ -80,7 +80,7 @@ class Track:
self.author: str = info.get("author", "Unknown")
self.uri: str = info.get("uri", "https://discord.com/application-directory/605618911471468554")
self.source: str = info.get("sourceName", extract(self.uri).domain)
self.spotify: bool = True if self.source == "spotify" else False
self.spotify: bool = self.source == "spotify"
if self.spotify:
self.artist_id: Optional[list] = info.get("artist_id")
@@ -91,8 +91,9 @@ class Track:
self.thumbnail: str = None
self.emoji: str = emoji_source(self.source)
if info.get("thumbnail"):
self.thumbnail = info.get("thumbnail")
if artworkUrl := info.get("artworkUrl"):
self.thumbnail = artworkUrl
elif YOUTUBE_REGEX.match(self.uri):
self.thumbnail = f"https://img.youtube.com/vi/{self.identifier}/hqdefault.jpg"
@@ -103,9 +104,6 @@ class Track:
self.is_seekable: bool = info.get("isSeekable", True)
self.position: int = info.get("position", 0)
if not track_id:
self.track_id = encode(self)
def __eq__(self, other) -> bool:
if not isinstance(other, Track):
return False
@@ -124,9 +122,13 @@ class Track:
"info": self.info,
"thumbnail": self.thumbnail
}
def encode(self) -> bytes:
return encode(self)
@property
def track_id(self) -> str:
if not self._track_id:
self._track_id = encode(self)
return self._track_id
@property
def formatted_length(self) -> str:

View File

@@ -303,7 +303,7 @@ class Player(VoiceProtocol):
event_type = data.get("type")
event: VoicelinkEvent = getattr(events, event_type)(data, self)
if isinstance(event, TrackEndEvent) and event.reason != "REPLACED":
if isinstance(event, TrackEndEvent) and event.reason != "replaced":
self._current = None
event.dispatch(self._bot)

View File

@@ -66,7 +66,7 @@ URL_REGEX = re.compile(
r"https?://(?:www\.)?.+"
)
NODE_VERSION = "v3"
NODE_VERSION = "v4"
CALL_METHOD = ["PATCH", "DELETE"]
def exception_catch_callback(task):
@@ -461,33 +461,44 @@ class Node:
) as response:
data = await response.json()
print(data)
load_type = data.get("loadType")
if not load_type:
raise TrackLoadError("There was an error while trying to load this track.")
elif load_type == "LOAD_FAILED":
elif load_type == "error":
exception = data["exception"]
raise TrackLoadError(f"{exception['message']} [{exception['severity']}]")
elif load_type == "NO_MATCHES":
elif load_type == "empty":
return None
elif load_type == "PLAYLIST_LOADED":
elif load_type == "playlist":
return Playlist(
playlist_info=data["playlistInfo"],
tracks=data["tracks"],
requester=requester
)
elif load_type == "SEARCH_RESULT" or load_type == "TRACK_LOADED":
elif load_type == "search":
return [
Track(
track_id=track["track"],
track_id=track["encoded"],
info=track["info"],
requester=requester
)
for track in data["data"]
]
elif load_type == "track":
track = data["data"]
return [
Track(
track_id=track["encoded"],
info=track["info"],
requester=requester
)
for track in data["tracks"]
]
class NodePool: