Advertisement
Towphy

Former Music.py code

Sep 10th, 2020
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. @bot.command()
  2. async def join(ctx):
  3.     channel = ctx.author.voice.channel
  4.     await channel.connect()
  5.  
  6. @bot.command()
  7. async def leave(ctx):
  8.     channel = ctx.author.voice.channel
  9.     await ctx.voice_client.disconnect()
  10.  
  11. @bot.command(pass_context=True, aliases=['p', 'pla'])
  12. async def play(ctx, url: str = None):
  13.     if url is None:
  14.         embed = discord.Embed(title = "Please Specify A YouTube URL")
  15.         await ctx.send(embed=embed)
  16.     else:
  17.         channel = ctx.author.voice.channel
  18.         await channel.connect()
  19.         embed=discord.Embed(title = 'Joined Voice Channel', description=f"Name: {channel}")
  20.         await ctx.send(embed=embed)
  21.  
  22.         song_there = os.path.isfile("song.mp3")
  23.         try:
  24.             if song_there:
  25.                 os.remove("song.mp3")
  26.                 print("Removed old song file")
  27.         except PermissionError:
  28.             print("Trying to delete song file, but it's being played")
  29.             await ctx.send("ERROR: Music playing")
  30.             return
  31.  
  32.         embed=discord.Embed(description=f"**Preparing:** I am preparing this audio track now.")
  33.         await ctx.send(embed=embed)
  34.  
  35.         voice = get(bot.voice_clients, guild=ctx.guild)
  36.  
  37.         ydl_opts = {
  38.             'format': 'bestaudio/best',
  39.             'postprocessors': [{
  40.                 'key': 'FFmpegExtractAudio',
  41.                 'preferredcodec': 'mp3',
  42.                 'preferredquality': '192',
  43.             }],
  44.         }
  45.  
  46.         with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  47.             print("Downloading audio now\n")
  48.             ydl.download([url])
  49.  
  50.         for file in os.listdir("./"):
  51.             if file.endswith(".mp3"):
  52.                 name = file
  53.                 print(f"Renamed File: {file}\n")
  54.                 os.rename(file, "song.mp3")
  55.  
  56.         voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print("Song done!"))
  57.         voice.source = discord.PCMVolumeTransformer(voice.source)
  58.         voice.source.volume = 0.09
  59.  
  60.         nname = name.rsplit("-", 2)
  61.  
  62.         embed2=discord.Embed(title=f"Now Playing Song", description = f"Song: **{nname[0]}**")
  63.         await ctx.send(embed=embed2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement