Advertisement
HaydnG

Untitled

Jun 4th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js');
  2. const client = new Discord.Client();
  3.  
  4. const fs = require('fs');
  5. const ytdl = require('ytdl-core');
  6. var Player;
  7. var playlist = [];
  8. var playing = false;
  9. var channel;
  10. var chatchannel;
  11. var PlaylistMax = 50;
  12. var request = require("request");
  13. var guild;
  14.  
  15.  
  16. function JsonRequest(url,callback){
  17.     console.log(url);
  18.     var data = request({url: url,json: true}, function (error, response, body) {
  19.         //console.log(body);
  20.         callback(body);
  21.     });
  22. }
  23.  
  24. function AddPlaylistSongs(url,message){
  25.     json = JsonRequest(url,function(body){
  26.         if(body['items'] != undefined || body['items'] != null){
  27.             var NextPageToken = body['nextPageToken'];
  28.             var count = 0;
  29.             body['items'].forEach(function(item,index){
  30.                 var videourl = 'https://www.youtube.com/watch?v='+item['snippet']['resourceId']['videoId'];
  31.                 ytdl.getInfo(videourl, 'info',function(err,info){
  32.                     //console.log(info);
  33.                     AddSong(videourl,info['title'],info['author']['name'],info['length_seconds']);
  34.                    
  35.                 });
  36.  
  37.  
  38.             });
  39.             message.channel.sendMessage(body['items'].length +' songs has been added to the queue.');
  40.         }
  41.        
  42.        
  43.     });
  44.    
  45. }
  46.  
  47.  
  48.  
  49. var prefix = '!'; var commands = [];
  50. var VC;
  51.  
  52.  
  53. client.on('ready', () => {
  54.   console.log('I am ready!');
  55.  
  56.   AddCommand('ping',function(message){
  57.       msgArray = message.content.split(' ');
  58.       message.channel.sendMessage("pong "+ client.ping + ' ms' +" "+ msgArray.slice(1).join(' '));
  59.   });
  60.   AddCommand('skip',function(message){
  61.       Skip();
  62.   });
  63.   AddCommand('help',function(message){
  64.       message.channel.sendMessage("`");
  65.   });
  66.   AddCommand('join',function(message){
  67.       var author = message.author;
  68.       message.guild.channels.findAll('type','voice').forEach(function(item,index){
  69.           item['members'].array().forEach(function(gmember,index){
  70.               if(gmember['user'] === author){
  71.                   VC = item.join();
  72.                   message.channel.sendMessage("Joining "+item['name']+"...");
  73.                  
  74.               }
  75.           });
  76.          
  77.       });
  78.      
  79.   });
  80.   AddCommand('play',function(message){
  81.         msgArray = message.content.split(' ');
  82.         console.log(msgArray.length);
  83.         if(msgArray.length >= 2){
  84.             var author = message.author;
  85.               message.guild.channels.findAll('type','voice').forEach(function(item,index){
  86.                   item['members'].array().forEach(function(gmember,index){
  87.                       if(gmember['user'] === author){
  88.                                 chatchannel = message.channel;
  89.                                 channel = item;
  90.                                 guild = message.guild;
  91.                                
  92.                                 ytdl.getInfo(msgArray[1], 'info',function(err,info){
  93.                                     if(info == null){//Im a playlist!!
  94.                                         var playlistId = msgArray[1].split('=')[1];
  95.                                         AddPlaylistSongs('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults='+PlaylistMax+'&pageToken=&playlistId='+playlistId+'&key=',message);
  96.                                     }else{
  97.                                         console.log(info['length_seconds']+" Time");
  98.                                         AddSong(msgArray[1],info['title'],info['author']['name'],info['length_seconds']);
  99.                                     }
  100.                                    
  101.                                 });
  102.  
  103.                            
  104.                       }
  105.                   });
  106.                  
  107.              });
  108.         }else{
  109.             message.channel.sendMessage("Please provide a url.");
  110.         }  
  111.   });
  112.  
  113. });
  114.  
  115. function AddSong(url,title,name,length){
  116.     playlist.push(new Song(url,title,name,length));
  117.     console.log(playlist);
  118.     if(!playing){
  119.        
  120.         Player = Play();
  121.     }  
  122. }
  123.  
  124. function Skip(){
  125.     console.log("Skipped")
  126.     dispatcher.end();
  127.  
  128.    
  129. }
  130. function End(){
  131.     console.log("Ended")
  132.     playing = false;
  133.     playlist = playlist.splice(1);
  134.     Play();
  135. }  
  136.  
  137.  function Play(){
  138.     if(!playing && playlist.length > 0){
  139.            var voiceConnection = guild.voiceConnection;
  140.            if(voiceConnection){
  141.                const stream = ytdl(playlist[0].url, { filter : 'audioonly' });
  142.                playing = true;
  143.  
  144.                dispatcher = voiceConnection.playStream(stream);
  145.                dispatcher.on('end', (reason) => {
  146.                   console.log('reason: '+ reason);
  147.                   End();
  148.                 });
  149.                
  150.                chatchannel.send("Playing " + playlist[0].name + " | " + playlist[0].author);
  151.                console.log("Playing " + playlist[0].name + " | " + playlist[0].author + " Length: " + playlist[0].length);
  152.            }else{
  153.                channel.join().then(Play);
  154.            }
  155.     }
  156. }
  157.  
  158. client.on('message', message => {
  159.     content = message.content + ' ';
  160.     msgArray = message.content.split(' ');
  161.     commands.forEach(function(item,index){
  162.         if (message.content.startsWith(item.command)) {
  163.             item.method(message);
  164.         }
  165.        
  166.     });
  167.    
  168.    
  169. });
  170. function AddCommand(command,method){
  171.     commands.push(new Command(prefix + command,method));
  172. }
  173. function Command(command,method){
  174.     this.method = method;
  175.     this.command = command;
  176. }
  177.  
  178. function Song(url,name,author,length){
  179.     this.url = url;
  180.     this.name = name;
  181.     this.author = author;
  182.     this.length = length;
  183. }
  184.  
  185.  
  186. client.login('tokenID');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement