Advertisement
Guest User

Untitled

a guest
Apr 15th, 2023
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | Source Code | 0 0
  1. import discord
  2. import yt_dlp
  3. import asyncio
  4. from discord.ext import commands
  5. from discord import app_commands
  6.  
  7. # supress noise about console usage from errors
  8. yt_dlp.utils.bug_reports_message = lambda: ''
  9.  
  10. ytdl_format_options = {
  11.     'format': 'bestaudio/best',
  12.     'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
  13.     'restrictfilenames': True,
  14.     'noplaylist': True,
  15.     'nocheckcertificate': True,
  16.     'ignoreerrors': False,
  17.     'logtostderr': False,
  18.     'quiet': True,
  19.     'no_warnings': True,
  20.     'default_search': 'auto',
  21.     'source_address': '0.0.0.0',  # bind to ipv4 since ipv6 addresses cause issues sometimes
  22. }
  23.  
  24. ffmpeg_options = {
  25.     'options': '-vn',
  26. }
  27.  
  28. ytdl = yt_dlp.YoutubeDL(ytdl_format_options)
  29.  
  30. class YTDLSource(discord.PCMVolumeTransformer):
  31.     def __init__(self, source, *, data, volume=1):
  32.         super().__init__(source, volume)
  33.  
  34.         self.data = data
  35.  
  36.         self.title = data.get('title')
  37.         self.url = data.get('url')
  38.    
  39.     @classmethod
  40.     async def from_url(cls, url, *, loop=None, stream=False):
  41.         loop = loop or asyncio.get_event_loop()
  42.         data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
  43.  
  44.         if 'entries' in data:
  45.             data = data['entries'][0]
  46.        
  47.         filename = data['url'] if stream else ytdl.prepare_filename(data)
  48.         return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
  49.  
  50. class Play(commands.Cog):
  51.     def __init__(self, bot: commands.Bot):
  52.         self.bot = bot
  53.         self._last_member = None
  54.  
  55.     @app_commands.command()
  56.     @app_commands.describe(url='The Youtube Video Link')
  57.     async def play(self, interaction: discord.Interaction, *, url: str):
  58.         # Get the voice channel the user is in
  59.         User = interaction.user.voice.channel.id
  60.         UserChannel = interaction.client.get_channel(User)
  61.         # Check if user is in a voice channel
  62.         if UserChannel is not None:
  63.             # Join the voice channel
  64.             await UserChannel.connect()
  65.             # Stream the song
  66.             async with interaction.channel.typing():
  67.                 player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
  68.                 interaction.guild.voice_client.play(player, after=lambda e: print(f'Player error {e}') if e else None)
  69.            
  70.             await interaction.response.send_message(f'Now Playing {player.title} Suga!')
  71.         # if User is not in Voice Channel, tells user to get into voice channel
  72.         else:
  73.             await interaction.response.send_message('You have to be in a voice channel to use me!')
  74.            
  75.  
  76.  
  77.  
  78. async def setup(bot: commands.Bot):
  79.     await bot.add_cog(Play(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement