Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @bot.command()
- async def join(ctx):
- channel = ctx.author.voice.channel
- await channel.connect()
- @bot.command()
- async def leave(ctx):
- channel = ctx.author.voice.channel
- await ctx.voice_client.disconnect()
- @bot.command(pass_context=True, aliases=['p', 'pla'])
- async def play(ctx, url: str = None):
- if url is None:
- embed = discord.Embed(title = "Please Specify A YouTube URL")
- await ctx.send(embed=embed)
- else:
- channel = ctx.author.voice.channel
- await channel.connect()
- embed=discord.Embed(title = 'Joined Voice Channel', description=f"Name: {channel}")
- await ctx.send(embed=embed)
- song_there = os.path.isfile("song.mp3")
- try:
- if song_there:
- os.remove("song.mp3")
- print("Removed old song file")
- except PermissionError:
- print("Trying to delete song file, but it's being played")
- await ctx.send("ERROR: Music playing")
- return
- embed=discord.Embed(description=f"**Preparing:** I am preparing this audio track now.")
- await ctx.send(embed=embed)
- voice = get(bot.voice_clients, guild=ctx.guild)
- ydl_opts = {
- 'format': 'bestaudio/best',
- 'postprocessors': [{
- 'key': 'FFmpegExtractAudio',
- 'preferredcodec': 'mp3',
- 'preferredquality': '192',
- }],
- }
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
- print("Downloading audio now\n")
- ydl.download([url])
- for file in os.listdir("./"):
- if file.endswith(".mp3"):
- name = file
- print(f"Renamed File: {file}\n")
- os.rename(file, "song.mp3")
- voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print("Song done!"))
- voice.source = discord.PCMVolumeTransformer(voice.source)
- voice.source.volume = 0.09
- nname = name.rsplit("-", 2)
- embed2=discord.Embed(title=f"Now Playing Song", description = f"Song: **{nname[0]}**")
- await ctx.send(embed=embed2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement