Advertisement
Guest User

Untitled

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