Skip to content

Commit 9b824c3

Browse files
author
timhauke
committed
fix: more logging
1 parent a942dfb commit 9b824c3

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

.github/instructions/codacy.instructions.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
# Codacy Rules
77
Configuration for AI behavior when interacting with Codacy's MCP Server
88

9+
## using any tool that accepts the arguments: `provider`, `organization`, or `repository`
10+
- ALWAYS use:
11+
- provider: gh
12+
- organization: VectoDE
13+
- repository: VectoBeat
14+
- Avoid calling `git remote -v` unless really necessary
15+
916
## CRITICAL: After ANY successful `edit_file` or `reapply` operation
1017
- YOU MUST IMMEDIATELY run the `codacy_cli_analyze` tool from Codacy's MCP Server for each file that was edited, with:
1118
- `rootPath`: set to the workspace path

src/commands/music_controls.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
import discord
99
import lavalink
10+
from aiohttp import ClientError as AiohttpClientError, ContentTypeError
1011
from discord import app_commands
1112
from discord.ext import commands
13+
from lavalink.errors import ClientError as LavalinkClientError
1214

1315
from src.services.lavalink_service import LavalinkVoiceClient
1416
from src.utils.embeds import EmbedFactory
@@ -17,6 +19,10 @@
1719
VOICE_PERMISSIONS = ("connect", "speak", "view_channel")
1820

1921

22+
class TrackLookupError(Exception):
23+
"""Raised when Lavalink fails to provide a usable load result."""
24+
25+
2026
def ms_to_clock(ms: int) -> str:
2127
"""Convert milliseconds into a human readable duration string."""
2228
seconds = max(0, int(ms // 1000))
@@ -140,19 +146,35 @@ def _tag_tracks(
140146
track.requester = requester_id
141147
return tracks
142148

149+
async def _safe_get_tracks(self, identifier: str) -> lavalink.LoadResult:
150+
"""Fetch tracks from Lavalink with user-friendly error handling."""
151+
try:
152+
return await self.bot.lavalink.get_tracks(identifier)
153+
except ContentTypeError as exc:
154+
status = getattr(exc, "status", "unknown")
155+
raise TrackLookupError(
156+
f"Lavalink returned an unexpected response (HTTP {status}). Please verify the node is reachable."
157+
) from exc
158+
except (AiohttpClientError, LavalinkClientError) as exc:
159+
raise TrackLookupError(
160+
"Unable to reach the Lavalink node. Please try again in a few moments."
161+
) from exc
162+
143163
async def _resolve(self, query: str) -> lavalink.LoadResult:
144164
query = query.strip()
165+
if not query:
166+
raise TrackLookupError("Please provide a search query or URL.")
145167
if URL_REGEX.match(query):
146-
result = await self.bot.lavalink.get_tracks(query)
168+
result = await self._safe_get_tracks(query)
147169
if result.tracks:
148170
return result
149171
last: Optional[lavalink.LoadResult] = None
150172
for prefix in ("ytsearch", "scsearch", "amsearch"):
151-
result = await self.bot.lavalink.get_tracks(f"{prefix}:{query}")
173+
result = await self._safe_get_tracks(f"{prefix}:{query}")
152174
if result.tracks:
153175
return result
154176
last = result
155-
return last or await self.bot.lavalink.get_tracks(query)
177+
return last or await self._safe_get_tracks(query)
156178

157179
async def _player(self, inter: discord.Interaction) -> Optional[lavalink.DefaultPlayer]:
158180
factory = EmbedFactory(inter.guild.id if inter.guild else None)
@@ -220,7 +242,11 @@ async def play(self, inter: discord.Interaction, query: str):
220242
if not player:
221243
return
222244

223-
results = await self._resolve(query)
245+
try:
246+
results = await self._resolve(query)
247+
except TrackLookupError as exc:
248+
await inter.followup.send(embed=factory.error(str(exc)), ephemeral=True)
249+
return
224250
if results.load_type == "LOAD_FAILED":
225251
return await inter.followup.send(embed=factory.error("Loading the track failed."), ephemeral=True)
226252
if not results.tracks:

src/services/lavalink_service.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,34 @@ async def connect(
4848
channel=self.channel, self_deaf=self_deaf, self_mute=self_mute
4949
)
5050

51+
def _is_self(self, user_id) -> bool:
52+
me = getattr(self.client, "user", None)
53+
if not me or user_id is None:
54+
return False
55+
return str(user_id) == str(me.id)
56+
57+
def _is_guild(self, guild_id) -> bool:
58+
if guild_id is None:
59+
return False
60+
try:
61+
return int(guild_id) == self.guild_id
62+
except (TypeError, ValueError):
63+
return False
64+
5165
async def on_voice_server_update(self, data):
66+
if not self._is_guild(data.get("guild_id")):
67+
return
68+
5269
payload = {
5370
"t": "VOICE_SERVER_UPDATE",
5471
"d": data,
5572
}
5673
await self.lavalink.voice_update_handler(payload)
5774

5875
async def on_voice_state_update(self, data):
76+
if not self._is_guild(data.get("guild_id")) or not self._is_self(data.get("user_id")):
77+
return
78+
5979
channel_id = data.get("channel_id")
6080

6181
if not channel_id:

0 commit comments

Comments
 (0)