patch load types and track events for v4

This commit is contained in:
cloudwithax 2023-08-23 10:44:51 -04:00
parent 7eca4724da
commit f3c5461854
3 changed files with 26 additions and 15 deletions

View File

@ -20,7 +20,7 @@ if not discord.version_info.major >= 2:
"using 'pip install discord.py'", "using 'pip install discord.py'",
) )
__version__ = "2.7.0" __version__ = "2.7.1"
__title__ = "pomice" __title__ = "pomice"
__author__ = "cloudwithax" __author__ = "cloudwithax"
__license__ = "GPL-3.0" __license__ = "GPL-3.0"

View File

@ -349,7 +349,7 @@ class Player(VoiceProtocol):
event_type: str = data["type"] event_type: str = data["type"]
event: PomiceEvent = getattr(events, event_type)(data, self) event: PomiceEvent = getattr(events, event_type)(data, self)
if isinstance(event, TrackEndEvent) and event.reason != "REPLACED": if isinstance(event, TrackEndEvent) and event.reason not in ("REPLACED", "replaced"):
self._current = None self._current = None
event.dispatch(self._bot) event.dispatch(self._bot)

View File

@ -329,14 +329,21 @@ class Node:
await self.disconnect() await self.disconnect()
async def _configure_resuming(self) -> None: async def _configure_resuming(self) -> None:
if self._resume_key: if self._resume_key and self._version.major == 3:
data = {"resumingKey": self._resume_key, "timeout": self._resume_timeout} data = {"resumingKey": self._resume_key, "timeout": self._resume_timeout}
await self.send( elif self._resume_key and self._version.major == 4:
method="PATCH", self._log.warning("Using a resume key with Lavalink v4 is deprecated.")
path=f"sessions/{self._session_id}", data = {
include_version=True, "resuming": True if self._resume_key else False,
data=data, "timeout": self._resume_timeout,
) }
await self.send(
method="PATCH",
path=f"sessions/{self._session_id}",
include_version=True,
data=data,
)
async def _listen(self) -> None: async def _listen(self) -> None:
while True: while True:
@ -785,21 +792,25 @@ class Node:
load_type = data.get("loadType") load_type = data.get("loadType")
# Lavalink v4 changed the name of the key from "tracks" to "data"
# so lets account for that
data_type = "data" if self._version.major >= 4 else "tracks"
if not load_type: if not load_type:
raise TrackLoadError( raise TrackLoadError(
"There was an error while trying to load this track.", "There was an error while trying to load this track.",
) )
elif load_type == "LOAD_FAILED": elif load_type in ("LOAD_FAILED", "error"):
exception = data["exception"] exception = data["exception"]
raise TrackLoadError( raise TrackLoadError(
f"{exception['message']} [{exception['severity']}]", f"{exception['message']} [{exception['severity']}]",
) )
elif load_type == "NO_MATCHES": elif load_type in ("NO_MATCHES", "empty"):
return None return None
elif load_type == "PLAYLIST_LOADED": elif load_type in ("PLAYLIST_LOADED", "playlist"):
tracks = [ tracks = [
Track( Track(
track_id=track["encoded"], track_id=track["encoded"],
@ -807,7 +818,7 @@ class Node:
ctx=ctx, ctx=ctx,
track_type=TrackType(track["info"]["sourceName"]), track_type=TrackType(track["info"]["sourceName"]),
) )
for track in data["tracks"] for track in data[data_type]
] ]
return Playlist( return Playlist(
playlist_info=data["playlistInfo"], playlist_info=data["playlistInfo"],
@ -817,7 +828,7 @@ class Node:
uri=query, uri=query,
) )
elif load_type == "SEARCH_RESULT" or load_type == "TRACK_LOADED": elif load_type in ("SEARCH_RESULT", "TRACK_LOADED", "track", "search"):
return [ return [
Track( Track(
track_id=track["encoded"], track_id=track["encoded"],
@ -827,7 +838,7 @@ class Node:
filters=filters, filters=filters,
timestamp=timestamp, timestamp=timestamp,
) )
for track in data["tracks"] for track in data[data_type]
] ]
else: else: