Discord Music bot VoiceClient' 对象没有属性 'create_ytdl_player' [英] Discord Music bot VoiceClient' object has no attribute 'create_ytdl_player'
问题描述
我想编写我自己的不和谐机器人,它可以播放一些来自 youtube 的歌曲,但它不会创建 ydl 播放器这是错误 命令引发异常:AttributeError: 'VoiceClient' object has no attribute '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
@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
- 无需下载音乐.以这种方式播放音乐更简单,但是,这会导致一个已知问题,这里解释得很好 所以你必须添加一个
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
@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.
参考: VoiceClient
文档.
这篇关于Discord Music bot VoiceClient' 对象没有属性 'create_ytdl_player'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!