SHARE
TWEET

Untitled

a guest Jan 24th, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js');
  2. const YouTube = require('simple-youtube-api');
  3. const ytdl = require('ytdl-core');
  4. const youtube = new YouTube(`YTAPI`);
  5. const queue = new Map();
  6. const client = new Discord.Client();
  7. const settings = require('./settings.json');
  8. var prefix = "!"
  9.  
  10. var servers = {};
  11. client.on("message", async message => {
  12.     var args = message.content.substring(prefix.length).split(" ");
  13.     if (!message.content.startsWith(prefix)) return;
  14.   var searchString = args.slice(1).join(' ');
  15.     var url = args[1] ? args[1].replace(/<(.+)>/g, '$1') : '';
  16.     var serverQueue = queue.get(message.guild.id);
  17.     switch (args[0].toLowerCase()) {
  18. case "play":
  19.     var voiceChannel = message.member.voiceChannel;
  20.         if (!voiceChannel) return message.channel.send('I\'m sorry but you need to be in a voice channel to play music!');
  21.         var permissions = voiceChannel.permissionsFor(message.client.user);
  22.         if (!permissions.has('CONNECT')) {
  23.             return message.channel.send('I cannot connect to your voice channel, make sure I have the proper permissions!');
  24.         }
  25.         if (!permissions.has('SPEAK')) {
  26.             return message.channel.send('I cannot speak in this voice channel, make sure I have the proper permissions!');
  27.         }
  28.       if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
  29.             var playlist = await youtube.getPlaylist(url);
  30.             var videos = await playlist.getVideos();
  31.             for (const video of Object.values(videos)) {
  32.                 var video2 = await youtube.getVideoByID(video.id); // eslint-disable-line no-await-in-loop
  33.                 await handleVideo(video2, message, voiceChannel, true); // eslint-disable-line no-await-in-loop
  34.             }
  35.             return message.channel.send(`✅ Playlist: **${playlist.title}** has been added to the queue!`);
  36.         } else {
  37.             try {
  38.                 var video = await youtube.getVideo(url);
  39.             } catch (error) {
  40.                 try {
  41.                     var videos = await youtube.searchVideos(searchString, 10);
  42.                     var index = 0;
  43.                     message.channel.send(`
  44. __**Song selection:**__
  45. ${videos.map(video2 => `**${++index} -** ${video2.title}`).join('\n')}
  46. Please provide a value to select one of the search results ranging from 1-10.
  47.                     `);
  48.                     // eslint-disable-next-line max-depth
  49.                     try {
  50.                         var response = await message.channel.awaitMessages(message2 => message2.content > 0 && message2.content < 11, {
  51.                             maxMatches: 1,
  52.                             time: 10000,
  53.                             errors: ['time']
  54.                         });
  55.                     } catch (err) {
  56.                         console.error(err);
  57.                         return message.channel.send('No or invalid value entered, cancelling video selection.');
  58.                     }
  59.                     var videoIndex = parseInt(response.first().content);
  60.                     var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
  61.                 } catch (err) {
  62.                     console.error(err);
  63.                     return message.channel.send('🆘 I could not obtain any search results.');
  64.                 }
  65.             }
  66.             return handleVideo(video, message, voiceChannel);
  67.         }
  68.         break;
  69. case "skip":
  70.         if (!message.member.voiceChannel) return message.channel.send('You are not in a voice channel!');
  71.         if (!serverQueue) return message.channel.send('There is nothing playing that I could skip for you.');
  72.         serverQueue.connection.dispatcher.end('Skip command has been used!');
  73.         return undefined;
  74.         break;
  75.       case "mstop":
  76.         if (!message.member.voiceChannel) return message.channel.send('You are not in a voice channel!');
  77.         if (!serverQueue) return message.channel.send('There is nothing playing that I could stop for you.');
  78.         serverQueue.songs = [];
  79.         serverQueue.connection.dispatcher.end('Stop command has been used!');
  80.         return undefined;
  81. break;
  82. case "volume":
  83.         if (!message.member.voiceChannel) return message.channel.send('You are not in a voice channel!');
  84.         if (!serverQueue) return message.channel.send('There is nothing playing.');
  85.         if (!args[1]) return message.channel.send(`The current volume is: **${serverQueue.volume}**`);
  86.         serverQueue.volume = args[1];
  87.         serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 5);
  88.         return message.channel.send(`I set the volume to: **${args[1]}**`);
  89. break;
  90. case "np":
  91.         if (!serverQueue) return message.channel.send('There is nothing playing.');
  92.         return message.channel.send(`🎶 Now playing: **${serverQueue.songs[0].title}**`);
  93. break;
  94. case "queue":
  95.         if (!serverQueue) return message.channel.send('There is nothing playing.');
  96.         return message.channel.send(`
  97. __**Song queue:**__
  98. ${serverQueue.songs.map(song => `**-** ${song.title}`).join('\n')}
  99. **Now playing:** ${serverQueue.songs[0].title}
  100.         `);
  101. break;
  102. case "pause":
  103.         if (serverQueue && serverQueue.playing) {
  104.             serverQueue.playing = false;
  105.             serverQueue.connection.dispatcher.pause();
  106.             return message.channel.send('⏸ Paused the music for you!');
  107.         }
  108.         return message.channel.send('There is nothing playing.');
  109. break;
  110. case "resume":
  111.         if (serverQueue && !serverQueue.playing) {
  112.             serverQueue.playing = true;
  113.             serverQueue.connection.dispatcher.resume();
  114.             return message.channel.send('▶ Resumed the music for you!');
  115.         }
  116.         return message.channel.send('There is nothing playing.');
  117.    
  118.  
  119.     return undefined;
  120. break;
  121. }
  122. async function handleVideo(video, message, voiceChannel, playlist = false) {
  123.     var serverQueue = queue.get(message.guild.id);
  124.     console.log(video);
  125.     var song = {
  126.         id: video.id,
  127.         title: video.title,
  128.         url: `https://www.youtube.com/watch?v=${video.id}`
  129.     };
  130.     if (!serverQueue) {
  131.         var queueConstruct = {
  132.             textChannel: message.channel,
  133.             voiceChannel: voiceChannel,
  134.             connection: null,
  135.             songs: [],
  136.             volume: 5,
  137.             playing: true
  138.         };
  139.         queue.set(message.guild.id, queueConstruct);
  140.  
  141.         queueConstruct.songs.push(song);
  142.  
  143.         try {
  144.             var connection = await voiceChannel.join();
  145.             queueConstruct.connection = connection;
  146.             play(message.guild, queueConstruct.songs[0]);
  147.         } catch (error) {
  148.             console.error(`I could not join the voice channel: ${error}`);
  149.             queue.delete(message.guild.id);
  150.             return message.channel.send(`I could not join the voice channel: ${error}`);
  151.         }
  152.     } else {
  153.         serverQueue.songs.push(song);
  154.         console.log(serverQueue.songs);
  155.         if (playlist) return undefined;
  156.         else return message.channel.send(`✅ **${song.title}** has been added to the queue!`);
  157.     }
  158.     return undefined;
  159. }
  160.   function play(guild, song) {
  161.     var serverQueue = queue.get(guild.id);
  162.  
  163.     if (!song) {
  164.         serverQueue.voiceChannel.leave();
  165.         queue.delete(guild.id);
  166.         return;
  167.     }
  168.     console.log(serverQueue.songs);
  169.  
  170.     const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  171.         .on('end', reason => {
  172.       message.channel.send('``The queue of song is end.``');
  173.             if (reason === 'Stream is not generating quickly enough.') console.log('Song ended.');
  174.             else console.log(reason);
  175.             serverQueue.songs.shift();
  176.             play(guild, serverQueue.songs[0]);
  177.         })
  178.         .on('error', error => console.error(error));
  179.     dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  180.  
  181.     serverQueue.textChannel.send(`🎶 Start playing: **${song.title}**`);
  182. }
  183. });
  184. client.login(settings.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