diff options
| author | salmonik <raisersoft@proton.me> | 2026-04-18 00:24:50 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-04-18 00:24:50 +0200 |
| commit | a2b5b10625126b7e98643af9fe40569c9e004047 (patch) | |
| tree | 2b59a929d068bc28bbc21e8c8d8836e4d29270c0 /cogs/music | |
| parent | add mathematical plotting (diff) | |
| download | senator-sperma-a2b5b10625126b7e98643af9fe40569c9e004047.tar.gz senator-sperma-a2b5b10625126b7e98643af9fe40569c9e004047.zip | |
refactor + preparing bot for public use
Signed-off-by: salmonik <raisersoft@proton.me>
Diffstat (limited to 'cogs/music')
| -rw-r--r-- | cogs/music/audio_playback.py | 13 | ||||
| -rw-r--r-- | cogs/music/media_processing.py | 16 | ||||
| -rw-r--r-- | cogs/music/play_commands.py | 17 |
3 files changed, 19 insertions, 27 deletions
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: | |||
| 81 | """Prepare audio source for playback. Returns (source, final_display_name).""" | 81 | """Prepare audio source for playback. Returns (source, final_display_name).""" |
| 82 | if is_url: | 82 | if is_url: |
| 83 | # For URLs, use FFmpeg with the direct URL and extract clean filename | 83 | # For URLs, use FFmpeg with the direct URL and extract clean filename |
| 84 | ffmpeg_options = { | 84 | before_options = "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5" |
| 85 | "options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5", | 85 | audio_source = discord.FFmpegPCMAudio(source, before_options=before_options) |
| 86 | "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5", | ||
| 87 | } | ||
| 88 | audio_source = discord.FFmpegPCMAudio(source, **ffmpeg_options) | ||
| 89 | # Extract clean filename for URL (remove extension and decode URL encoding) | 86 | # Extract clean filename for URL (remove extension and decode URL encoding) |
| 90 | filename = os.path.basename(source.split("?")[0]) # Remove query parameters | 87 | filename = os.path.basename(source.split("?")[0]) # Remove query parameters |
| 91 | filename = urllib.parse.unquote(filename) # Decode URL encoding | 88 | filename = urllib.parse.unquote(filename) # Decode URL encoding |
| @@ -97,7 +94,7 @@ class AudioPlaybackManager: | |||
| 97 | 94 | ||
| 98 | # Try to get metadata for local files | 95 | # Try to get metadata for local files |
| 99 | try: | 96 | try: |
| 100 | metadata = mutagen.File(source) | 97 | metadata = mutagen.open(source) # type: ignore[call-overload] |
| 101 | if metadata: | 98 | if metadata: |
| 102 | title = ( | 99 | title = ( |
| 103 | metadata.get("title", [None])[0] | 100 | metadata.get("title", [None])[0] |
| @@ -152,8 +149,8 @@ class AudioPlaybackManager: | |||
| 152 | ) | 149 | ) |
| 153 | 150 | ||
| 154 | # Stop any currently playing audio | 151 | # Stop any currently playing audio |
| 155 | if vc.is_playing() or vc.is_paused(): | 152 | if vc.is_playing() or vc.is_paused(): # type: ignore[union-attr] |
| 156 | vc.stop() | 153 | vc.stop() # type: ignore[union-attr] |
| 157 | 154 | ||
| 158 | # Prepare audio source | 155 | # Prepare audio source |
| 159 | ( | 156 | ( |
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: | |||
| 19 | @staticmethod | 19 | @staticmethod |
| 20 | async def download(ctx, url): | 20 | async def download(ctx, url): |
| 21 | """Download audio from YouTube URL.""" | 21 | """Download audio from YouTube URL.""" |
| 22 | # Create temp directory if it doesn't exist | 22 | from cogs.utils.temp import get_temp_dir |
| 23 | temp_dir = os.path.join("cogs", "temp", "music") | 23 | |
| 24 | os.makedirs(temp_dir, exist_ok=True) | 24 | temp_dir = get_temp_dir("senator_sperma_music") |
| 25 | 25 | ||
| 26 | ytdl_opts = { | 26 | ytdl_opts = { |
| 27 | "outtmpl": os.path.join(temp_dir, "%(title)s.%(ext)s"), | 27 | "outtmpl": os.path.join(temp_dir, "%(title)s.%(ext)s"), |
| @@ -69,17 +69,13 @@ class AudioCompressor: | |||
| 69 | @staticmethod | 69 | @staticmethod |
| 70 | async def compress(ctx, filename): | 70 | async def compress(ctx, filename): |
| 71 | """Compress audio file to opus format.""" | 71 | """Compress audio file to opus format.""" |
| 72 | from cogs.utils.temp import get_temp_dir | ||
| 73 | |||
| 72 | if not filename: | 74 | if not filename: |
| 73 | await ctx.respond("dzie wideło", ephemeral=True) | 75 | await ctx.respond("dzie wideło", ephemeral=True) |
| 74 | return None | 76 | return None |
| 75 | 77 | ||
| 76 | # Create temp directory if it doesn't exist | 78 | temp_dir = get_temp_dir("senator_sperma_compressed") |
| 77 | temp_dir = os.path.join( | ||
| 78 | os.path.dirname(os.path.dirname(os.path.abspath(__file__))), | ||
| 79 | "temp", | ||
| 80 | "compressed", | ||
| 81 | ) | ||
| 82 | os.makedirs(temp_dir, exist_ok=True) | ||
| 83 | 79 | ||
| 84 | # Create a temporary output filename in the temp directory | 80 | # Create a temporary output filename in the temp directory |
| 85 | base_name = os.path.basename(filename) | 81 | 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): | |||
| 23 | """Discord slash commands for music playback.""" | 23 | """Discord slash commands for music playback.""" |
| 24 | 24 | ||
| 25 | def __init__(self, bot): | 25 | def __init__(self, bot): |
| 26 | from cogs.utils.temp import get_temp_dir | ||
| 27 | |||
| 26 | self.bot = bot | 28 | self.bot = bot |
| 27 | self.temp_dir = os.path.join( | 29 | self.temp_dir = get_temp_dir() |
| 28 | os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "temp" | ||
| 29 | ) | ||
| 30 | self.path_mapping = {} # Maps display names to full paths | 30 | self.path_mapping = {} # Maps display names to full paths |
| 31 | 31 | ||
| 32 | @staticmethod | 32 | @staticmethod |
| @@ -104,12 +104,11 @@ class PlayCommands(commands.Cog): | |||
| 104 | 104 | ||
| 105 | display = display.strip() | 105 | display = display.strip() |
| 106 | 106 | ||
| 107 | self.path_mapping[display] = url | 107 | if len(display) > 100: |
| 108 | display = display[:99] + "…" | ||
| 108 | 109 | ||
| 109 | if len(display) > 90: | 110 | self.path_mapping[display] = url |
| 110 | display = display[:87] + "..." | 111 | choices.append(OptionChoice(display, display)) |
| 111 | |||
| 112 | choices.append(OptionChoice(display[:100], url)) | ||
| 113 | 112 | ||
| 114 | if len(choices) >= 25: | 113 | if len(choices) >= 25: |
| 115 | break | 114 | break |
| @@ -167,7 +166,7 @@ class PlayCommands(commands.Cog): | |||
| 167 | self, ctx: discord.ApplicationContext, file: str, compressed: bool | 166 | self, ctx: discord.ApplicationContext, file: str, compressed: bool |
| 168 | ) -> None: | 167 | ) -> None: |
| 169 | """Handle file playback from copyparty URL.""" | 168 | """Handle file playback from copyparty URL.""" |
| 170 | url = file # The option value is now the URL directly | 169 | url = self.path_mapping.get(file, file) # Look up URL from display name |
| 171 | 170 | ||
| 172 | if compressed: | 171 | if compressed: |
| 173 | await self._handle_compressed_file_play(ctx, url) | 172 | await self._handle_compressed_file_play(ctx, url) |
