title: Discord bot connection is not defined
body: <p>I'm trying to make a discord music bot with a queue feature and I keep getting the following error.
  How do I define the connection?</p>
  <p>I have tried adding the line</p>
  <pre><code>const connection = await message.member.voice.channel.join();
  </code></pre>
  <p>to define connection but when i add that i get a different error saying that connection is already declared.</p>
  <pre><code>(node:14724) UnhandledPromiseRejectionWarning: ReferenceError: connection is not defined
      at Client.&lt;anonymous&gt; (E:\Crashbot\index.js:493:14)
      at Client.emit (events.js:327:22)
      at MessageCreateAction.handle (E:\Crashbot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
      at Object.module.exports [as MESSAGE_CREATE] (E:\Crashbot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
      at WebSocketManager.handlePacket (E:\Crashbot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
      at WebSocketShard.onPacket (E:\Crashbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
      at WebSocketShard.onMessage (E:\Crashbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
      at WebSocket.onMessage (E:\Crashbot\node_modules\ws\lib\event-target.js:125:16)
      at WebSocket.emit (events.js:315:20)
      at Receiver.receiverOnMessage (E:\Crashbot\node_modules\ws\lib\websocket.js:797:20)
  (node:14724) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
  (node:14724) [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>
  <pre><code>var servers = {};
  
  function play(connection, message) {
      var server = servers[message.guild.id];
  
      //const connection = await message.member.voice.channel.join();
  
      const dispatcher = connection.play(ytdl(server.queue[0], { filter: 
          &quot;audioonly&quot; }));
  
      server.dispatcher = connection.play(ytdl(server.queue[0], { filter: 
      &quot;audioonly&quot; })).then(()=&gt; {
          playing = true;
      });
  
  
      dispatcher.on('error', console.error);
  
      server.dispatcher.setVolume(0.2);
  
      server.dispatcher.on(&quot;end&quot;, function () {
  
          server.queue.shift();
  
      if (server.queue[0]) play(connection, message);
      else connection.disconnect().then(()=&gt; {
          playing = false;
      });
  
  });
  }
  
  
  
  Crashbot.on('message', async message =&gt;{
  
      //let args = message.content.substring(PREFIX.length).split(&quot; &quot;);
      let args = message.content.toString().substring(PREFIX.length).split(&quot; &quot;);
  
  switch (args[0]) {
   case &quot;play&quot;:
          if (!args[1]) {
              message.reply(`You need to put a link after ${PREFIX}play to add a song to the queue`)
              return;
          }
          if (!message.member.voice.channel) {
              message.channel.send(&quot;You must be in a voice channel to use this command&quot;)
              return;
          }
          if (!servers[message.guild.id]) servers[message.guild.id] = {
              queue: []
          }
          var server = servers[message.guild.id];
  
          server.queue.push(args[1]);
          play(connection, message);
  
  
          if (!message.guild.voiceConnection) message.member.voice.channel.join().then(function (connection) {
                  play(connection, message);
          });
          break;
  
   case &quot;skip&quot;:
          var server = servers[message.guild.id];
  
          if (server.dispatcher) server.dispatcher.end();
          break;
   case &quot;queue&quot;:
          message.channel.send(queue);
          break;
  
   case &quot;stop&quot;:
          var server = servers[message.guild.id];
  
          if (message.guild.voiceConnection) message.guild.voiceConnection.disconnect().then(()=&gt; {
              playing = false;
          });
          break;
  }
  })
  </code></pre>