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

Discord.js v13 @discordjs/voice 播放命令进频道但无法播放音乐求助

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

以下是代码存在的问题和对应修复方法:

  • 缺失核心依赖引入
    代码中调用了ytdl获取音频流但未引入对应依赖,首先安装ytdl-core包,再在代码顶部添加引入语句:
    const ytdl = require('ytdl-core');
    
  • 搜索结果判断逻辑错误
    当前的判断条件会导致搜索结果仅1条时直接返回空值,修改videoFinder内的返回逻辑:
    return (videoResult.videos.length > 0) ? videoResult.videos[0] : null;
    
  • 音频播放器配置错误
    导入的NoSubscriberBehavior未使用,默认配置下无订阅者时播放器会自动暂停,修改播放器创建逻辑:
    const player = createAudioPlayer({
      behaviors: {
        noSubscriber: NoSubscriberBehavior.Play
      }
    });
    
  • 方法调用顺序与异步误用
    player.play()不是异步方法,无需加await,且需要先完成订阅再触发播放,调整逻辑为:
    connection.subscribe(player);
    player.play(resource);
    
  • 缺少语音相关网关意图
    Discord.js v13需要手动开启网关意图,确认创建客户端时已添加GUILD_VOICE_STATES意图:
    const client = new Discord.Client({
      intents: [
        Discord.Intents.FLAGS.GUILDS,
        Discord.Intents.FLAGS.GUILD_MESSAGES,
        Discord.Intents.FLAGS.GUILD_VOICE_STATES
      ]
    });
    
  • 音频流配置优化
    给ytdl添加缓存参数,避免音频加载中断:
    const stream = ytdl(video.url, { 
      filter: 'audioonly',
      highWaterMark: 1 << 25
    });
    
  • 缺少FFmpeg转码依赖
    @discordjs/voice依赖FFmpeg完成音频转码,需要在运行环境中安装FFmpeg并配置环境变量,也可以直接安装ffmpeg-static包引入使用。

修正后完整代码

const ytsearch = require('yt-search');
const ytdl = require('ytdl-core');
const Discord = require('discord.js')
const {
    joinVoiceChannel,
    createAudioPlayer,
    createAudioResource,
    NoSubscriberBehavior
} = require('@discordjs/voice');


module.exports = {
    name: "play",
    description: "test command",

    async run(client, message, args) {

        const voiceChannel = message.member.voice.channel;
        if (!voiceChannel) return message.channel.send('Please connect to a voice channel!');
        if (!args.length) return message.channel.send('Please Provide Something To Play!')

        const connection = joinVoiceChannel({
            channelId: message.member.voice.channel.id,
            guildId: message.guild.id,
            adapterCreator: message.guild.voiceAdapterCreator
        });
        const videoFinder = async (query) => {
            const videoResult = await ytsearch(query);
            return (videoResult.videos.length > 0) ? videoResult.videos[0] : null;
        }
        const video = await videoFinder(args.join(' '));
        if (video) {
            const stream = ytdl(video.url, { filter: 'audioonly', highWaterMark: 1 << 25 });
            const player = createAudioPlayer({
                behaviors: {
                    noSubscriber: NoSubscriberBehavior.Play
                }
            });
            const resource = createAudioResource(stream)

            connection.subscribe(player);
            player.play(resource);

            await message.reply(`:thumbsup: Now Playing *${video.title}*`)
            
        } else {
            message.channel.send('No video results found');
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 16:45:03