Advertisement
Guest User

Untitled

a guest
Dec 18th, 2020
29,949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------- Play COMMAND ------------
  2.  
  3. const ytdl = require('ytdl-core');
  4. const ytSearch = require('yt-search');
  5.  
  6. module.exports = {
  7.     name: 'play',
  8.     description: 'Joins and plays a video from youtube',
  9.     async execute(message, args) {
  10.         const voiceChannel = message.member.voice.channel;
  11.  
  12.         if (!voiceChannel) return message.channel.send('You need to be in a channel to execute this command!');
  13.         const permissions = voiceChannel.permissionsFor(message.client.user);
  14.         if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins');
  15.         if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');
  16.         if (!args.length) return message.channel.send('You need to send the second argument!');
  17.  
  18.         const validURL = (str) =>{
  19.             var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
  20.             if(!regex.test(str)){
  21.                 return false;
  22.             } else {
  23.                 return true;
  24.             }
  25.         }
  26.  
  27.         if(validURL(args[0])){
  28.  
  29.             const  connection = await voiceChannel.join();
  30.             const stream  = ytdl(args[0], {filter: 'audioonly'});
  31.  
  32.             connection.play(stream, {seek: 0, volume: 1})
  33.             .on('finish', () =>{
  34.                 voiceChannel.leave();
  35.                 message.channel.send('leaving channel');
  36.             });
  37.  
  38.             await message.reply(`:thumbsup: Now Playing ***Your Link!***`)
  39.  
  40.             return
  41.         }
  42.  
  43.        
  44.         const  connection = await voiceChannel.join();
  45.  
  46.         const videoFinder = async (query) => {
  47.             const videoResult = await ytSearch(query);
  48.  
  49.             return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
  50.  
  51.         }
  52.  
  53.         const video = await videoFinder(args.join(' '));
  54.  
  55.         if(video){
  56.             const stream  = ytdl(video.url, {filter: 'audioonly'});
  57.             connection.play(stream, {seek: 0, volume: 1})
  58.             .on('finish', () =>{
  59.                 voiceChannel.leave();
  60.             });
  61.  
  62.             await message.reply(`:thumbsup: Now Playing ***${video.title}***`)
  63.         } else {
  64.             message.channel.send('No video results found');
  65.         }
  66.     }
  67. }
  68.  
  69.  
  70. ------------- Leave Command ------------
  71.  
  72. module.exports = {
  73.     name: 'leave',
  74.     description: 'stop the bot and leave the channel',
  75.     async execute(message, args) {
  76.         const voiceChannel = message.member.voice.channel;
  77.  
  78.         if(!voiceChannel) return message.channel.send("You need to be in a voice channel to stop the music!");
  79.         await voiceChannel.leave();
  80.         await message.channel.send('Leaving channel :smiling_face_with_tear:')
  81.  
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement