Discord.js机器人加入语音频道但无音频输出问题排查
Discord机器人语音播放无声音问题排查
环境
- Node.js:本地v24.10.0,Railway生产环境v18.20.5
- 操作系统:本地Windows 11,生产环境Railway Linux
- discord.js:14.14.1
- @discordjs/voice:0.17.0
问题现象
除音频播放外,其他功能均正常:
✅ 机器人连接Discord正常
✅ 斜杠命令可用
✅ 机器人可加入语音频道(Discord中可见)
✅ connection.subscribe(player)返回真值
✅ 播放器触发AudioPlayerStatus.Playing事件
✅ 通过ffmpeg-static可获取FFmpeg
✅ 通过opusscript可获取Opus编码器
✅ 通过tweetnacl可获取加密库
✅ 控制台/日志无报错
❌ 频道内无人听到音频
❌ 机器人静默至音频时长后断开连接
该问题在本地Windows 11和Railway Linux环境中均存在。
核心代码
音频播放的核心实现:
import { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus, StreamType } from '@discordjs/voice'; import fs from 'fs'; import path from 'path'; async function playAdhanInGuild(guild, prayerName) { const voiceChannels = guild.channels.cache.filter(channel => channel.type === 2 && channel.id !== guild.afkChannelId && channel.members.size > 0 ); for (const [channelId, channel] of voiceChannels) { const connection = joinVoiceChannel({ channelId: channel.id, guildId: guild.id, adapterCreator: guild.voiceAdapterCreator, }); const player = createAudioPlayer(); const audioPath = path.resolve('./audio/adhan.mp3'); const audioStream = fs.createReadStream(audioPath); const resource = createAudioResource(audioStream, { inputType: StreamType.Arbitrary, inlineVolume: true }); if (resource.volume) { resource.volume.setVolume(1.0); } player.play(resource); connection.subscribe(player); player.on(AudioPlayerStatus.Idle, () => { connection.destroy(); }); player.on('error', error => { console.error('Player error:', error); }); } }
音频文件验证
通过ffprobe确认音频文件有效:
- 格式:MP3,320kbps,44.1kHz立体声
- 时长:45秒
- 文件存在且可读
已尝试的解决方法
- 更换加密库:从
libsodium-wrappers切换至sodium再到tweetnacl(当前使用tweetnacl) - 更换Opus编码器:从
@discordjs/opus切换至opusscript(当前使用opusscript) - 调整AudioResource配置:
// 尝试直接传入路径 createAudioResource('./audio/adhan.mp3') // 尝试显式指定输入类型(当前配置) createAudioResource(stream, { inputType: StreamType.Arbitrary }) // 尝试启用内联音量 createAudioResource(stream, { inputType: StreamType.Arbitrary, inlineVolume: true }) - 更换@discordjs/voice版本:测试0.16.1和0.17.0版本,问题依旧
- 验证权限:启用Connect、Speak、Use Voice Activity权限;机器人未被服务器或用户静音;音量滑块设为100%
- 测试不同环境:本地Windows 11和Railway Linux均无音频
- 重新创建机器人:从零创建2个不同的Discord机器人,问题相同
当前依赖包
{ "discord.js": "^14.14.1", "@discordjs/voice": "^0.17.0", "ffmpeg-static": "^5.2.0", "tweetnacl": "^1.0.3", "opusscript": "^0.0.8" }
网关意图配置
new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages ] })
疑问
- 音频设置是否存在根本性错误?
- 使用纯JavaScript库(
tweetnacl+opusscript)而非原生编译库是否会导致问题? - 有哪些诊断步骤可用于定位音频管道的故障点?
ffmpeg-static向@discordjs/voice提供FFmpeg二进制文件的方式是否存在问题?
最令人困惑的是无任何报错——所有流程看似正常,仅无音频输出。
内容的提问来源于stack exchange,提问作者Nassim Chouirfa
相关产品推荐
相关产品推荐

