title: ytdl: The "url" argument must be of type string. Received type undefined
body: <p>I'm working on a Discord bot using Discord.js and ytdl. However, whenever I type <code>?play</code> and my play command is executed, this error is thrown:</p>
  
  <blockquote>
    <pre class="lang-js prettyprint-override"><code>(node:15840) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined
      at URL.parse(url.js:154:11)
      at Object.urlParse [as parse] (url.js:148:13)
      at Object.exports.getURLVideoID (C:\Users\MauSc\Desktop\[MENU DOCS] Bot\node_modules\ytdl-core\lib\util.js:273:22)
      at Function.exports.validateURL(C:\Users\MauSc\Desktop\[MENU DOCS] Bot\node_modules\ytdl-core\lib\util.js:328:20)
      at Object.module.exports.run (C:\Users\MauSc\Desktop\[MENU DOCS] Bot\commands\play.js:18:22)
      at Client.bot.on (C:\Users\MauSc\Desktop\[Menu DOCS] Bot\index.js:51:38)
      at Client.emit (events.js:198:13)
      at MessageCreateHandler.handle (C:\Users\MauSc\Desktop\[MENU DOCS] Bot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
      at WebSocketPacketManager.handle (C:\Users\MauSc\Desktop\[MENU DOCS] Bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
      at WebSocketConnection.onPacket (C:\Users\MauSc\Desktop\[MENU DOCS] Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
  (node:15840) 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:15840) [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.
  </code></pre>
  </blockquote>
  
  <p>The code is all copied from another bot I made, so I'm not sure why the error suddenly appears.</p>
  
  <p>This is the code:</p>
  
  <pre><code>const Discord = require("discord.js");
  const ytdl = require('ytdl-core');
  const streamOptions ={
      seek: 0,
      volume: 1
  }
  
  const bot = new Discord.Client({disableEveryone: true});
  
  var musicUrls = [];
  
  bot.on("message", async message =&gt; {
  
  module.exports.run = async (bot, message, args) =&gt; {
               let url = args[1];
               let voiceChannel = message.guild.channels.find(channel =&gt; channel.name === 'Musik Bot');
  
               if(ytdl.validateURL(url))
               {
                  console.log("Valid URL");
                  var flag = musicUrls.some(element =&gt; element === url);
                  if(!flag)
                  {
                      musicUrls.push(url);
                      if(voiceChannel != null)
                      {
  
                          if(voiceChannel.connection)
                          {
                              console.log("Connection exists");
                              const embed = new Discord.RichEmbed();
                              embed.setAuthor(bot.user.username, bot.user.displayAvatarURL);
                              embed.setDescription("Du hast erfolgreich einen Track zu der Playlist hinzugefügt!");
                              message.channel.send(embed);
                          }
                          else {
                              try {
                                  const voiceConnection = await voiceChannel.join();
                                  await playSong(message.channel, voiceConnection, voiceChannel);
                              }
                              catch(ex)
                              {
                                  console.log(ex);
                               }
                           }
                      }
  
                   }
               }
  }
       async function playSong(messageChannel, voiceConnection, voiceChannel)
  {
      const stream = ytdl(musicUrls[0], { filter : 'audioonly'});
      const dispatcher = voiceConnection.playStream(stream, streamOptions);
  
      dispatcher.on('end', () =&gt; {
          musicUrls.shift();
  
          if(musicUrls.length == 0)
              voiceChannel.leave();
          else
          {
              setTimeout(() =&gt; {
                   playSong(messageChannel, voiceConnection, voiceChannel);
              }, 1000);
          }
      });
      try {
          if(!message.content.startsWith(prefix)) return;
          let commandfile = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)))
          console.log(commandfile);
          if (commandfile) commandfile.run(bot, message, args)
      } catch (error) {
          console.error(error);
      }
  }},
  
  
  module.exports.config = {
      name: "play",
      aliases: []
  });
  </code></pre>