text: const ytdl = require('ytdl-core-discord');
  var scrapeYt = require("scrape-yt");
  const discord = require('discord.js')
  const express = require("express")
  const app = express()
  const db = require('quick.db');
  
  
  const Discord = require("discord.js");
  const fs = require("fs");
  
  const client = new Discord.Client();
  
  
  
  
  exports.run = async (client, message, args) => {
  
      if(!args[0]) return message.channel.send('Nisi navedel naslova pesmi za predvajanje!')
      let channel = message.member.voice.channel;
      if(!channel) return message.channel.send('Za predvajanje glasbe moraš biti v klicu!')
  
      if (!channel.permissionsFor(message.client.user).has("CONNECT")) return message.channel.send('Volta Media nima dovoljenja za pridružitev v klic')
      if (!channel.permissionsFor(message.client.user).has("SPEAK"))return message.channel.send('Volta Media nima dovoljenja za govor')
  
  
        
      const server = message.client.queue.get(message.guild.id);
      let video = await scrapeYt.search(args.join(' '))
      let result = video[0]
  
      client.on('voiceStateUpdate', (oldState, newState) => {
          // check if someone connects or disconnects
          if (oldState.channelID === null || typeof oldState.channelID == 'undefined') return;
          // check if the bot is disconnecting
          if (newState.id !== client.user.id) return;
          // clear the queue
          return message.client.queue.delete(message.guild.id);
  
      });
  
  
  
      const song = {
          id: result.id,
          title: result.title,
          duration: result.duration,
          thumbnail: result.thumbnail,
          upload: result.uploadDate,
          views: result.viewCount,
          requester: message.author,
          channel: result.channel.name,
          channelurl: result.channel.url
        };
  
      var date = new Date(0);
      date.setSeconds(song.duration);
      var timeString = date.toISOString().substr(11, 8);
  
        if (server) {
          server.songs.push(song);
          console.log(server.songs);
                  const connection = await channel.join();
          let embed = new discord.MessageEmbed()
          .setTitle('Dodano na seznam predvajanja')
          .setColor('#FF8000')
          .addField('Naslov', song.title, true)
          .setThumbnail(song.thumbnail)
          .addField('Ogledi', song.views, true)
          .addField('Ukaz podan s strani', song.requester, true)
          .addField('Trajanje', timeString, true)
          return message.channel.send(embed)
  
  
      }
  
      const queueConstruct = {
          textChannel: message.channel,
          voiceChannel: channel,
          connection: null,
          songs: [],
          volume: 2,
          playing: true
      };
      
      message.client.queue.set(message.guild.id, queueConstruct);
      queueConstruct.songs.push(song);
  
  
      const play = async song => {
        
          const queue = message.client.queue.get(message.guild.id);
          const connection = await channel.join();
          if (!song) {
              queue.voiceChannel.leave();
              message.client.queue.delete(message.guild.id);
              message.channel.send('Na seznamu predvajanja ni več skladb, zato odhajam iz klica!')
              return;
          }
  
          const dispatcher = queue.connection.play(await ytdl(`https://youtube.com/watch?v=${song.id}`, {
              filter: format => ['251'],
              highWaterMark: 1 << 25
          }), {
              type: 'opus'
          })
              .on('finish', () => {
                  queue.songs.shift();
                  play(queue.songs[0]);
              })
              .on('error', error => console.error(error));
          dispatcher.setVolumeLogarithmic(queue.volume / 5);
          let noiceEmbed = new discord.MessageEmbed()
          .setTitle('Začelo predvajati')
          .setThumbnail(song.thumbnail)
          .addField('Naslov', song.title, true)
          .addField('Ukaz podan s strani', song.requester, true)
          .addField('Ogledi', song.views, true)
          .addField('Trajanje', timeString, true)
          queue.textChannel.send(noiceEmbed);
  
      };
  
  
      try {
          const connection = await channel.join();
          queueConstruct.connection = connection;
          play(queueConstruct.songs[0]);
  
      } catch (error) {
          console.error(`VOLTA MEDIA nima dovoljenja za vstop v klic`);
          message.client.queue.delete(message.guild.id);
          await channel.leave();
          return message.channel.send(`VOLTA MEDIA nima dovoljenja za vstop v klic: ${error}`);
      }
  }