python-3.x 无法在语音通道中播放音频,AttributeError:'VoiceChannel'对象没有'play'属性

kgsdhlau  于 2022-11-19  发布在  Python
关注(0)|答案(2)|浏览(165)
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:\Users\Poco\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Poco\Desktop\py\elevate\main.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:\Users\Poco\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Poco\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Poco\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.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、服务器、权限,但都无法解决问题

vmjh9lq9

vmjh9lq91#

AttributeError通常意味着你有错误的对象类型。就像错误所说的,VoiceChannel没有属性play。如果你查看语音通道的API文档,你确实可以看到那里没有play()方法。
然而,如果你快速搜索文档,你会在VoiceClient上找到这个方法。所以你需要一个VoiceClient的示例来使用play方法。你如何获得VoiceClient的示例?文档清楚地说明了如何做到这一点:
您不需要创建这些参数,通常可以从VoiceChannel.connect()中获取这些参数。
如果您查看VoiceChannel.connect()的文档,您会看到它明确指出:

返回完全连接到语音服务器的语音客户端。

所以你需要做的就是保存返回值。

await vc.connect()

与此:

connection = await vc.connect()

现在你有了一个语音通道对象的示例。现在,不是在通道上调用play(),它没有名为play()的方法,而是在VoiceClient上调用它,它有名为play()的方法。我们的VoiceClient示例名为“connenction”,所以我们可以这样做:

await connection.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/a.mp3"))
83qze16e

83qze16e2#

voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected:
    await voice.move_to(channel)
else:
    voice = await channel.connect()

相关问题