Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Client } = require('discord.js');
  2. const yt = require('ytdl-core');
  3. const client = new Client();
  4.  
  5. let queue = {};
  6.  
  7. const commands = {
  8.     'play': (msg) => {
  9.         if (queue[msg.guild.id] === undefined) return msg.channel.sendMessage(`Я не вижу песен в оч ${process.env.PREFIX}add`);
  10.         if (!msg.guild.voiceConnection) return commands.join(msg).then(() => commands.play(msg));
  11.         if (queue[msg.guild.id].playing) return msg.channel.sendMessage('Already Playing');
  12.         let dispatcher;
  13.         queue[msg.guild.id].playing = true;
  14.  
  15.         console.log(queue);
  16.         (function play(song) {
  17.             console.log(song);
  18.             if (song === undefined) return msg.channel.sendMessage('Queue is empty').then(() => {
  19.                 queue[msg.guild.id].playing = false;
  20.                 msg.member.voiceChannel.leave();
  21.             });
  22.             msg.channel.sendMessage(`Playing: **${song.title}** as requested by: **${song.requester}**`);
  23.             dispatcher = msg.guild.voiceConnection.playStream(yt(song.url, { audioonly: true }), { passes : 1 });
  24.             let collector = msg.channel.createCollector(m => m);
  25.             collector.on('message', m => {
  26.                 if (m.content.startsWith(process.env.PREFIX + 'pause')) {
  27.                     msg.channel.sendMessage('paused').then(() => {dispatcher.pause();});
  28.                 } else if (m.content.startsWith(process.env.PREFIX + 'resume')){
  29.                     msg.channel.sendMessage('resumed').then(() => {dispatcher.resume();});
  30.                 } else if (m.content.startsWith(process.env.PREFIX + 'skip')){
  31.                     msg.channel.sendMessage('skipped').then(() => {dispatcher.end();});
  32.                 } else if (m.content.startsWith('volume+')){
  33.                     if (Math.round(dispatcher.volume*50) >= 100) return msg.channel.sendMessage(`Volume: ${Math.round(dispatcher.volume*50)}%`);
  34.                     dispatcher.setVolume(Math.min((dispatcher.volume*50 + (2*(m.content.split('+').length-1)))/50,2));
  35.                     msg.channel.sendMessage(`Volume: ${Math.round(dispatcher.volume*50)}%`);
  36.                 } else if (m.content.startsWith('volume-')){
  37.                     if (Math.round(dispatcher.volume*50) <= 0) return msg.channel.sendMessage(`Volume: ${Math.round(dispatcher.volume*50)}%`);
  38.                     dispatcher.setVolume(Math.max((dispatcher.volume*50 - (2*(m.content.split('-').length-1)))/50,0));
  39.                     msg.channel.sendMessage(`Volume: ${Math.round(dispatcher.volume*50)}%`);
  40.                 } else if (m.content.startsWith(process.env.PREFIX + 'time')){
  41.                     msg.channel.sendMessage(`time: ${Math.floor(dispatcher.time / 60000)}:${Math.floor((dispatcher.time % 60000)/1000) <10 ? '0'+Math.floor((dispatcher.time % 60000)/1000) : Math.floor((dispatcher.time % 60000)/1000)}`);
  42.                 }
  43.             });
  44.             dispatcher.on('end', () => {
  45.                 collector.stop();
  46.                 play(queue[msg.guild.id].songs.shift());
  47.             });
  48.             dispatcher.on('error', (err) => {
  49.                 return msg.channel.sendMessage('error: ' + err).then(() => {
  50.                     collector.stop();
  51.                     play(queue[msg.guild.id].songs.shift());
  52.                 });
  53.             });
  54.         })(queue[msg.guild.id].songs.shift());
  55.     },
  56.     'join': (msg) => {
  57.         return new Promise((resolve, reject) => {
  58.             const voiceChannel = msg.member.voiceChannel;
  59.             if (!voiceChannel || voiceChannel.type !== 'voice') return msg.reply('I couldn\'t connect to your voice channel...');
  60.             voiceChannel.join().then(connection => resolve(connection)).catch(err => reject(err));
  61.         });
  62.     },
  63.     'add': (msg) => {
  64.         let url = msg.content.split(' ')[1];
  65.         if (url == '' || url === undefined) return msg.channel.sendMessage(`You must add a YouTube video url, or id after ${process.env.PREFIX}add`);
  66.         yt.getInfo(url, (err, info) => {
  67.             if(err) return msg.channel.sendMessage('Invalid YouTube Link: ' + err);
  68.             if (!queue.hasOwnProperty(msg.guild.id)) queue[msg.guild.id] = {}, queue[msg.guild.id].playing = false, queue[msg.guild.id].songs = [];
  69.             queue[msg.guild.id].songs.push({url: url, title: info.title, requester: msg.author.username});
  70.             msg.channel.sendMessage(`added **${info.title}** to the queue`);
  71.         });
  72.     },
  73.     'queue': (msg) => {
  74.         if (queue[msg.guild.id] === undefined) return msg.channel.sendMessage(`Add some songs to the queue first with ${process.env.PREFIX}add`);
  75.         let tosend = [];
  76.         queue[msg.guild.id].songs.forEach((song, i) => { tosend.push(`${i+1}. ${song.title} - Requested by: ${song.requester}`);});
  77.         msg.channel.sendMessage(`__**${msg.guild.name}'s Music Queue:**__ Currently **${tosend.length}** songs queued ${(tosend.length > 15 ? '*[Only next 15 shown]*' : '')}\n\`\`\`${tosend.slice(0,15).join('\n')}\`\`\``);
  78.     },
  79.     'help': (msg) => {
  80.         let tosend = ['```xl', process.env.PREFIX + 'join : "Join Voice channel of msg sender"',    process.env.PREFIX + 'add : "Add a valid youtube link to the queue"', process.env.PREFIX + 'queue : "Shows the current queue, up to 15 songs shown."', process.env.PREFIX + 'play : "Play the music queue if already joined to a voice channel"', '', 'the following commands only function while the play command is running:'.toUpperCase(), process.env.PREFIX + 'pause : "pauses the music"',   process.env.PREFIX + 'resume : "resumes the music"', process.env.PREFIX + 'skip : "skips the playing song"', process.env.PREFIX + 'time : "Shows the playtime of the song."',   'volume+(+++) : "increases volume by 2%/+"',    'volume-(---) : "decreases volume by 2%/-"',    '```'];
  81.         msg.channel.sendMessage(tosend.join('\n'));
  82.     },
  83.     'reboot': (msg) => {
  84.         if (msg.author.id == tokens.adminID) process.exit(); //Requires a node module like Forever to work.
  85.     }
  86. };
  87.  
  88. client.on('ready', () => {
  89.     console.log('ready!');
  90. });
  91.  
  92. client.on('message', msg => {
  93.     if (!msg.content.startsWith(process.env.PREFIX)) return;
  94.     if (commands.hasOwnProperty(msg.content.toLowerCase().slice(process.env.PREFIX.length).split(' ')[0])) commands[msg.content.toLowerCase().slice(process.env.PREFIX.length).split(' ')[0]](msg);
  95. });
  96. client.login(process.env.TOKEN);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement