Discord.py机器人播放音频报错:VoiceClient无create_ffmpeg_player属性
报错原因
你当前使用的是discord.py v1.0及以上的正式版本,旧版本v0.16.x中VoiceClient自带的create_ffmpeg_player方法已经被官方彻底移除,因此调用时会抛出属性不存在的异常。
修复方案
- 先导入discord提供的FFmpeg音频处理类
from discord import FFmpegPCMAudio
- 修改你的
playSound函数中播放逻辑部分的代码,替换旧的create_ffmpeg_player相关写法:
async def playSound(ctx, channel, file, bot, connected): ctx = ctx.message # 先判断是否已经连接,避免重复加入报错 if ctx.guild.voice_client is not None: await ctx.guild.voice_client.disconnect() vc = await connected.channel.connect() await ctx.channel.send(embed = returnembed(preset = 'playingSound', info = str(channel))) # 替换旧的播放器创建逻辑 vc.play(FFmpegPCMAudio(file)) # 等待播放完成 while vc.is_playing() or vc.is_paused(): await asyncio.sleep(1) # 播放结束后断开连接 await vc.disconnect() await os.remove(file)
额外注意事项
- 确保你的设备已经安装了FFmpeg,并且已将FFmpeg路径加入系统环境变量,否则会出现找不到执行文件的报错
- 如果需要添加播放错误处理,可以给
vc.play方法加after回调函数捕获异常 - 调用函数前可以先加判断
if ctx.author.voice is None避免用户未加入语音频道时调用出错
内容的提问来源于stack exchange,提问作者Anony Mous
相关产品推荐
相关产品推荐

