Mat*_*Mat 1 python-3.x youtube-dl discord discord.py
我想编写自己的不和谐机器人,它可以播放 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()
Run Code Online (Sandbox Code Playgroud)
create_ytdl_player是创建玩家的旧方法。使用discord.py@rewrite (> v.1.0),播放音乐有点复杂。有两种播放音乐的方式。对于这两种方式,都需要使用 FFmpeg,因此您必须安装它。
以下是播放视频的两种方法(使用youtube-dl和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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这些命令只会播放歌曲,因此您必须对所有其他命令(加入、离开等)进行编程。
互联网上有很多例子,一旦你习惯了创建音乐机器人,你应该看看它们。
参考: VoiceClient文档。
| 归档时间: |
|
| 查看次数: |
9310 次 |
| 最近记录: |