diff options
| author | salmonik <raisersoft@proton.me> | 2026-01-13 16:37:53 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-01-13 16:37:53 +0100 |
| commit | 260c3acf932719f621bd47aea59a3593cbfb629c (patch) | |
| tree | 668eded18764ef5597289731cb8650ffbaf327dc /cogs/music/play.py | |
| parent | bump libs (diff) | |
| download | senator-sperma-260c3acf932719f621bd47aea59a3593cbfb629c.tar.gz senator-sperma-260c3acf932719f621bd47aea59a3593cbfb629c.zip | |
add support for internet streams for playing
Diffstat (limited to '')
| -rw-r--r-- | cogs/music/play.py | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/cogs/music/play.py b/cogs/music/play.py index c9abe4b..2c5b641 100644 --- a/cogs/music/play.py +++ b/cogs/music/play.py | |||
| @@ -137,60 +137,68 @@ class Play(commands.Cog): | |||
| 137 | ctx: discord.ApplicationContext, | 137 | ctx: discord.ApplicationContext, |
| 138 | file: str = discord.Option( | 138 | file: str = discord.Option( |
| 139 | str, | 139 | str, |
| 140 | "Wybierz plik do odtworzenia", | 140 | "wyszukaj plik z serwera", |
| 141 | autocomplete=_search_autocomplete, | 141 | autocomplete=_search_autocomplete, |
| 142 | required=True | 142 | required=False |
| 143 | ), | 143 | ), |
| 144 | source: str = discord.Option( | 144 | stream: str = discord.Option( |
| 145 | str, | 145 | str, |
| 146 | "Źródło dźwięku", | 146 | "odtwórz stream internetowy (e.g. radio)", |
| 147 | choices=["local", "soundcloud", "youtube"], | 147 | required=False |
| 148 | default="local" | ||
| 149 | ), | 148 | ), |
| 150 | compressed: bool = discord.Option( | 149 | compressed: bool = discord.Option( |
| 151 | bool, | 150 | bool, |
| 152 | "Czy skompresować dźwięk?", | 151 | "Czy skompresować dźwięk? (tylko dla plików lokalnych)", |
| 153 | default=False | 152 | default=False |
| 154 | ) | 153 | ) |
| 155 | ): | 154 | ): |
| 156 | """Odtwarza wybrany plik dźwiękowy z opcjonalną kompresją.""" | 155 | """Odtwarza wybrany plik dźwiękowy lub stream internetowy.""" |
| 157 | await ctx.defer() | 156 | await ctx.defer() |
| 158 | 157 | ||
| 159 | if source != "local": | 158 | # Check if either file or stream is provided |
| 160 | await ctx.respond("Tylko źródło 'local' jest obecnie obsługiwane w tej komendzie.", ephemeral=True) | 159 | if not file and not stream: |
| 160 | await ctx.respond("Musisz podać plik do odtworzenia lub adres streamu.", ephemeral=True) | ||
| 161 | return | 161 | return |
| 162 | 162 | ||
| 163 | # Look up the full path from the display name | 163 | if file and stream: |
| 164 | full_path = self.path_mapping.get(file, file) | 164 | await ctx.respond("Możesz podać tylko plik LUB stream, nie oba naraz.", ephemeral=True) |
| 165 | |||
| 166 | if not os.path.exists(full_path): | ||
| 167 | logger.warning(f"File not found: {full_path} (display name: {file})") | ||
| 168 | await ctx.respond("Wybrany plik nie istnieje.", ephemeral=True) | ||
| 169 | return | 165 | return |
| 170 | 166 | ||
| 171 | try: | 167 | try: |
| 172 | if compressed: | 168 | if stream: |
| 173 | # Compress the file before playing | 169 | # Handle internet stream |
| 174 | compressed_path = await play_utils.PlayUtils(self.bot).compress(ctx, full_path) | 170 | await play_utils.PlayUtils(self.bot).play(ctx, stream) |
| 175 | if not compressed_path: | ||
| 176 | await ctx.respond("Nie udało się skompresować pliku.", ephemeral=True) | ||
| 177 | return | ||
| 178 | |||
| 179 | try: | ||
| 180 | await play_utils.PlayUtils(self.bot).play(ctx, compressed_path, delete_after_play=True) | ||
| 181 | except Exception as e: | ||
| 182 | # Clean up the compressed file if play fails | ||
| 183 | if os.path.exists(compressed_path): | ||
| 184 | try: | ||
| 185 | os.remove(compressed_path) | ||
| 186 | logger.info(f"Cleaned up temporary file after play error: {compressed_path}") | ||
| 187 | except Exception as cleanup_error: | ||
| 188 | logger.error(f"Error cleaning up file {compressed_path}: {str(cleanup_error)}") | ||
| 189 | raise e | ||
| 190 | else: | 171 | else: |
| 191 | await play_utils.PlayUtils(self.bot).play(ctx, full_path) | 172 | # Handle local file |
| 173 | full_path = self.path_mapping.get(file, file) | ||
| 174 | |||
| 175 | if not os.path.exists(full_path): | ||
| 176 | logger.warning(f"File not found: {full_path} (display name: {file})") | ||
| 177 | await ctx.respond("Wybrany plik nie istnieje.", ephemeral=True) | ||
| 178 | return | ||
| 179 | |||
| 180 | if compressed: | ||
| 181 | # Compress the file before playing | ||
| 182 | compressed_path = await play_utils.PlayUtils(self.bot).compress(ctx, full_path) | ||
| 183 | if not compressed_path: | ||
| 184 | await ctx.respond("Nie udało się skompresować pliku.", ephemeral=True) | ||
| 185 | return | ||
| 186 | |||
| 187 | try: | ||
| 188 | await play_utils.PlayUtils(self.bot).play(ctx, compressed_path, delete_after_play=True) | ||
| 189 | except Exception as e: | ||
| 190 | # Clean up the compressed file if play fails | ||
| 191 | if os.path.exists(compressed_path): | ||
| 192 | try: | ||
| 193 | os.remove(compressed_path) | ||
| 194 | logger.info(f"Cleaned up temporary file after play error: {compressed_path}") | ||
| 195 | except Exception as cleanup_error: | ||
| 196 | logger.error(f"Error cleaning up file {compressed_path}: {str(cleanup_error)}") | ||
| 197 | raise e | ||
| 198 | else: | ||
| 199 | await play_utils.PlayUtils(self.bot).play(ctx, full_path) | ||
| 192 | except Exception as e: | 200 | except Exception as e: |
| 193 | logger.error(f"Error playing file: {str(e)}", exc_info=True) | 201 | logger.error(f"Error playing: {str(e)}", exc_info=True) |
| 194 | await ctx.respond(f"Wystąpił błąd podczas odtwarzania: {str(e)}", ephemeral=True) | 202 | await ctx.respond(f"Wystąpił błąd podczas odtwarzania: {str(e)}", ephemeral=True) |
| 195 | 203 | ||
| 196 | @discord.slash_command(name="grajcuj_plik", description="grajcuj plik") | 204 | @discord.slash_command(name="grajcuj_plik", description="grajcuj plik") |
