I'm trying to create a command that searches youtube for a video URL. When I run the command on its own, it sends the embed telling you to narrow your search. However, when I run the command + a query, it logs most of the video info in the console, but doesn't state the URL to the video itself, as well as not sending anything to the server. It doesn't log errors, and the bot stays online. How to fix this issue?
const opts = {
maxResults: 25,
key: '(youtube api key)',
type: 'video'
};
const search = require('youtube-search')
bot.on('message', async message => {
if(message.author.bot) return;
if(message.content.toLowerCase() === 'a!play') {
let embed = new Discord.MessageEmbed()
.setColor("#73ffdc")
.setDescription("Please enter a search query. Remember to narrow down your search.")
.setTitle("YouTube Search API");
let embedMsg = await message.channel.send(embed);
let filter = m => m.author.id === message.author.id;
let query = await message.channel.awaitMessages(filter, { max: 1 });
let results = await search(query.first().content, opts).catch(err => console.log(err));
if(results) {
let youtubeResults = results.results;
let i =0;
let titles = youtubeResults.map(result => {
i++;
return i + ") " + result.title;
});
console.log(titles);
message.channel.send({
embed: {
title: 'Select which song you want by typing the number',
description: titles.join("\n")
}
}).catch(err => console.log(err));
filter = m => (m.author.id === message.author.id) && m.content >= 1 && m.content <= youtubeResults.length;
let collected = await message.channel.awaitMessages(filter, { maxMatches: 1 });
let selected = youtubeResults[collected.first().content - 1];
embed = new Discord.MessageEmbed()
.setTitle(`${selected.title}`)
.setURL(`${selected.link}`)
.setDescription(`${selected.description}`)
.setThumbnail(`${selected.thumbnails.default.url}`);
message.channel.send(embed);
}
}
});