Discord.py 不播放音频
Discord.py doesn't play audio
基本上我制作了一个简单地播放音乐的机器人,我制作了一个播放歌曲的命令,所以你 ?play [url of song]
它会播放,现在我正在开发一个 ?search 命令,你可以输入关键字,它会播放歌曲,在出现很多错误后,我在输入歌曲时找到了歌曲。但是它不会通过音频传输声音(但是当您使用 ?play 命令时它会传输)这是我的代码:
import discord
from discord.ext import commands
import youtube_dl
import requests
class music(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("Join a voice channel dumb fuck")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
@commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
@commands.command()
async def play(self,ctx,url):
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def search(self, ctx, arg):
ctx.voice_client.stop()
try:
requests.get("" + str(arg))
except: arg = " " + str(arg)
else: arg = "" + str(arg)
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
url2 = info['formats'][0]['url']
return {'source': info['formats'][0]['url'], 'title': info['title']}
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def pause(self,ctx):
await ctx.voice_client.pause()
await ctx.send("PAUSED")
@commands.command()
async def resume(self,ctx):
await ctx.voice_client.resume()
await ctx.send("RESUMED")
def setup(client):
client.add_cog(music(client))
控制台没有给出任何错误,它只是这样说:
[download] Downloading playlist: superidol
[youtube:search] query " superidol": Downloading page 1
[youtube:search] playlist superidol: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] chY9p-XLHHk: Downloading webpage
[download] Finished downloading playlist: superidol
不知道答案,但从你所说的内容和控制台发送的内容来看,我认为它下载的是网页,而不是视频。
如果是这种情况,则无法播放网页,所以这可能是问题所在。
PS: 我发了一个答案,不是评论,因为我没有足够的声誉。
基本上我制作了一个简单地播放音乐的机器人,我制作了一个播放歌曲的命令,所以你 ?play [url of song]
它会播放,现在我正在开发一个 ?search 命令,你可以输入关键字,它会播放歌曲,在出现很多错误后,我在输入歌曲时找到了歌曲。但是它不会通过音频传输声音(但是当您使用 ?play 命令时它会传输)这是我的代码:
import discord
from discord.ext import commands
import youtube_dl
import requests
class music(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("Join a voice channel dumb fuck")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
@commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
@commands.command()
async def play(self,ctx,url):
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def search(self, ctx, arg):
ctx.voice_client.stop()
try:
requests.get("" + str(arg))
except: arg = " " + str(arg)
else: arg = "" + str(arg)
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
url2 = info['formats'][0]['url']
return {'source': info['formats'][0]['url'], 'title': info['title']}
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def pause(self,ctx):
await ctx.voice_client.pause()
await ctx.send("PAUSED")
@commands.command()
async def resume(self,ctx):
await ctx.voice_client.resume()
await ctx.send("RESUMED")
def setup(client):
client.add_cog(music(client))
控制台没有给出任何错误,它只是这样说:
[download] Downloading playlist: superidol
[youtube:search] query " superidol": Downloading page 1
[youtube:search] playlist superidol: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] chY9p-XLHHk: Downloading webpage
[download] Finished downloading playlist: superidol
不知道答案,但从你所说的内容和控制台发送的内容来看,我认为它下载的是网页,而不是视频。
如果是这种情况,则无法播放网页,所以这可能是问题所在。
PS: 我发了一个答案,不是评论,因为我没有足够的声誉。