Discord音乐人语音工具“ VoiceClient”对象没有属性“ create_ytdl_player” [英] Discord Music bot VoiceClient' object has no attribute 'create_ytdl_player'

查看:109
本文介绍了Discord音乐人语音工具“ VoiceClient”对象没有属性“ create_ytdl_player”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对自己的discord机器人进行编程,该机器人播放youtube上的某些歌曲,但不会创建ydl播放器,这是错误命令引发了异常:AttributeError:'VoiceClient'对象没有属性' create_ytdl_player',这是我的代码。

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


推荐答案

create_ytdl_player 是创建播放器的旧方法。使用 discord.py@rewrite (> v.1.0),播放音乐有点更复杂。有两种播放音乐的方式。对于这两种方式,都必须使用FFmpeg,因此您必须安装它

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.

以下是两种播放视频的方法(使用 youtube-dl ffmpeg ):

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


  • 从文件中(您必须下载文件):

from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from youtube_dl import YoutubeDL

@client.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



  • 无需下载音乐。这样更容易播放音乐,但是,这会导致一个已知问题,在这里进行了很好的解释,因此您必须添加 FFMPEG_OPTIONS 变量:

    • 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
      
      @commands.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.

      参考文献: VoiceClient 文档。

      这篇关于Discord音乐人语音工具“ VoiceClient”对象没有属性“ create_ytdl_player”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆