fix: clean up `Player.position()`

This commit is contained in:
NiceAesth 2023-03-11 18:24:28 +02:00
parent 8860df99de
commit e8d29f9263
1 changed files with 18 additions and 8 deletions

View File

@ -135,7 +135,6 @@ class Player(VoiceProtocol):
*, *,
node: Optional[Node] = None, node: Optional[Node] = None,
) -> None: ) -> None:
self.client: Client = client self.client: Client = client
self.channel: VoiceChannel = channel self.channel: VoiceChannel = channel
@ -166,10 +165,12 @@ class Player(VoiceProtocol):
@property @property
def position(self) -> float: def position(self) -> float:
"""Property which returns the player's position in a track in milliseconds""" """Property which returns the player's position in a track in milliseconds"""
if not self.is_playing or not self._current: if not self.is_playing:
return 0 return 0
current = getattr(self._current, "original", self._current) current: Track = self._current # type: ignore
if current.original:
current = current.original
if self.is_paused: if self.is_paused:
return min(self._last_position, current.length) return min(self._last_position, current.length)
@ -353,7 +354,9 @@ class Player(VoiceProtocol):
self, *, timeout: float, reconnect: bool, self_deaf: bool = False, self_mute: bool = False self, *, timeout: float, reconnect: bool, self_deaf: bool = False, self_mute: bool = False
) -> None: ) -> None:
await self.guild.change_voice_state( await self.guild.change_voice_state(
channel=self.channel, self_deaf=self_deaf, self_mute=self_mute, channel=self.channel,
self_deaf=self_deaf,
self_mute=self_mute,
) )
self._node._players[self.guild.id] = self self._node._players[self.guild.id] = self
self._is_connected = True self._is_connected = True
@ -388,7 +391,9 @@ class Player(VoiceProtocol):
self._node._players.pop(self.guild.id) self._node._players.pop(self.guild.id)
await self._node.send( await self._node.send(
method="DELETE", path=self._player_endpoint_uri, guild_id=self._guild.id, method="DELETE",
path=self._player_endpoint_uri,
guild_id=self._guild.id,
) )
async def play( async def play(
@ -405,15 +410,20 @@ class Player(VoiceProtocol):
raise raise
search = ( search = (
await self._node.get_tracks(f"{track._search_type}:{track.isrc}", ctx=track.ctx) await self._node.get_tracks(f"{track._search_type}:{track.isrc}", ctx=track.ctx)
)[0] # type: ignore )[
0
] # type: ignore
except Exception: except Exception:
# First method didn't work, lets try just searching it up # First method didn't work, lets try just searching it up
try: try:
search = ( search = (
await self._node.get_tracks( await self._node.get_tracks(
f"{track._search_type}:{track.title} - {track.author}", ctx=track.ctx, f"{track._search_type}:{track.title} - {track.author}",
ctx=track.ctx,
) )
)[0] # type: ignore )[
0
] # type: ignore
except: except:
# The song wasn't able to be found, raise error # The song wasn't able to be found, raise error
raise TrackLoadError( raise TrackLoadError(