From a2b5b10625126b7e98643af9fe40569c9e004047 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 18 Apr 2026 00:24:50 +0200 Subject: refactor + preparing bot for public use Signed-off-by: salmonik --- cogs/music/audio_playback.py | 13 +++++-------- cogs/music/media_processing.py | 16 ++++++---------- cogs/music/play_commands.py | 17 ++++++++--------- 3 files changed, 19 insertions(+), 27 deletions(-) (limited to 'cogs/music') diff --git a/cogs/music/audio_playback.py b/cogs/music/audio_playback.py index 75608b8..1267fa7 100644 --- a/cogs/music/audio_playback.py +++ b/cogs/music/audio_playback.py @@ -81,11 +81,8 @@ class AudioPlaybackManager: """Prepare audio source for playback. Returns (source, final_display_name).""" if is_url: # For URLs, use FFmpeg with the direct URL and extract clean filename - ffmpeg_options = { - "options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5", - "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5", - } - audio_source = discord.FFmpegPCMAudio(source, **ffmpeg_options) + before_options = "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5" + audio_source = discord.FFmpegPCMAudio(source, before_options=before_options) # Extract clean filename for URL (remove extension and decode URL encoding) filename = os.path.basename(source.split("?")[0]) # Remove query parameters filename = urllib.parse.unquote(filename) # Decode URL encoding @@ -97,7 +94,7 @@ class AudioPlaybackManager: # Try to get metadata for local files try: - metadata = mutagen.File(source) + metadata = mutagen.open(source) # type: ignore[call-overload] if metadata: title = ( metadata.get("title", [None])[0] @@ -152,8 +149,8 @@ class AudioPlaybackManager: ) # Stop any currently playing audio - if vc.is_playing() or vc.is_paused(): - vc.stop() + if vc.is_playing() or vc.is_paused(): # type: ignore[union-attr] + vc.stop() # type: ignore[union-attr] # Prepare audio source ( diff --git a/cogs/music/media_processing.py b/cogs/music/media_processing.py index e04911c..b9b814c 100644 --- a/cogs/music/media_processing.py +++ b/cogs/music/media_processing.py @@ -19,9 +19,9 @@ class MediaDownloader: @staticmethod async def download(ctx, url): """Download audio from YouTube URL.""" - # Create temp directory if it doesn't exist - temp_dir = os.path.join("cogs", "temp", "music") - os.makedirs(temp_dir, exist_ok=True) + from cogs.utils.temp import get_temp_dir + + temp_dir = get_temp_dir("senator_sperma_music") ytdl_opts = { "outtmpl": os.path.join(temp_dir, "%(title)s.%(ext)s"), @@ -69,17 +69,13 @@ class AudioCompressor: @staticmethod async def compress(ctx, filename): """Compress audio file to opus format.""" + from cogs.utils.temp import get_temp_dir + if not filename: await ctx.respond("dzie wideło", ephemeral=True) return None - # Create temp directory if it doesn't exist - temp_dir = os.path.join( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))), - "temp", - "compressed", - ) - os.makedirs(temp_dir, exist_ok=True) + temp_dir = get_temp_dir("senator_sperma_compressed") # Create a temporary output filename in the temp directory base_name = os.path.basename(filename) diff --git a/cogs/music/play_commands.py b/cogs/music/play_commands.py index e90d85f..fd5113a 100644 --- a/cogs/music/play_commands.py +++ b/cogs/music/play_commands.py @@ -23,10 +23,10 @@ class PlayCommands(commands.Cog): """Discord slash commands for music playback.""" def __init__(self, bot): + from cogs.utils.temp import get_temp_dir + self.bot = bot - self.temp_dir = os.path.join( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "temp" - ) + self.temp_dir = get_temp_dir() self.path_mapping = {} # Maps display names to full paths @staticmethod @@ -104,12 +104,11 @@ class PlayCommands(commands.Cog): display = display.strip() - self.path_mapping[display] = url + if len(display) > 100: + display = display[:99] + "…" - if len(display) > 90: - display = display[:87] + "..." - - choices.append(OptionChoice(display[:100], url)) + self.path_mapping[display] = url + choices.append(OptionChoice(display, display)) if len(choices) >= 25: break @@ -167,7 +166,7 @@ class PlayCommands(commands.Cog): self, ctx: discord.ApplicationContext, file: str, compressed: bool ) -> None: """Handle file playback from copyparty URL.""" - url = file # The option value is now the URL directly + url = self.path_mapping.get(file, file) # Look up URL from display name if compressed: await self._handle_compressed_file_play(ctx, url) -- cgit v1.2.3