【问题标题】:Can't play audio in voice channels, AttributeError: 'VoiceChannel' object has no attribute 'play'Can\'t play audio in voice channels, AttributeError: \'VoiceChannel\' object has no attribute \'play\'
【发布时间】:2022-11-14 23:14:17
【问题描述】:
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) 没有属性 play 而不能工作。它停止工作的任何原因以及如何解决它?

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、服务器、权限,但没有解决问题

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    AttributeError 通常意味着您的对象类型错误。正如错误所说,VoiceChannel 没有属性play。如果您查看the API docs for voice channel,您确实可以看到那里没有play() 方法。

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

    你不创建,你通常从 a 中获取它们。语音 Channel.connect()。

    如果您查看the docs for 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"))
    

    【讨论】:

      【解决方案2】:
      voice = get(bot.voice_clients, guild=ctx.guild)
      if voice and voice.is_connected:
          await voice.move_to(channel)
      else:
          voice = await channel.connect()
      

      【讨论】:

        猜你喜欢
        • 2022-12-01
        • 2022-10-21
        • 2016-08-28
        • 2021-11-05
        • 2020-04-26
        • 2020-11-21
        • 1970-01-01
        • 1970-01-01
        • 2022-12-15
        相关资源
        最近更新 更多