Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { RichEmbed } = require("discord.js");
  2. const { purple } = require(`../../colours.json`)
  3. const { Utils } = require("erela.js")
  4.  
  5.  
  6. module.exports = {
  7.     config: {
  8.         name: "play",
  9.         description: "Our Play Command",
  10.         usage: "^play",
  11.         accessableby: "member",
  12.         aliases: []
  13.     },
  14.     run: async (bot, message, args) => {
  15.  
  16.         const { voiceChannel } = message.member;
  17.         if (!voiceChannel) return message.channel.send("You need to be in a voice channel to play music.");
  18.  
  19.         const permissions = voiceChannel.permissionsFor(bot.user);
  20.         if (!permissions.has("CONNECT")) return message.channel.send("I cannot connect to your voice channel, make sure I have permission to!");
  21.         if (!permissions.has("SPEAK")) return message.channel.send("I cannot connect to your voice channel, make sure I have permission to!");
  22.  
  23.         if (!args[0]) return message.channel.send("Please provide a song name or link to search.");
  24.  
  25.         const player = bot.music.players.spawn({
  26.             guild: message.guild,
  27.             textChannel: message.channel,
  28.             voiceChannel
  29.         });
  30.  
  31.         bot.music.search(args.join(" "), message.author).then(async res => {
  32.             switch (res.loadType) {
  33.                 case "TRACK_LOADED":
  34.                     player.queue.add(res.tracks[0]);
  35.                     message.channel.send(`Enqueuing \`${res.tracks[0].title}\` \`${Utils.formatTime(res.tracks[0].duration, true)}\``);
  36.                     if (!player.playing) player.play()
  37.                     break;
  38.                
  39.                 case "SEARCH_RESULT":
  40.                     let index = 1;
  41.                     const tracks = res.tracks.slice(0, 5);
  42.                     const embed = new RichEmbed()
  43.                         .setAuthor("Song Selection.", message.author.displayAvatarURL)
  44.                         .setDescription(tracks.map(video => `**${index++} -** ${video.title}`))
  45.                         .setFooter("Your response time closes within the next 30 seconds. Type 'cancel' to cancel the selection");
  46.  
  47.                     await message.channel.send(embed);
  48.  
  49.                     const collector = message.channel.createMessageCollector(m => {
  50.                         return m.author.id === message.author.id && new RegExp(`^([1-5]|cancel)$`, "i").test(m.content)
  51.                     }, { time: 30000, max: 1});
  52.  
  53.                     collector.on("collect", m => {
  54.                         if (/cancel/i.test(m.content)) return collector.stop("cancelled")
  55.  
  56.                         const track = tracks[Number(m.content) - 1];
  57.                         player.queue.add(track)
  58.                         message.channel.send(`Enqueuing \`${track.title}\` \`${Utils.formatTime(track.duration, true)}\``);
  59.                         if(!player.playing) player.play();
  60.                     });
  61.  
  62.                     collector.on("end", (_, reason) => {
  63.                         if(["time", "cancelled"].includes(reason)) return message.channel.send("Cancelled selection.")
  64.                     });
  65.                     break;
  66.  
  67.                 case "PLAYLIST_LOADED":
  68.                     res.playlist.tracks.forEach(track => player.queue.add(track));
  69.                     const duration = Utils.formatTime(res.playlist.tracks.reduce((acc, cur) => ({duration: acc.duration + cur.duration})).duration, true);
  70.                     message.channel.send(`Enqueuing \`${res.playlist.tracks.length}\` \`${duration}\` tracks in playlist \`${res.playlist.info.name}\``);
  71.                     if(!player.playing) player.play()
  72.                     break;
  73.             }
  74.         }).catch(err => message.channel.send(err.message))
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement