SHARE
TWEET

Untitled

supg Feb 21st, 2020 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http = require('http');
  2. http.createServer(function(request,responce)
  3.                   {
  4.   responce.writeHead(200, {'Content-Type': 'text/plain'});
  5. }).listen(3000);
  6.  
  7. const { Client, Util } = require('discord.js');
  8. const { TOKEN, PREFIX, GOOGLE_API_KEY, COLOR, STATUS, CMDNAME } = require('./config');
  9. const YouTube = require('simple-youtube-api');
  10. const ytdl = require('ytdl-core');
  11. const Discord = require("discord.js");
  12.  
  13. const client = new Client({ disableEveryone: true });
  14.  
  15. const youtube = new YouTube(GOOGLE_API_KEY);
  16.  
  17. const queue = new Map();
  18.  
  19. client.on('message', message => {
  20.   if (message.content === `${PREFIX}ping`) {
  21.     message.reply('pong');
  22.   }
  23. });
  24.  
  25. client.on('message', message => {
  26.   if (message.content === `${PREFIX}help`) {
  27.     var embed = new Discord.RichEmbed()
  28.     .setTitle('MUSIC COMMANDS')
  29.     .setColor(`${COLOR}`)
  30.     .setDescription(`${PREFIX}play, ${PREFIX}skip, ${PREFIX}np, ${PREFIX}stop, ${PREFIX}resume, ${PREFIX}queue, ${PREFIX}pause, ${PREFIX}clean`);
  31.     message.channel.send(embed);
  32.   }
  33. });
  34.  
  35.  
  36. client.on('warn', console.warn);
  37.  
  38. client.on('error', console.error);
  39.  
  40.  
  41. client.on('ready', () => {
  42.      
  43.  
  44.   console.log(`${client.user.tag} Yo this ready!`)
  45.         client.user.setActivity(`${STATUS}`);
  46.    
  47. });
  48.  
  49. client.on('disconnect', () => console.log('I just disconnected, making sure you know, I will reconnect now...'));
  50.  
  51. client.on('reconnecting', () => console.log('I am reconnecting now!'));
  52.  
  53. client.on('message', async msg => { // eslint-disable-line
  54.     if (msg.author.bot) return undefined;
  55.     if (!msg.content.startsWith(PREFIX)) return undefined;
  56.  
  57.     const args = msg.content.split(' ');
  58.     const searchString = args.slice(1).join(' ');
  59.     const url = args[1] ? args[1].replace(/<(.+)>/g, '$1') : '';
  60.     const serverQueue = queue.get(msg.guild.id);
  61.  
  62.     let command = msg.content.toLowerCase().split(' ')[0];
  63.     command = command.slice(PREFIX.length)
  64.  
  65.     if (command === 'play') {
  66.         const voiceChannel = msg.member.voiceChannel;
  67.         if (!voiceChannel) return msg.channel.send('I\'m sorry but you need to be in a voice channel to play music!');
  68.         const permissions = voiceChannel.permissionsFor(msg.client.user);
  69.         if (!permissions.has('CONNECT')) {
  70.             return msg.channel.send('I cannot connect to your voice channel, make sure I have the proper permissions!');
  71.         }
  72.         if (!permissions.has('SPEAK')) {
  73.             return msg.channel.send('I cannot speak in this voice channel, make sure I have the proper permissions!');
  74.         }
  75.  
  76.         if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
  77.             const playlist = await youtube.getPlaylist(url);
  78.             const videos = await playlist.getVideos();
  79.             for (const video of Object.values(videos)) {
  80.                 const video2 = await youtube.getVideoByID(video.id); // eslint-disable-line no-await-in-loop
  81.                 await handleVideo(video2, msg, voiceChannel, true); // eslint-disable-line no-await-in-loop
  82.             }
  83.           var embed = new Discord.RichEmbed()
  84.                 .setTitle("Song Selection")
  85.                 .setDescription(`✅ Song has been added to the queue!`)
  86.                 .setColor(`${COLOR}`)
  87.             return msg.channel.send(embed);
  88.         } else {
  89.             try {
  90.                 var video = await youtube.getVideo(url);
  91.             } catch (error) {
  92.                 try {
  93.                 msg.react('✅');
  94.                     var videos = await youtube.searchVideos(searchString, 10);
  95.                     let index = 0;
  96.                     // eslint-disable-next-line max-depth
  97.                  
  98.                  
  99.                     var video = await youtube.getVideoByID(videos[1].id);
  100.                 } catch (err) {
  101.                     console.error(err);
  102.                     return msg.channel.send('🆘 I could not obtain any search results.');
  103.                 }
  104.             }
  105.             return handleVideo(video, msg, voiceChannel);
  106.         }
  107.     } else if (command === 'skip') {
  108.     if (!msg.member.hasPermission("ADMINISTRATOR")) {
  109.       return msg.reply('YOU DIDENT HAVE ADMINISTRATOR PERMISSIONS!')
  110.     }
  111.    
  112.    
  113.    
  114.         if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  115.         if (!serverQueue) return msg.channel.send('There is nothing playing that I could skip for you.');
  116.         serverQueue.connection.dispatcher.end('Skip command has been used!');
  117.     const embed = new Discord.RichEmbed()
  118.     .setTitle('Song')
  119.     .setColor(`${COLOR}`)
  120.     .setDescription('✅ Successfully skipped the song');
  121.     msg.channel.send(embed);
  122.    
  123.         return undefined;
  124.     } else if (command === 'stop') {
  125.         if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  126.         if (!serverQueue) return msg.channel.send('There is nothing playing that I could stop for you.');
  127.         serverQueue.songs = [];
  128.         serverQueue.connection.dispatcher.end('Stop command has been used!');
  129.         msg.reply("**bot has been stopped !**");
  130.         return undefined;
  131.     } else if (command === '9090090909090909090090090') {
  132.         if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  133.         if (!serverQueue) return msg.channel.send('There is nothing playing.');
  134.         if (!args[1]) return msg.channel.send(`The current volume is: **${serverQueue.volume}**`);
  135.         serverQueue.volume = args[1];
  136.         serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 4);
  137.         return msg.channel.send(`I set the volume to: **${args[1]}**`);
  138.     } else if (command === 'clean') {
  139.     if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  140.         if (!serverQueue) return msg.channel.send('Quene is already empty');
  141.      msg.channel.send(`Cleared the queue`).then(m => serverQueue.connection.dispatcher.clear());
  142.   } else if (command === 'duration') {
  143.     if (!msg.member.voiceChannel) return msg.channel.send('Please join voice channel first');
  144.     if (!serverQueue) return msg.channel.send('There is nothing playing');
  145.                   let data = await Promise.resolve(ytdl.getInfo(serverQueue.songs[0].url));
  146.         let duration = (data.length_seconds * 1000).toFixed(0);
  147.     var seconds = Math.floor((duration / 1000) % 60),
  148.         minutes = Math.floor((duration / (1000 * 60)) % 60),
  149.         hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
  150.  
  151.     hours = (hours < 10) ? "0" + hours : hours;
  152.     minutes = (minutes < 10) ? "0" + minutes : minutes;
  153.     seconds = (seconds < 10) ? "0" + seconds : seconds;
  154.  
  155.     return msg.channel.send(`Song duration is - ${hours}h:${minutes}m:${seconds}s`);
  156. }
  157.  
  158.  
  159.   else if (command === 'np') {
  160.     var embed = new Discord.RichEmbed()
  161.     .setTitle("Song Detail")
  162.     .setDescription(`🎶 \`Now playing:\` **${serverQueue.songs[0].title}**`)
  163.     .setColor(`${COLOR}`)
  164.         if (!serverQueue) return msg.channel.send('There is nothing playing.');
  165.         return msg.channel.send(embed);
  166.  
  167. } else if (command === 'queue') {
  168.         if (!serverQueue) return msg.channel.send('There is nothing playing.');
  169.         var embed = new Discord.RichEmbed()
  170.                 .setTitle("Song Queue")
  171.                 .setDescription(`${serverQueue.songs.map(song => `**• ** ${song.title}`).join('\n')}
  172.  
  173. 🎵 \`Now playing:\` **${serverQueue.songs[0].title}**`)
  174.                 .setColor(`${COLOR}`)
  175.     return msg.channel.send(embed);
  176.     } else if (command === 'pause') {
  177.         if (serverQueue && serverQueue.playing) {
  178.             serverQueue.playing = false;
  179.             serverQueue.connection.dispatcher.pause();
  180.             var embed = new Discord.RichEmbed()
  181.                 .setTitle("Song")
  182.                 .setDescription(`⏸ Paused the music for you!`)
  183.                 .setColor(`${COLOR}`)
  184.             msg.channel.send(embed)
  185.          
  186.            }
  187.           } else if (command === 'resume') {
  188.         if (serverQueue && !serverQueue.playing) {
  189.             serverQueue.playing = true;
  190.             serverQueue.connection.dispatcher.resume();
  191.             var embed = new Discord.RichEmbed()
  192.                 .setTitle("Song")
  193.                 .setDescription(`▶ Resumed the music for you!`)
  194.                 .setColor(`${COLOR}`)
  195.             msg.channel.send(embed)
  196.         }
  197.     }
  198.  
  199. });
  200.  
  201.  
  202. async function handleVideo(video, msg, voiceChannel, playlist = false) {
  203.     const serverQueue = queue.get(msg.guild.id);
  204.     console.log(video);
  205.     const song = {
  206.         id: video.id,
  207.         title: Util.escapeMarkdown(video.title),
  208.         url: `https://www.youtube.com/watch?v=${video.id}`
  209.     };
  210.     if (!serverQueue) {
  211.         const queueConstruct = {
  212.             textChannel: msg.channel,
  213.             voiceChannel: voiceChannel,
  214.             connection: null,
  215.             songs: [],
  216.             volume: 10,
  217.             playing: true
  218.         };
  219.         queue.set(msg.guild.id, queueConstruct);
  220.  
  221.         queueConstruct.songs.push(song);
  222.  
  223.         try {
  224.             var connection = await voiceChannel.join();
  225.             queueConstruct.connection = connection;
  226.             play(msg.guild, queueConstruct.songs[0]);
  227.         } catch (error) {
  228.             console.error(`I could not join the voice channel: ${error}`);
  229.             queue.delete(msg.guild.id);
  230.             return msg.channel.send(`I could not join the voice channel: ${error}`);
  231.         }
  232.     } else {
  233.         serverQueue.songs.push(song);
  234.         console.log(serverQueue.songs);
  235.         if (playlist) return undefined;
  236.     var embed = new Discord.RichEmbed()
  237.                 .setTitle("Song Selection")
  238.                 .setDescription(`✅ Song has been added to the queue!`)
  239.                 .setColor(`${COLOR}`)
  240.          return msg.channel.send(embed);
  241.     }
  242.     return undefined;
  243. }
  244.  
  245. function play(guild, song) {
  246.     const serverQueue = queue.get(guild.id);
  247.  
  248.     if (!song) {
  249.         serverQueue.voiceChannel.leave();
  250.         queue.delete(guild.id);
  251.         return;
  252.     }
  253.     console.log(serverQueue.songs);
  254.  
  255.     const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  256.         .on('end', reason => {
  257.             if (reason === 'Stream is not generating quickly enough.')
  258.       console.log('Song ended');
  259.        else console.log(reason);
  260.             serverQueue.songs.shift();
  261.             play(guild, serverQueue.songs[0]);
  262.              
  263.         })
  264.         .on('error', error => console.error(error));
  265.     dispatcher.setVolumeLogarithmic(serverQueue.volume / 10);
  266.  
  267.     var embed = new Discord.RichEmbed()
  268.         .setTitle("Song Selection")
  269.         .setDescription(`🎵 \`Start playing:\` **${song.title}**`)
  270.         .setColor(`${COLOR}`)
  271.     serverQueue.textChannel.send(embed);
  272. }
  273.          
  274.  
  275.  
  276. client.login(TOKEN);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top