N
N
Not Coder2020-11-12 17:04:07
Node.js
Not Coder, 2020-11-12 17:04:07

How to search youtube video without youtube data api?

Hello everyone. I have quotas for YouTube api. How can I request a link to a video without YouTube data api?
Here is my code:

const { MessageEmbed } = require("discord.js");
const YouTubeAPI = require("simple-youtube-api");

let YOUTUBE_API_KEY;
try {
  const config = require("../config.json");
  YOUTUBE_API_KEY = config.YOUTUBE_API_KEY;
} catch (error) {
  YOUTUBE_API_KEY = process.env.YOUTUBE_API_KEY;
}
const youtube = new YouTubeAPI(YOUTUBE_API_KEY);

module.exports = {
  name: "search",
  aliases: ["se"],
  description: "Поиск всех вариантов музыки для проигрования",
  async execute(message, args) {
    if (!args.length)
      return message
        .reply(`Использование: ${message.client.prefix}${module.exports.name} <Название музыки>`)
        .catch(console.error);
    if (message.channel.activeCollector)
      return message.reply("Поисковой запрос уже работает в этом канале!");
    if (!message.member.voice.channel)
      return message.reply("Вы должны зайти в голосовой канал первее!").catch(console.error);

    const search = args.join(" ");

    let resultsEmbed = new MessageEmbed()
      .setTitle(`**Напишите номер для проигроания**`)
      .setDescription(`Результаты для: **${search}**`)
      .setColor("#FF00FF")
      .setFooter(`${message.guild.name} | ${message.author.username} | Поисковой запрос закроется автоматически через 25 секунд`)
      .setTimestamp();

    try {
      const results = await youtube.searchVideos(search, 10);
      results.map((video, index) => resultsEmbed.addField(video.shortURL, `${index + 1}. ${video.title}`));

      let resultsMessage = await message.channel.send(resultsEmbed);

      function filter(msg) {
        const pattern = /^[0-9]{1,2}(\s*,\s*[0-9]{1,2})*$/g;
        return pattern.test(msg.content);
      }

      message.channel.activeCollector = true;
      const response = await message.channel.awaitMessages(filter, { max: 1, time: 25000, errors: ["time"] });
      const reply = response.first().content;

      if (reply.includes(",")) {
        let songs = reply.split(",").map((str) => str.trim());

        for (let song of songs) {
          await message.client.commands
            .get("play")
            .execute(message, [resultsEmbed.fields[parseInt(song) - 1].name]);
        }
      } else {
        const choice = resultsEmbed.fields[parseInt(response.first()) - 1].name;
        message.client.commands.get("play").execute(message, [choice]);
      }

      message.channel.activeCollector = false;
      resultsMessage.delete().catch(console.error);
      response.first().delete().catch(console.error);
    } catch (error) {
      console.error(error);
      message.channel.activeCollector = false;
      message.reply(error.message).catch(console.error);
    }
  }
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question