Advertisement
Guest User

play.js

a guest
Nov 23rd, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const commando = require("discord.js-commando");
  2. const config = require("../.././config.json");
  3. const ytdl = require("ytdl-core");
  4. const { RichEmbed } = require("discord.js");
  5.  
  6. class play extends commando.Command {
  7.   constructor(client) {
  8.     super(client, {
  9.       name: "play",
  10.       aliases: ["p"],
  11.       group: "music",
  12.       memberName: "play",
  13.       description: "Aditi joins the user's current voice channel and plays a song.",
  14.       clientPermissions: ["CONNECT", "SPEAK"],
  15.       args: [{
  16.         key: "link",
  17.         prompt: "What do you want me to play?",
  18.         type: "string"
  19.       }]
  20.     });
  21.   } async run(message, args) {
  22.     const queue = new Map();
  23.     const serverQueue = queue.get(message.guild.id);
  24.     const avatarURL = this.client.user.avatarURL;
  25.  
  26.     const voiceChannel = message.member.voiceChannel;
  27.     var embed = new RichEmbed()
  28.       .setAuthor("Song Status", avatarURL)
  29.       .setColor(message.guild.roles.find("name", "Aditi").color);
  30.     if (!voiceChannel) {
  31.       embed.setTitle("Error!")
  32.         .setDescription("You need to be in a voice channel before I can connect to one.");
  33.       return message.channel.send({embed});
  34.     }
  35.     const songInfo = await ytdl.getInfo(args.link);
  36.     const song = {
  37.       title: songInfo.title,
  38.       url: songInfo.video_url,
  39.     };
  40.     if (!serverQueue) {
  41.       const queueConstruct = {
  42.         textChannel: message.channel,
  43.         voiceChannel: voiceChannel,
  44.         connection: null,
  45.         songs: [],
  46.         volume: 5,
  47.         playing: true
  48.       };
  49.       queue.set(message.guild.id, queueConstruct);
  50.       queueConstruct.songs.push(song);
  51.       try {
  52.         var connection = await voiceChannel.join();
  53.         queueConstruct.connection = connection;
  54.         playSong(message.guild, queueConstruct.songs[0], avatarURL);
  55.       } catch (error) {
  56.         console.error(error);
  57.         embed.setTitle("Error!")
  58.           .setDescription("I could not join this voice channel due to an unknown error.");
  59.         queue.delete(message.guild.id);
  60.         return message.channel.send({embed});
  61.       }
  62.     } else {
  63.       serverQueue.songs.push(song);
  64.       return message.channel.send(`**${song.title}** has been added to the queue.`); // Redo!!! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65.     }
  66.     return undefined;
  67.     function playSong(guild, song, avatarURL) {
  68.       var embed = new RichEmbed()
  69.        .setAuthor("Song Status", avatarURL)
  70.        .setColor(message.guild.roles.find("name", "Aditi").color);
  71.       const serverQueue = queue.get(guild.id);
  72.       if (!song) {
  73.        serverQueue.voiceChannel.leave();
  74.        queue.delete(guild.id);
  75.        embed.setTitle("There are no more songs remaining in the queue!")
  76.          .setDescription("I'm going to leave this voice channel now.");
  77.        return;
  78.       }
  79.       const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  80.        .on("end", () => {
  81.          embed.setTitle("Song ended.");
  82.          serverQueue.songs.shift();
  83.          playSong(guild, serverQueue.songs[0], avatarURL);
  84.          message.channel.send({embed});
  85.        }).on("error", error => {
  86.          console.error(error);
  87.          embed.setTitle("Error!")
  88.            .setDescription("I caught an unknown error.");
  89.          message.channel.send({embed});
  90.        });
  91.       dispatcher.setVolumeLogarithmic(5 / 5);
  92.     }
  93.   }
  94. }
  95.  
  96. module.exports = play;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement