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

@discordjs/voice库运行无报错 机器人进语音频道无声音求助

@discordjs/voice 无音频输出问题修复方案

代码层面可直接修复的问题

  • 缺失ytdl依赖导入:你使用了ytdl()拉取音频流,但代码中没有对应导入语句,先安装ytdl-core,再在代码头部添加导入:
const ytdl = require('ytdl-core');
  • 重复声明播放器实例:文件顶部已经初始化了全局播放器const player = createAudioPlayer(),但在播放逻辑块中又重复声明了局部player变量,导致资源调度异常,删除if(video)块内的const player = createAudioPlayer()这一行即可。
  • 音频资源配置不完整:@discordjs/voice默认不会自动适配ytdl返回的流格式,需要修改createAudioResource的配置,同时要先从库中导入StreamType:
    首先修改导入语句:
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus, StreamType } = require('@discordjs/voice');

再修改音频资源创建逻辑:

const resource = createAudioResource(stream, {
  inlineVolume: true,
  inputType: StreamType.Arbitrary
})
  • 语音连接参数缺失:加入频道时显式指定机器人的语音状态,避免权限导致的静音问题,修改joinVoiceChannel配置:
var connection = joinVoiceChannel({
    channelId: voiceChannel.id,
    guildId: voiceChannel.guild.id,
    adapterCreator: msg.channel.guild.voiceAdapterCreator,
    selfDeaf: false,
    selfMute: false
});

环境配置排查项

  • 确认Discord开发者面板中,你的机器人已经开启GUILD_VOICE_STATES网关意图,无该意图会导致语音状态同步失败,无法输出音频。
  • 确认已安装全套音频依赖:执行npm install @discordjs/opus ffmpeg-static安装必需的解码依赖,二者缺一不可。
  • 若修改后仍无声音,可调整ytdl的缓冲区配置,避免流加载中断:
const stream = ytdl(video.url, { filter: 'audioonly', highWaterMark: 1 << 25 });

修复后完整代码

const ytSearch = require('yt-search');
const ytdl = require('ytdl-core');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus, StreamType } = require('@discordjs/voice');
const player = createAudioPlayer()
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('I don\'t have permission to join this voice channel');
        if (!permissions.has('SPEAK')) return msg.channel.send('I don\'t have permission to speak in this voice channel');
        if (!arg.length) return msg.channel.send('You need to specify the song you want to play');

        var connection = joinVoiceChannel({
            channelId: voiceChannel.id,
            guildId: voiceChannel.guild.id,
            adapterCreator: msg.channel.guild.voiceAdapterCreator,
            selfDeaf: false,
            selfMute: false
        });

        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) {
            const stream = ytdl(video.url, { filter: 'audioonly', highWaterMark: 1 << 25 });
            const resource = createAudioResource(stream, {
                inlineVolume: true,
                inputType: StreamType.Arbitrary
            })
            player.play(resource)
            connection.subscribe(player);
            await msg.reply(`:thumbsup: Now Playing ***${video.title}***`)
        }
        else{
            msg.reply('No Results Found')
        }
    }
}

内容的提问来源于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.05 11:36:03