0
votes

I have built a discord but using javascript I've got one command where I want the ability to search for a youtube video and play the first result in a voice channel.

I'm using the discordjs and discord-youtube-api libraries.

This code looks for the command to search. The args array is the search query

else if (command === 'search') {
      isReady = false;
      if (message.channel.type !== 'text') return;

      const { voiceChannel } = message.member;

      if (!voiceChannel) {
        return message.reply('please join a voice channel first!');
      }

      voiceChannel.join().then(connection => {
        const stream = ytdl(searchYouTubeAsync(args), { filter: 'audioonly' });
        const dispatcher = connection.playStream(stream);

        dispatcher.on('end', () => voiceChannel.leave());
        isReady = true;
      })

};

And this is the function that uses the youtube api to search for a video and return its url.

async function searchYouTubeAsync(args) {
  var video = await youtube.searchVideos(args.toString().replace(/,/g,' '));
  console.log(video.url);
  console.log(typeof String(video.url));
  return String(video.url);
}

I do get the following error message when trying the command.

(node:13141) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type object
    at Url.parse (url.js:146:11)
    at Object.urlParse [as parse] (url.js:140:13)
    at Object.exports.getURLVideoID (/Users/alexanderhoerl/Developer/discord-music-bot/node_modules/ytdl-core/lib/util.js:248:20)
    at Object.exports.getVideoID (/Users/alexanderhoerl/Developer/discord-music-bot/node_modules/ytdl-core/lib/util.js:279:20)
    at getInfo (/Users/alexanderhoerl/Developer/discord-music-bot/node_modules/ytdl-core/lib/info.js:46:17)
    at ytdl (/Users/alexanderhoerl/Developer/discord-music-bot/node_modules/ytdl-core/lib/index.js:17:3)
    at voiceChannel.join.then.connection (/Users/alexanderhoerl/Developer/discord-music-bot/index.js:89:24)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:13141) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:13141) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I guess the problem is that the music bot tries to load a stream before the the searchYouTube function has found a link therefore no valid url is provided. Though i don't know how to fix this since the function needs to be async to await the youtube search result.

2

2 Answers

0
votes

You can run that in an async function:

voiceChannel.join().then(async connection => {
  let url = await searchYouTubeAsync(args);
  let stream = ytdl(url, { filter: 'audioonly' });
  let dispatcher = connection.playStream(stream);

  dispatcher.on('end', () => voiceChannel.leave());
  isReady = true;
})
0
votes

I'm almost sure that this is unactual now. However, here is the solution:

const stream = ytdl(searchYouTubeAsync(args).toString(), { filter: 'audioonly' });

xxx.toString() hepled me when I had same trouble with ytdl.