我正在尝试通过 discord.py 构建一个播放器音乐机器人 我使用 voice.play() 播放音乐但它不起作用 这是我的代码:
description="Play a song given by a url",
options=[
create_option(
name="input",
description="Url or name of song",
option_type=3,
required=True,
)]
)
async def play(self, ctx, input: str):
try:
channel = ctx.guild.voice_channels[0]
await channel.connect()
except:
pass
YDL_OPTIONS = {'format': 'bestaudio'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'}
voice = discord.utils.get(self.client.voice_clients, guild=ctx.guild)
if not voice.is_playing():
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(input, download=False)
if "entries" in info.keys():
for ent in info["entries"]:
try:
self.queue[ctx.guild.id].append((ent['title'], ent['url']))
except:
self.queue[ctx.guild.id] = []
self.queue[ctx.guild.id].append((ent['title'], ent['url']))
finally:
voice.play(FFmpegPCMAudio(executable="ffmpeg.exe", source=self.queue[ctx.guild.id][0][1],
**FFMPEG_OPTIONS))
else:
URL = info['formats'][0]['url']
voice.play(FFmpegPCMAudio(executable="ffmpeg.exe", source=URL,
**FFMPEG_OPTIONS))
但是音乐不播放。控制台没有出现错误,在不和谐客户端有一个红色警报“此交互失败”。任何人都可以帮助我,请?。实际上,当我尝试使用另一种方式播放音乐时
DiscordUtils.Music().create_player(ctx.guild, ffmpeg_error_betterfix=True, **FFMPEG_OPTIONS)
它运行良好,但我更喜欢使用第一个,因为它可以帮助我更轻松地管理歌曲队列