SHARE
TWEET

Untitled

a guest May 20th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ytdl = require('ytdl-core');
  2.  
  3. module.exports = {
  4.  
  5.     name: 'music',
  6.     description: 'Command fpr music related stuffs.',
  7.     gulidOnly: true,
  8.  
  9.     execute(client, message, args) {
  10.       const queue = new Map();  
  11.       const serverQueue = queue.get(message.guild.id);
  12.  
  13.       const type = args[0];
  14.  
  15.  
  16.       if(type == 'play'){
  17.         const voiceChannel = message.member.voiceChannel;
  18.           if (!voiceChannel) return message.channel.send('You need to be in a voice channel to play music!');
  19.         const permissions = voiceChannel.permissionsFor(message.client.user);
  20.             if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
  21.                   return message.channel.send('I am not allwd to do dat hooman :( (Insuffect Permissions)');
  22.             }
  23.             const songInfo = ytdl.getInfo(args[1]);
  24.               const song = {
  25.                   title: songInfo.title,
  26.                   url: songInfo.video_url,
  27.                 };
  28.  
  29.              if (!serverQueue) {
  30.                   // Creating the contract for our queue
  31.                       const queueContruct = {
  32.                           textChannel: message.channel,
  33.                           voiceChannel: voiceChannel,
  34.                           connection: null,
  35.                           songs: [],
  36.                           volume: 5,
  37.                           playing: true,
  38.                         };
  39.                         // Setting the queue using our contract
  40.                           queue.set(message.guild.id, queueContruct);
  41.                         // Pushing the song to our songs array
  42.                           queueContruct.songs.push(song);
  43.  
  44.                       try {
  45.                         // Here we try to join the voicechat and save our connection into our object.
  46.                           var connection = voiceChannel.join();
  47.                           queueContruct.connection = connection;
  48.                         // Calling the play function to start a song
  49.                             play(message.guild, queueContruct.songs[0]);
  50.                           } catch (err) {
  51.                         // Printing the error message if the bot fails to join the voicechat
  52.                           console.log(err);
  53.                           queue.delete(message.guild.id);
  54.                           return message.channel.send(err);
  55.                           }
  56.                 } else {
  57.                  serverQueue.songs.push(song);
  58.                  console.log(serverQueue.songs);
  59.                  return message.channel.send(`${song.title} has been added to the queue!`);
  60.                 }
  61.       } else if (type == 'skip') {
  62.         if (!message.member.voiceChannel) return message.channel.send('You have to be in a voice channel to stop the music!');
  63.           if (!serverQueue) return message.channel.send('There is no song that I could skip!');
  64.           serverQueue.connection.dispatcher.end();
  65.       } else if (type == 'stop') {
  66.         if (!message.member.voiceChannel) return message.channel.send('You have to be in a voice channel to stop the music!');
  67.             serverQueue.songs = [];
  68.             serverQueue.connection.dispatcher.end();
  69.       }
  70.  
  71.  
  72.  
  73.       function play(guild, song) {
  74.         const serverQueue = queue.get(guild.id);
  75.      
  76.         if (!song) {
  77.           serverQueue.voiceChannel.leave();
  78.           queue.delete(guild.id);
  79.           return;
  80.         }
  81.      
  82.         const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  83.           .on('end', () => {
  84.             console.log('Music ended!');
  85.             serverQueue.songs.shift();
  86.             play(guild, serverQueue.songs[0]);
  87.           })
  88.           .on('error', error => {
  89.             console.error(error);
  90.           });
  91.         dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  92.       }
  93.  
  94.   }
  95. }
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