You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

语音频道无法播放音频:'VoiceChannel'无'play'属性问题求助

故障原因

你错误地在VoiceChannel对象上调用了play()方法——这个方法属于**VoiceClient(语音客户端)**对象,而非VoiceChannel(语音频道)对象。

旧版discord.py可能存在兼容逻辑让错误写法暂时生效,但版本更新后API行为更严格,必须明确使用connect()返回的VoiceClient实例来调用播放方法。

修复方法

调用VoiceChannel.connect()时,将返回的VoiceClient对象保存下来,再用这个对象调用play()方法。

修正后的代码示例

# 获取语音频道对象
vc_channel = bot.get_channel(ctx.author.voice.channel.id)
# 连接频道并获取语音客户端实例
vc_client = await vc_channel.connect()
# 通过语音客户端播放音频
await vc_client.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/a.mp3"))

也可以用更简洁的写法:

vc_client = await ctx.author.voice.channel.connect()
await vc_client.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/a.mp3"))
额外注意事项
  • 确保discord.py是最新稳定版,避免旧版本的API兼容问题
  • 检查ffmpeg的路径是否正确,保证程序能正常找到该可执行文件

内容的提问来源于stack exchange,提问作者tone5wheelchair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 13:05:37