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

Discord.py MP3音乐机器人无法播放音乐问题求助

Discord音乐机器人播放故障排查与修复

问题概述

  • 原本正常运行的Discord音乐机器人,中断开发1个月后彻底无法播放音乐,自用代码和他人开源代码均失效
  • 执行;;play命令时触发ClientException: Not connected to voice错误,连带FFmpeg进程异常终止
  • 尝试将join和play合并为单命令后无报错,但MP3文件仍无法播放
  • 已尝试重装PyNaCl、FFmpeg、VS Code,更新Python,问题依旧,怀疑是本地路径或环境配置问题

错误信息

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: Not connected to voice.
INFO     discord.player ffmpeg process 22896 has not terminated. Waiting to terminate...
INFO     discord.player ffmpeg process 22896 should have terminated with a return code of 1.

当前代码示例

import discord
import time
import os
from discord.ext import commands

BOT_TOKEN = "" # 填写你的机器人Token

intentss = discord.Intents.default()
intentss.message_content = True
intentss.voice_states = True

bot = commands.Bot(command_prefix=";;", intents = intentss)

OPUS_LIBS = ['libopus-0.x86.dll', 'libopus-0.x64.dll', 'libopus-0.dll', 'libopus.so.0', 'libopus.0.dylib']

@bot.command()
async def join(ctx):
    channelVC = ctx.author.voice.channel
    await channelVC.connect()

@bot.command()
async def leave(ctx):
    await ctx.voice_client.disconnect()

@bot.command()
async def play(ctx):
    voice = ctx.guild.voice_client
    mloc = 'C:/Users/Lukas/Desktop/Bot Bethoven/Youtube/test.mp3'
    voice.play(discord.FFmpegPCMAudio(executable = "C:/ffmpeg/bin/ffmpeg.exe", source = mloc))


bot.run(BOT_TOKEN)

排查与修复方案

1. 强制校验语音连接状态

原play命令没有判断机器人是否已连接语音,直接调用voice.play会触发报错。修改play命令,先检查连接状态,未连接则自动加入用户所在频道:

@bot.command()
async def play(ctx):
    voice = ctx.guild.voice_client
    
    # 检查语音连接,未连接则尝试加入用户频道
    if not voice or not voice.is_connected():
        if not ctx.author.voice:
            await ctx.send("你得先加入语音频道啊!")
            return
        voice = await ctx.author.voice.channel.connect()
    
    # 停止当前播放(如果有)
    if voice.is_playing():
        voice.stop()
    
    # 播放音频,添加参数忽略视频流,避免FFmpeg异常
    mloc = 'C:/Users/Lukas/Desktop/Bot Bethoven/Youtube/test.mp3'
    try:
        source = discord.FFmpegPCMAudio(
            executable="C:/ffmpeg/bin/ffmpeg.exe",
            source=mloc,
            options="-vn"
        )
        voice.play(source)
        await ctx.send(f"正在播放:{os.path.basename(mloc)}")
    except Exception as e:
        await ctx.send(f"播放失败:{str(e)}")
        await voice.disconnect()

2. 验证FFmpeg与文件有效性

  • 打开CMD,执行C:/ffmpeg/bin/ffmpeg.exe -version,如果无法输出版本信息,说明FFmpeg路径错误或未正确安装,重新下载对应系统版本的FFmpeg并配置路径
  • 检查MP3文件路径是否正确,直接复制路径到文件资源管理器打开,确认文件存在且能正常播放
  • 右键MP3文件→属性→安全,确保当前运行Python的用户有读取权限

3. 确认Discord权限与Intents配置

  • 登录Discord开发者门户,找到你的机器人,在Privileged Gateway Intents中开启Message Content Intent和Voice States Intent
  • 重新邀请机器人到服务器,权限勾选Connect、Speak、Use Voice Activity(或直接勾选Administrator测试)

4. 修复PyNaCl与opus依赖

  • 卸载重装PyNaCl:打开CMD执行pip uninstall -y pynacl,然后pip install pynacl
  • 如果opus库缺失,手动下载对应系统的opus文件(Windows用户下载libopus-0.x64.dll),放到Python安装目录的Lib/site-packages/discord/bin文件夹下

5. 合并命令的优化版本

如果要把join和play合并,使用以下完整逻辑:

@bot.command()
async def play(ctx):
    # 处理语音连接
    voice = ctx.guild.voice_client
    if not voice or not voice.is_connected():
        if not ctx.author.voice:
            await ctx.send("先进语音频道再听歌!")
            return
        voice = await ctx.author.voice.channel.connect()
    
    # 播放音频
    mloc = 'C:/Users/Lukas/Desktop/Bot Bethoven/Youtube/test.mp3'
    if voice.is_playing():
        voice.stop()
    try:
        source = discord.FFmpegPCMAudio(
            executable="C:/ffmpeg/bin/ffmpeg.exe",
            source=mloc,
            options="-vn"
        )
        voice.play(source)
        await ctx.send("开始播放啦!")
    except Exception as e:
        await ctx.send(f"出错了:{str(e)}")
        await voice.disconnect()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 20:42:46