Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Command } = require('klasa');
  2. const yt = require('ytdl-core');
  3. const search = require('youtube-search');
  4.  
  5. module.exports = class extends Command {
  6.  
  7.     constructor(...args) {
  8.         super(...args, {
  9.             name: 'play',
  10.             enabled: true,
  11.             runIn: ['text'],
  12.             cooldown: 0,
  13.             aliases: [],
  14.             permLevel: 0,
  15.             botPerms: ['CONNECT', 'SPEAK', 'SEND_MESSAGES', 'EMBED_LINKS'],
  16.             requiredSettings: [],
  17.             description: 'Plays the audio of a YouTube video.',
  18.             quotedStringSupport: false,
  19.             usage: '<search:string>',
  20.             usageDelim: undefined
  21.         });
  22.     }
  23.  
  24.     async run(msg, [searchString]) {
  25.       if(!msg.member.voiceChannel)return msg.channel.send('You must be in a voice channel to use music commands.').catch(console.error);
  26.  
  27.       const options = {
  28.         maxResults: 1,
  29.         type: 'video',
  30.         key: 'AIzaSyBp0nhmScKPAIy7BfS4pp4SbmdvB5lZn04'
  31.       };
  32.  
  33.       search(searchString, options, (err, results) => {
  34.         if(err) return console.log(err);
  35.         if(results.length === 0)return message.channel.send('No results found.').catch(console.error);
  36.         if(!this.client.music[msg.guild.id])this.client.music[msg.guild.id] = {queue: [], loop: false};
  37.         let video = results[0];
  38.         video.requester = msg.author.tag;
  39.         if(!this.client.music[msg.guild.id].nowPlaying){
  40.           playVideo(this.client, msg, video);
  41.         } else {
  42.           this.client.music[msg.guild.id].queue.push(video);
  43.         }
  44.         const embed = new this.client.methods.Embed();
  45.         embed.setTitle(video.title);
  46.         embed.setURL(video.link);
  47.         embed.setDescription(video.description);
  48.         embed.addField('Channel:', video.channelTitle);
  49.         embed.setThumbnail(video.thumbnails.default.url);
  50.         embed.setFooter('Requested by ' + video.requester);
  51.         embed.setColor('RANDOM');
  52.         msg.channel.send(embed).catch(console.error);
  53.       });
  54.  
  55.     }
  56.  
  57.     playVideo = async function(client, msg, video) {
  58.       const connection = await msg.member.voiceChannel.join().catch(console.error);
  59.       client.music[msg.guild.id].dispatcher = connection.playStream(yt(video.link, { audioonly: true }), { passes: 1 });
  60.       client.music[msg.guild.id].nowPlaying = video;
  61.  
  62.       client.music[msg.guild.id].dispatcher.on('end', () => {
  63.         if(client.music[msg.guild.id].loop)client.music[msg.guild.id].queue.push(client.music[msg.guild.id].nowPlaying);
  64.         if(client.music[msg.guild.id].queue.length > 0)return playVideo(client, msg, client.music[msg.guild.id].queue.shift());
  65.         connection.disconnect();
  66.         delete client.music[msg.guild.id];
  67.       });
  68.     }
  69.  
  70.     async init() {
  71.       if(!this.client.music)this.client.music = {};
  72.     }
  73.  
  74. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement