first, tbh Im still not familiar with all of this but I still decided to give it a try, so pls be patient with me here.
the package Im using here is discord.js Commando and discord-youtube-api. before I add the play function, I decided to see if I can get the search function right. But everytime I tried to search for something, the result is a total nonsense (not even a single relation to the video I tried to search) and its only giving me one result (watch?v=-yDd2D5OHyc) and nothing else.
class SearchCommand extends Commando.Command {
constructor(client){
super(client,{
name: 'search',
group:'music',
memberName:'search',
description: 'Search a Youtube video',
args: [
{
key: 'text',
prompt: 'Input the video name?',
type: 'string'
}
]
});
}
async run (message, args, {text})
{
message.channel.send(args)
message.channel.send(text)
var video = await youtube.searchVideos(args.toString().replace(/,/g,' '));
message.channel.send(video.url);
message.channel.send(video.thumbnail);
message.channel.send(video.length);
}
}
module.exports = SearchCommand;
thanks slothiful for the answer to my problem. while your solution is correct for the questioned problem, another error occured
(node:14492) UnhandledPromiseRejectionWarning: TypeError: Parameter "url" must be a string, not object
But, the presented solution is not wrong. It's just incomplete. Fortunately, I've found the solution for the new error, and combined with your solution, I can make it work like this
async run(message, {text})
{
const args = text.split(/ +/g);
const video = await youtube.searchVideos(args);
console.log(args.toString());
console.log(args.join(' '));
console.log(video.url)
const streamOptions = { seek: 0, volume: 1 };
if (message.member.voiceChannel){
message.member.voiceChannel.join()
.then(
connection => {
const stream = yt(video.url, { filter : 'audioandvideo', quality : 'highestaudio', lang : 'en'});
const dispatcher = connection.playStream(stream, streamOptions);
})
}
else {
message.channel.send("Please join a voice channel before I can play it for you !")
}