actually make logging optional lol

This commit is contained in:
cloudwithax 2024-02-01 22:03:30 -05:00
parent 4507b50b8b
commit a926616028
4 changed files with 100 additions and 58 deletions

View File

@ -96,6 +96,7 @@ class Client:
).decode()
token_data = json.loads(token_json)
self.expiry = datetime.fromtimestamp(token_data["exp"])
if self._log:
self._log.debug(f"Fetched Apple Music bearer token successfully")
async def search(self, query: str) -> Union[Album, Playlist, Song, Artist]:
@ -130,6 +131,7 @@ class Client:
)
data: dict = await resp.json(loads=json.loads)
if self._log:
self._log.debug(
f"Made request to Apple Music API with status {resp.status} and response {data}",
)

View File

@ -298,6 +298,7 @@ class Player(VoiceProtocol):
self._last_update = int(state.get("time", 0))
self._is_connected = bool(state.get("connected"))
self._last_position = int(state.get("position", 0))
if self._log:
self._log.debug(f"Got player update state with data {state}")
async def _dispatch_voice_update(self, voice_data: Optional[Dict[str, Any]] = None) -> None:
@ -319,7 +320,10 @@ class Player(VoiceProtocol):
data={"voice": data},
)
self._log.debug(f"Dispatched voice update to {state['event']['endpoint']} with data {data}")
if self._log:
self._log.debug(
f"Dispatched voice update to {state['event']['endpoint']} with data {data}",
)
async def on_voice_server_update(self, data: VoiceServerUpdate) -> None:
self._voice_state.update({"event": data})
@ -361,6 +365,7 @@ class Player(VoiceProtocol):
if isinstance(event, TrackStartEvent):
self._ending_track = self._current
if self._log:
self._log.debug(f"Dispatched event {data['type']} to player.")
async def _refresh_endpoint_uri(self, session_id: Optional[str]) -> None:
@ -383,6 +388,7 @@ class Player(VoiceProtocol):
data=data or None,
)
if self._log:
self._log.debug(f"Swapped all players to new node {new_node._identifier}.")
async def get_tracks(
@ -456,6 +462,7 @@ class Player(VoiceProtocol):
data={"encodedTrack": None},
)
if self._log:
self._log.debug(f"Player has been stopped.")
async def disconnect(self, *, force: bool = False) -> None:
@ -484,6 +491,7 @@ class Player(VoiceProtocol):
guild_id=self._guild.id,
)
if self._log:
self._log.debug("Player has been destroyed.")
async def play(
@ -577,6 +585,7 @@ class Player(VoiceProtocol):
query=f"noReplace={ignore_if_playing}",
)
if self._log:
self._log.debug(
f"Playing {track.title} from uri {track.uri} with a length of {track.length}",
)
@ -600,6 +609,7 @@ class Player(VoiceProtocol):
data={"position": position},
)
if self._log:
self._log.debug(f"Seeking to {position}.")
return self.position
@ -613,6 +623,7 @@ class Player(VoiceProtocol):
)
self._paused = pause
if self._log:
self._log.debug(f"Player has been {'paused' if pause else 'resumed'}.")
return self._paused
@ -626,6 +637,7 @@ class Player(VoiceProtocol):
)
self._volume = volume
if self._log:
self._log.debug(f"Player volume has been adjusted to {volume}")
return self._volume
@ -655,8 +667,10 @@ class Player(VoiceProtocol):
data={"filters": payload},
)
if self._log:
self._log.debug(f"Filter has been applied to player with tag {_filter.tag}")
if fast_apply:
if self._log:
self._log.debug(f"Fast apply passed, now applying filter instantly.")
await self.seek(self.position)
@ -678,8 +692,10 @@ class Player(VoiceProtocol):
guild_id=self._guild.id,
data={"filters": payload},
)
if self._log:
self._log.debug(f"Filter has been removed from player with tag {filter_tag}")
if fast_apply:
if self._log:
self._log.debug(f"Fast apply passed, now removing filter instantly.")
await self.seek(self.position)
@ -709,8 +725,10 @@ class Player(VoiceProtocol):
guild_id=self._guild.id,
data={"filters": payload},
)
if self._log:
self._log.debug(f"Filter with tag {filter_tag} has been edited to {edited_filter!r}")
if fast_apply:
if self._log:
self._log.debug(f"Fast apply passed, now editing filter instantly.")
await self.seek(self.position)
@ -735,8 +753,10 @@ class Player(VoiceProtocol):
guild_id=self._guild.id,
data={"filters": {}},
)
if self._log:
self._log.debug(f"All filters have been removed from player.")
if fast_apply:
if self._log:
self._log.debug(f"Fast apply passed, now removing all filters instantly.")
await self.seek(self.position)

View File

@ -247,6 +247,7 @@ class Node:
int(_version_groups[2] or 0),
)
if self._log:
self._log.debug(f"Parsed Lavalink version: {major}.{minor}.{fix}")
self._version = LavalinkVersion(major=major, minor=minor, fix=fix)
if self._version < LavalinkVersion(3, 7, 0):
@ -306,6 +307,7 @@ class Node:
if self._version.major == 3:
data["resumingKey"] = self._resume_key
elif self._version.major == 4:
if self._log:
self._log.warning("Using a resume key with Lavalink v4 is deprecated.")
data["resuming"] = True
@ -321,6 +323,7 @@ class Node:
try:
msg = await self._websocket.recv()
data = json.loads(msg)
if self._log:
self._log.debug(f"Recieved raw websocket message {msg}")
self._loop.create_task(self._handle_ws_msg(data=data))
except exceptions.ConnectionClosed:
@ -335,13 +338,17 @@ class Node:
backoff = ExponentialBackoff(base=7)
retry = backoff.delay()
self._log.debug(f"Retrying connection to Node {self._identifier} in {retry} secs")
if self._log:
self._log.debug(
f"Retrying connection to Node {self._identifier} in {retry} secs",
)
await asyncio.sleep(retry)
if not self.is_connected:
self._loop.create_task(self.connect(reconnect=True))
async def _handle_ws_msg(self, data: dict) -> None:
if self._log:
self._log.debug(f"Recieved raw payload from Node {self._identifier} with data {data}")
op = data.get("op", None)
@ -395,6 +402,7 @@ class Node:
headers=self._headers,
json=data or {},
)
if self._log:
self._log.debug(
f"Making REST request to Node {self._identifier} with method {method} to {uri}",
)
@ -405,17 +413,20 @@ class Node:
)
if method == "DELETE" or resp.status == 204:
if self._log:
self._log.debug(
f"REST request to Node {self._identifier} with method {method} to {uri} completed sucessfully and returned no data.",
)
return await resp.json(content_type=None)
if resp.content_type == "text/plain":
if self._log:
self._log.debug(
f"REST request to Node {self._identifier} with method {method} to {uri} completed sucessfully and returned text with body {await resp.text()}",
)
return await resp.text()
if self._log:
self._log.debug(
f"REST request to Node {self._identifier} with method {method} to {uri} completed sucessfully and returned JSON with body {await resp.json()}",
)
@ -446,6 +457,7 @@ class Node:
await self._handle_version_check(version=version)
await self._set_ext_client_session(session=self._session)
if self._log:
self._log.debug(
f"Version check from Node {self._identifier} successful. Returned version {version}",
)
@ -457,11 +469,13 @@ class Node:
)
if reconnect:
if self._log:
self._log.debug(f"Trying to reconnect to Node {self._identifier}...")
if self.player_count:
for player in self.players.values():
await player._refresh_endpoint_uri(self._session_id)
if self._log:
self._log.debug(
f"Node {self._identifier} successfully connected to websocket using {self._websocket_uri}/v{self._version.major}/websocket",
)
@ -473,6 +487,7 @@ class Node:
end = time.perf_counter()
if self._log:
self._log.info(f"Connected to node {self._identifier}. Took {end - start:.3f}s")
return self
@ -498,10 +513,12 @@ class Node:
for player in self.players.copy().values():
await player.destroy()
if self._log:
self._log.debug("All players disconnected from node.")
await self._websocket.close()
await self._session.close()
if self._log:
self._log.debug("Websocket and http session closed.")
del self._pool._nodes[self._identifier]
@ -509,6 +526,7 @@ class Node:
self._task.cancel()
end = time.perf_counter()
if self._log:
self._log.info(
f"Successfully disconnected from node {self._identifier} and closed all sessions. Took {end - start:.3f}s",
)

View File

@ -63,6 +63,7 @@ class Client:
)
data: dict = await resp.json(loads=json.loads)
if self._log:
self._log.debug(f"Fetched Spotify bearer token successfully")
self._bearer_token = data["access_token"]
@ -91,6 +92,7 @@ class Client:
)
data: dict = await resp.json(loads=json.loads)
if self._log:
self._log.debug(
f"Made request to Spotify API with status {resp.status} and response {data}",
)