无法在语音通道中播放音频,属性错误:"语音通道"对象没有属性"播放"


vc = bot.get_channel(ctx.author.voice.channel.id)
await vc.connect()
await vc.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/a.mp3"))

这个代码工作像3个月前,现在它不,因为语音通道对象(vc)没有属性播放。它停止工作的原因是什么?如何修复它?

Ignoring exception in command play:
Traceback (most recent call last):
File "C:UsersPocoAppDataLocalProgramsPythonPython310libsite-packagesdiscordextcommandscore.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:UsersPocoDesktoppyelevatemain.py", line 38, in play
await vc.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/ass.mp3"))
AttributeError: 'VoiceChannel' object has no attribute 'play'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersPocoAppDataLocalProgramsPythonPython310libsite-packagesdiscordextcommandsbot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:UsersPocoAppDataLocalProgramsPythonPython310libsite-packagesdiscordextcommandscore.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersPocoAppDataLocalProgramsPythonPython310libsite-packagesdiscordextcommandscore.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceChannel' object has no attribute 'play'

我尝试使用不同的vcs,服务器,权限,但没有解决问题

AttributeError通常意味着您有错误的对象类型。如错误所示,VoiceChannel没有属性play。如果您查看语音通道的API文档,您确实可以看到那里没有play()方法。

但是,如果您在文档中进行快速搜索,您将在VoiceClient上找到该方法。所以你需要VoiceClient的实例来使用play方法。如何获得VoiceClient的实例?文档清楚地说明了如何做到这一点:

您不创建这些,您通常从例如voicecchannel .connect()中获取它们。

如果你看一下VoiceChannel.connect()的文档,你会看到它清楚地写着:

收益

语音客户端与语音服务器完全连接。

所以你需要做的就是保存返回值。所以替换这个:

await vc.connect()

与这个:

connection = await vc.connect()

现在您有了一个语音通道对象的实例。现在,不是在通道上调用play()(通道上没有名为play()的方法),而是在通道上调用VoiceClient(通道上有)。我们的VoiceClient实例被命名为"conenction",所以我们可以这样做:

await connection.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/a.mp3"))
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected:
await voice.move_to(channel)
else:
voice = await channel.connect()

相关内容

最新更新