【问题标题】:Discord Music bot VoiceClient' object has no attribute 'create_ytdl_player'Discord Music bot VoiceClient' 对象没有属性 'create_ytdl_player'
【发布时间】:2020-11-11 09:39:23
【问题描述】:

我想编写我自己的不和谐机器人,它可以播放一些来自 youtube 的歌曲,但它不会创建 ydl 播放器这是错误Command raised an exception: AttributeError: 'VoiceClient' object has no attribute 'create_ytdl_player',这是我的代码。提前致谢。

@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()

【问题讨论】:

    标签: python-3.x discord discord.py youtube-dl


    【解决方案1】:

    create_ytdl_player 是创建播放器的旧方法。使用discord.py@rewrite (> v.1.0),播放音乐有点复杂。有两种播放音乐的方法。对于这两种方式,都需要使用 FFmpeg,所以你必须install it

    这里有两种播放视频的方法(youtube-dlffmpeg):

    • 从文件(您必须下载文件):
    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
    
    • 无需下载音乐。以这种方式播放音乐更简单,但是,这会导致一个已知问题,well explained here,因此您必须添加一个 FFMPEG_OPTIONS 变量:
    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
    

    这些命令只会播放歌曲,因此您必须对所有其他命令(加入、离开等)进行编程。
    网上有很多例子,习惯了制作音乐机器人后应该看看。

    参考: VoiceClient 文档。

    【讨论】:

    • 非常感谢您,但现在我收到以下错误discord.ext.commands.errors.CommandNotFound: Command "play" is not found
    • @Mat 使用@client.command(brief="Plays a single video, from a youtube URL") 而不是@commands.command(brief="Plays a single video, from a youtube URL")。请注意,您不再需要在discord.py-rewrite 中写pass_context=True
    • 很抱歉再次打扰您,我收到此错误discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'get' is not defined
    • 你需要写from discord.utils import get ^^ 我的错,这是我的代码的一部分,我忘了改一些东西。
    • 抱歉再次中断,但我收到此错误eError: name 'self' is not defined 我试图通过删除此部分并将self.bot.voice_clients 替换为client.voice_clients 来修复此错误name 'youtube_dl' is not defined。已安装 YTDL。
    猜你喜欢
    • 2020-02-29
    • 2019-03-21
    • 2022-12-09
    • 2021-02-04
    • 1970-01-01
    • 2018-06-15
    • 2019-12-12
    • 1970-01-01
    相关资源
    最近更新 更多