import os import tempfile import logging import discord import yt_dlp from discord.ext import commands from dotenv import load_dotenv logger = logging.getLogger(__name__) # Load environment variables load_dotenv() class Downloader(commands.Cog): def __init__(self, bot): self.bot = bot @commands.slash_command( name="download", description="download video from internet", ) async def download( self, ctx: discord.ApplicationContext, url: discord.Option(str, "url", required=True) ): await ctx.defer() try: # Create a temporary directory with tempfile.TemporaryDirectory() as temp_dir: # Configure yt-dlp to use the temp directory outtmpl = os.path.join(temp_dir, '%(title)s.%(ext)s') ydl_opts = { 'outtmpl': outtmpl, 'merge_output_format': 'mp4', } with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url, download=True) actual_filename = ydl.prepare_filename(info) try: file = discord.File(actual_filename) await ctx.followup.send("Pobran", file=file) logger.info(f"File downloaded: {actual_filename}") except Exception as e: await ctx.followup.send(f":(\n{str(e)}") logger.error(f"Error in download command: {e}", exc_info=True) except Exception as e: await ctx.followup.send(f":(\n{str(e)}") logger.error(f"Error in download command: {e}", exc_info=True) def setup(bot): bot.add_cog(Downloader(bot))