SHARE
TWEET

play.js

a guest Sep 22nd, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fetch = require('snekfetch');
  2. const ytdl = require('ytdl-core');
  3. const fs = require('fs');
  4. const config = require('./config.json');
  5. const fetchVideoInfo = require('youtube-info');
  6. const Discord = require('discord.js');
  7. const moment = require('moment');
  8. const db = require('node-json-db');
  9.  
  10. const queue = new db('./queue/songs.json', true, true);
  11. const titleForFinal = [];
  12. const chalk = require('chalk');
  13.  
  14. const skipper = [];
  15. const skipReq = 0;
  16.  
  17. exports.run = async (client, message) => {
  18.   // Guilds = {};
  19.     message.delete();
  20.     // =========================Play Command==============================
  21.     const toPlay = message.content.split(' ').slice(1).join(' ');
  22.     if (!toPlay) {
  23.         return message.reply('Please add a link of the song to the command');
  24.     }
  25.  
  26.     if (!message.member.voiceChannel) {
  27.         return message.channel.send('Please get into a voice channel');
  28.     }
  29.     if (!toPlay.includes('&list') && !toPlay.includes('index')) {
  30.         fetch.get(`https://www.googleapis.com/youtube/v3/search?part=id&type=video&q=` + encodeURIComponent(toPlay) + '&key=' + config.ytKey)
  31.      .then(async r => {
  32.     if (r.body.items[0]) {
  33.         fetchVideoInfo(`${r.body.items[0].id.videoId}`).then(l => {
  34.             titleForFinal.push(l.title);
  35.             const embed = new Discord.RichEmbed()
  36.            .setAuthor(`Requested by ${message.author.username} and added to the queue`, l.thumbnailUrl)
  37.            .addField(`Song Info`, `**Owner:** ${l.owner}\n\
  38.     **Date Published:** ${l.datePublished}\n\
  39.     **Duration:** ${(l.duration / 60).toFixed(2)} Minutes\n\
  40.     **Views:** ${l.views}\n\
  41.     **Song Name:** ${l.title}`)
  42.            .setThumbnail(l.thumbnailUrl);
  43.             message.channel.send({embed});
  44.         });
  45.     }
  46.  
  47.     try {
  48.         queue.getData(`/parent/${message.guild.id}`);
  49.     } catch (e) {
  50.         queue.push(`/parent/${message.guild.id}/TheSongs/mySongs`, {queue: []}, false);
  51.     }
  52.  
  53.     try {
  54.         queue.push(`/parent/${message.guild.id}/TheSongs/mySongs`, {queue: [r.body.items[0].id.videoId]}, false);
  55.     } catch (e) {
  56.         logger.error(e);
  57.     }
  58.  
  59.     if (!message.guild.voiceConnection) {
  60.         message.member.voiceChannel.join().then(async connection => {
  61.             logger.info(`Started to stream ${chalk.magenta(r.body.items[0].title)} for ${message.author.username}`);
  62.             play(connection, message);
  63.         });
  64.     }
  65. })
  66.       .catch(e => {
  67.     message.reply('We could\' find the requested song :pensive:');
  68.     logger.error(e);
  69. });
  70.     } else {
  71.     // =========================Plays Playlists==============================
  72.         await playLists(message, toPlay);
  73.         logger.info(`Streaming a playlist for ${message.author.username}`);
  74.     }
  75. };
  76.  
  77. // =========================Play Function==============================
  78.  
  79. function play(connection, message) {
  80.     const songsQueue = [];
  81.     const json = queue.getData(`/parent/${message.guild.id}/TheSongs/mySongs/queue`);
  82.     dispatcher = connection.playStream(ytdl(json[0], {filter: 'audioonly'}));
  83.  
  84.     const list = queue.getData(`/parent/${message.guild.id}/TheSongs/mySongs/queue[0]`);
  85.     if (!message.guild.voiceConnection) {
  86.         message.member.voiceChannel.join().then(async connection => {
  87.             logger.info(`Started to stream ${chalk.magenta(titleForFinal)} for ${message.author.username}`);
  88.             play(connection, message);
  89.         });
  90.     }
  91.     fetchVideoInfo(`${list}`).then(l => {
  92.         message.channel.send(`Started to stream **\`${l.title}\`** Requested by ${message.author.username}`);
  93.         logger.info(`Downloading the song ==> ${l.title} for ${message.author.username}`);
  94.         logger.info(`Downloaded ${l.title} successfully Enjoy!`);
  95.     });
  96.     setTimeout(() => {
  97.         queue.delete((`/parent/${message.guild.id}/TheSongs/mySongs/queue[0]`));
  98.     }, 3000);
  99.  
  100.     dispatcher.on('end', () => {
  101.         if (list) {
  102.             play(connection, message);
  103.         } else {
  104.             connection.disconnect();
  105.             queue.delete(`/parent/`);
  106.         }
  107.     });
  108. }
  109.  
  110. // =========================Get Playlist Function==============================
  111.  
  112. function playLists(message, id) {
  113.     fetch.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' + id.split('&list=')[1] + '&key=' + config.ytKey)
  114.     .then(res => {
  115.     const playembed = new Discord.RichEmbed()
  116.             .setAuthor(`New playlist added contains ${res.body.items.length} songs in it`, message.author.displayAvatarURL);
  117.     message.channel.send({embed: playembed});
  118.     try {
  119.         queue.getData(`/parent/${message.guild.id}`);
  120.     } catch (e) {
  121.         queue.push(`/parent/${message.guild.id}/TheSongs/mySongs`, {queue: []}, false);
  122.     }
  123.     res.body.items.forEach(i => {
  124.         if (i.id) {
  125.             queue.push(`/parent/${message.guild.id}/TheSongs/mySongs`, {queue: [i.snippet.resourceId.videoId]}, false);
  126.         }
  127.     });
  128.     if (!message.guild.voiceConnection) {
  129.         message.member.voiceChannel.join().then(async connection => {
  130.             play(connection, message);
  131.         });
  132.     }
  133. })
  134.     .catch(e => {
  135.     log.error(e);
  136.     log.error(id.split('&list=')[1]);
  137. });
  138. }
  139.  
  140. module.exports.help = {
  141.     name: 'play'
  142. };
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