Discord Music bot VoiceClient' object has no attribute 'create_ytdl_player'
Asked Answered
T

1

1

I wanted to programm my own discord bot, which plays some songs from youtube but it wont create the ydl player this is the error Command raised an exception: AttributeError: 'VoiceClient' object has no attribute 'create_ytdl_player' and this is my code. Thanks in advance.

@client.command(pass_context=True)
async def s(ctx):
    user=ctx.message.author
    voicech = ctx.author.voice.channel
    voice = await  voicech.connect()
    player = await voice.create_ytdl_player("some url")

    
    
    player = await vc.create_ytdl_player()
    player.start()
Towle answered 21/7, 2020 at 22:41 Comment(0)
T
5

create_ytdl_player was the old way of creating a player. With discord.py@rewrite (> v.1.0), playing music is a bit more complicated. There are two ways to play music. For both ways, using FFmpeg will be necessary, so you'll have to install it.

Here are two of ways to play videos (with youtube-dl and ffmpeg):

  • From file (you'll have to download files):
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from youtube_dl import YoutubeDL

@bot.command(brief="Plays a single video, from a youtube URL") #or bot.command()
async def play(ctx, url):
    voice = get(client.voice_clients, guild=ctx.guild)
    YDL_OPTIONS = {
        'format': 'bestaudio',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'outtmpl': 'song.%(ext)s',
    }

    with YoutubeDL(Music.YDL_OPTIONS) as ydl:
        ydl.download("URL", download=True)

    if not voice.is_playing():
        voice.play(FFmpegPCMAudio("song.mp3"))
        voice.is_playing()
        await ctx.send(f"Now playing {url}")
    else:
        await ctx.send("Already playing song")
        return
  • Without downloading music. This is simpler to play music this way, however, this causes a know issue, well explained here so you'll have to add a FFMPEG_OPTIONS variable:
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from youtube_dl import YoutubeDL

@bot.command(brief="Plays a single video, from a youtube URL")
async def play(ctx, url):
    YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'True'}
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
    voice = get(client.voice_clients, guild=ctx.guild)

    if not voice.is_playing():
        with YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(video_link, download=False)
        URL = info['formats'][0]['url']
        voice.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))
        voice.is_playing()
    else:
        await ctx.send("Already playing song")
        return

These commands will only play songs so you'll have to program every other commands (join, leave, ...).
There are a lot of example on internet, you should look at them once you're used to creating music bots.

Reference: VoiceClient documentation.

Travelled answered 22/7, 2020 at 7:31 Comment(11)
Thank you a lot for this but now i get the following errordiscord.ext.commands.errors.CommandNotFound: Command "play" is not found.Towle
@Towle Use @client.command(brief="Plays a single video, from a youtube URL") instead of @commands.command(brief="Plays a single video, from a youtube URL"). Note that you no longer need to write pass_context=True in discord.py-rewrite.Corrody
Im sorry for interrupting you again now im getting this error discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'get' is not definedTowle
You need to write from discord.utils import get ^^ My bad, this is a part of my code and I forgot to change some things.Travelled
Sorry for another interuption but so Im gettings this error eError: name 'self' is not defined im tried to fix by deleting this part and replacing the self.bot.voice_clients by client.voice_clientsthen i got this error name 'youtube_dl' is not defined. YTDL is installed.Towle
And what is with the ytdl Thing?Towle
I know Im using it alot but Im getting this error even if its installed.Towle
Yup, I didn't realised that I wrote with youtube_dl.YoutubeDL(ydl_opts) as ydl:. I directly imported YoutubeDL so it wasn't recognising youtube_dl. You just have to replace the line to with YoutubeDL(ydl_opts) as ydl:Travelled
Now its working thank you a lot is there way to stop it?Towle
You simply have to create a new command, stop for instance, and write voice = get(client.voice_clients, guild=ctx.guild) and voice.stop() (you'll have to do some error managment too). VoiceClient documentation.Travelled
I suggest changing URL = info['formats'][0]['url'] to URL = info['url'] so you actually use the format set by YDL_OPTIONSLavonia

© 2022 - 2024 — McMap. All rights reserved.