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

使用discord-player开发Discord Bot:播放YouTube歌曲遇提取器错误

Discord-Player 播放YouTube音乐出现ERR_NO_RESULT错误

我正在用discord-player库开发Discord音乐Bot,代码如下:

const { Client, GatewayIntentBits } = require('discord.js');
const { Player } = require('discord-player');
 
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.GuildVoiceStates,
    GatewayIntentBits.MessageContent, // Add MESSAGE CONTENT INTENT
  ],
});
 
const player = new Player(client, {
  youtubeKey: 'AIzaSyDjImSQ-3JMrrjG70hS2r4Hp-vO3W70IgQ', // Replace with your YouTube API key
});
 
client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});
 
const prefix = '!'; // Command prefix
 
client.on('messageCreate', async (message) => {
  if (!message.guild) return;
 
  if (message.content.startsWith(prefix)) {
    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();
 
    if (command === 'play') {
      const query = args.join(' ');
 
      if (!query) {
        message.channel.send('Please provide a valid YouTube URL or search query.');
        return;
      }
 
      const voiceChannel = message.member.voice.channel;
      if (!voiceChannel) {
        message.channel.send('You need to be in a voice channel to use this command.');
        return;
      }
 
      try {
        const song = await player.play(voiceChannel, query);
        message.channel.send(`Now playing: ${song.name}`);
      } catch (error) {
        console.error('Error playing the song:', error);
        message.channel.send('An error occurred while playing the song.');
      }
    }
    // Add more command handling here...
  }
});
 
client.login('MTE0ODExNTcwMTg2MTcxNjAwOA.GFXTBm.Y8eCJ6E5fH0S4ySsMyoJMleCp1VmEbfRMJpYJY'); // Replace with your bot token

执行!play命令时触发错误:

Error playing the song: ERR_NO_RESULT: No results found for [YouTube URL] (Extractor: N/A).

已做排查:

  • 按文档完成discord-player配置
  • 配置了有效YouTube API密钥
  • 测试多个YouTube链接和搜索词

预期Bot能在语音频道播放音乐,但实际提示未找到查询结果。


解决方法

1. 安装并注册YouTube提取器

discord-player v6及以上版本不再内置YouTube提取器,需手动安装@discord-player/extractor:

npm install @discord-player/extractor

在代码中注册提取器:

const { Player } = require('discord-player');
const { YouTubeExtractor } = require('@discord-player/extractor');

// 初始化player后添加注册代码
player.extractors.register(YouTubeExtractor, {
    apiKey: '你的YouTube API密钥' // 可在此处单独配置,或沿用player初始化时的youtubeKey
});

2. 验证API密钥有效性

  • 登录Google Cloud控制台,确认YouTube Data API v3已启用
  • 检查密钥的配额使用情况,确保未超出每日请求限制
  • 确认密钥未设置IP访问限制(若Bot部署在云服务器,需允许对应IP)

3. 调整播放逻辑的返回值处理

新版本discord-player中player.play返回的是队列对象,需通过track属性获取歌曲信息:

try {
  const result = await player.play(voiceChannel, query);
  message.channel.send(`Now playing: ${result.track.title}`);
} catch (error) {
  console.error('Error playing the song:', error);
  message.channel.send('An error occurred while playing the song.');
}

4. 排查网络环境

若Bot部署在无法直接访问YouTube的环境(如国内服务器),需配置代理,或更换支持本地访问的提取器。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 12:03:06