From 260c3acf932719f621bd47aea59a3593cbfb629c Mon Sep 17 00:00:00 2001 From: salmonik Date: Tue, 13 Jan 2026 16:37:53 +0100 Subject: add support for internet streams for playing --- cogs/music/play.py | 84 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 38 deletions(-) (limited to 'cogs/music/play.py') 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): ctx: discord.ApplicationContext, file: str = discord.Option( str, - "Wybierz plik do odtworzenia", + "wyszukaj plik z serwera", autocomplete=_search_autocomplete, - required=True + required=False ), - source: str = discord.Option( - str, - "Źródło dźwięku", - choices=["local", "soundcloud", "youtube"], - default="local" + stream: str = discord.Option( + str, + "odtwórz stream internetowy (e.g. radio)", + required=False ), compressed: bool = discord.Option( bool, - "Czy skompresować dźwięk?", + "Czy skompresować dźwięk? (tylko dla plików lokalnych)", default=False ) ): - """Odtwarza wybrany plik dźwiękowy z opcjonalną kompresją.""" + """Odtwarza wybrany plik dźwiękowy lub stream internetowy.""" await ctx.defer() - if source != "local": - await ctx.respond("Tylko źródło 'local' jest obecnie obsługiwane w tej komendzie.", ephemeral=True) + # Check if either file or stream is provided + if not file and not stream: + await ctx.respond("Musisz podać plik do odtworzenia lub adres streamu.", ephemeral=True) return - - # Look up the full path from the display name - full_path = self.path_mapping.get(file, file) - - if not os.path.exists(full_path): - logger.warning(f"File not found: {full_path} (display name: {file})") - await ctx.respond("Wybrany plik nie istnieje.", ephemeral=True) + + if file and stream: + await ctx.respond("Możesz podać tylko plik LUB stream, nie oba naraz.", ephemeral=True) return try: - if compressed: - # Compress the file before playing - compressed_path = await play_utils.PlayUtils(self.bot).compress(ctx, full_path) - if not compressed_path: - await ctx.respond("Nie udało się skompresować pliku.", ephemeral=True) - return - - try: - await play_utils.PlayUtils(self.bot).play(ctx, compressed_path, delete_after_play=True) - except Exception as e: - # Clean up the compressed file if play fails - if os.path.exists(compressed_path): - try: - os.remove(compressed_path) - logger.info(f"Cleaned up temporary file after play error: {compressed_path}") - except Exception as cleanup_error: - logger.error(f"Error cleaning up file {compressed_path}: {str(cleanup_error)}") - raise e + if stream: + # Handle internet stream + await play_utils.PlayUtils(self.bot).play(ctx, stream) else: - await play_utils.PlayUtils(self.bot).play(ctx, full_path) + # Handle local file + full_path = self.path_mapping.get(file, file) + + if not os.path.exists(full_path): + logger.warning(f"File not found: {full_path} (display name: {file})") + await ctx.respond("Wybrany plik nie istnieje.", ephemeral=True) + return + + if compressed: + # Compress the file before playing + compressed_path = await play_utils.PlayUtils(self.bot).compress(ctx, full_path) + if not compressed_path: + await ctx.respond("Nie udało się skompresować pliku.", ephemeral=True) + return + + try: + await play_utils.PlayUtils(self.bot).play(ctx, compressed_path, delete_after_play=True) + except Exception as e: + # Clean up the compressed file if play fails + if os.path.exists(compressed_path): + try: + os.remove(compressed_path) + logger.info(f"Cleaned up temporary file after play error: {compressed_path}") + except Exception as cleanup_error: + logger.error(f"Error cleaning up file {compressed_path}: {str(cleanup_error)}") + raise e + else: + await play_utils.PlayUtils(self.bot).play(ctx, full_path) except Exception as e: - logger.error(f"Error playing file: {str(e)}", exc_info=True) + logger.error(f"Error playing: {str(e)}", exc_info=True) await ctx.respond(f"Wystąpił błąd podczas odtwarzania: {str(e)}", ephemeral=True) @discord.slash_command(name="grajcuj_plik", description="grajcuj plik") -- cgit v1.2.3