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

Discord.js v13音乐机器人显示播放正常但无声音问题求助

问题排查与修复方案

核心错误点

  • 你完全错误使用了@discordjs/voice的播放流程:VoiceConnection的subscribe方法只能接收AudioPlayer实例,不能直接传入ytdl生成的音频流
  • 缺少必要的转码依赖,ytdl输出的原始音频流无法直接被语音接口识别播放
  • 大概率未开启Discord v13版本强制要求的语音相关Intents权限

修复步骤

1. 先安装缺失依赖

npm install @discordjs/opus ffmpeg-static

2. 修复代码逻辑

正确播放流程为:创建音频播放器 → 生成可识别的音频资源 → 播放器加载资源 → 语音连接订阅播放器
替换你现有代码中对应逻辑即可:

const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('@discordjs/voice');

module.exports = {
    name: 'play',
    description: 'Joins and plays a video from youtube',
    execute: async (client, msg, arg, Discord) => {
        const voiceChannel = msg.member.voice.channel;

        if (!voiceChannel) return msg.channel.send('You need to be in a channel to execute this command!');
        const permissions = voiceChannel.permissionsFor(msg.client.user);
        if (!permissions.has('CONNECT')) return msg.channel.send('You dont have the correct permissions');
        if (!permissions.has('SPEAK')) return msg.channel.send('You dont have the correct permissions');
        if (!arg.length) return msg.channel.send('You need to send the second argument!');

        // 接入语音频道
        const connection = joinVoiceChannel({
            channelId: voiceChannel.id,
            guildId: voiceChannel.guild.id,
            adapterCreator: msg.guild.voiceAdapterCreator,
        });

        // 创建音频播放器
        const player = createAudioPlayer();
        // 订阅播放器到语音连接
        connection.subscribe(player);

        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);
            return videoResult.videos.length >= 1 ? videoResult.videos[0] : null;
        }
        const keywords = arg.join(' ')
        const video = await videoFinder(keywords);

        if (video) {
            // 生成ytdl流,调整缓冲参数避免卡顿
            const stream = ytdl(video.url, { 
                filter: 'audioonly',
                highWaterMark: 1 << 25
            });
            // 创建音频资源
            const resource = createAudioResource(stream);
            // 播放器加载资源开始播放
            player.play(resource);

            // 播放结束自动退出
            player.on(AudioPlayerStatus.Idle, () => {
                connection.destroy();
                msg.channel.send('leaving channel');
            });

            await msg.reply(`:thumbsup: Now Playing *${video.title}*`)
        } else {
            msg.reply('No Results Found')
        }
    }
}

3. 检查客户端Intents配置

你初始化Discord Client的代码中,必须开启以下Intent:

const { Client, Intents } = require('discord.js');
const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_VOICE_STATES // 必须开启,否则没有语音权限
    ]
});

同时要去Discord开发者后台,对应应用的Bot设置页,开启Privileged Gateway Intents下的所有开关。

额外验证点

如果修复后仍无声音,检查:

  • 你的服务器区域是否被YouTube限制访问,可尝试更换节点
  • 更新ytdl-core到最新版本:npm install ytdl-core@latest
  • 确认机器人在语音频道没有被服务器静音

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 05:15:02