Discord 机器人使用不和谐播放器:播放 YouTube 歌曲时出错 - 提取器问题

问题描述 投票:0回答:1

我正在开发一个 Discord 机器人,使用 Discord 播放器库来播放 YouTube 上的音乐。

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

我遇到一个问题,在尝试播放歌曲时收到错误消息。

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

我尝试过:

  1. 库设置:我按照文档和教程在我的 Discord 机器人中设置了

    discord-player
    库。

  2. YouTube API 密钥:我生成了一个 YouTube API 密钥并在需要时将其添加到我的代码中。

  3. 命令执行:我使用各种 YouTube URL 和搜索查询执行

    !play
    命令来测试音乐播放。

我期待的是:

我预计,当我使用有效的 YouTube URL 或搜索查询执行

!play
命令时,机器人将成功在语音频道中播放请求的音乐,但是我遇到了
ERR_NO_RESULT
错误,表明未找到任何结果提供查询。

javascript discord.js youtube-api bots
1个回答
0
投票

根据您提供的代码片段,您似乎使用的是几年前过时的代码。 Discord Player 不需要您提供 API 密钥。最新版本的不和谐播放器要求您加载提取器,否则将无法工作。为此,您需要安装

@discord-player/extractor
库,它提供了多个提取器,允许您从不同来源提取元数据。然后你必须加载它们:

// after declaring player, put the following code
player.extractors.loadDefault();

但请记住,创建由 YouTube 支持的 Discord 音乐机器人是违反规定的。为了禁用 youtube,您可以使用以下方法加载提取器

await player.extractors.loadDefault((ext) => ext !== 'YouTubeExtractor');

这将加载除 youtube 之外的所有提取器。

您还应该参考该框架的官方文档:https://discord-player.js.org

我希望能回答您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.