Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const commando = require("discord.js-commando");
- const config = require("../.././config.json");
- const ytdl = require("ytdl-core");
- const { RichEmbed } = require("discord.js");
- class play extends commando.Command {
- constructor(client) {
- super(client, {
- name: "play",
- aliases: ["p"],
- group: "music",
- memberName: "play",
- description: "Aditi joins the user's current voice channel and plays a song.",
- clientPermissions: ["CONNECT", "SPEAK"],
- args: [{
- key: "link",
- prompt: "What do you want me to play?",
- type: "string"
- }]
- });
- } async run(message, args) {
- const queue = new Map();
- const serverQueue = queue.get(message.guild.id);
- const avatarURL = this.client.user.avatarURL;
- const voiceChannel = message.member.voiceChannel;
- var embed = new RichEmbed()
- .setAuthor("Song Status", avatarURL)
- .setColor(message.guild.roles.find("name", "Aditi").color);
- if (!voiceChannel) {
- embed.setTitle("Error!")
- .setDescription("You need to be in a voice channel before I can connect to one.");
- return message.channel.send({embed});
- }
- const songInfo = await ytdl.getInfo(args.link);
- const song = {
- title: songInfo.title,
- url: songInfo.video_url,
- };
- if (!serverQueue) {
- const queueConstruct = {
- textChannel: message.channel,
- voiceChannel: voiceChannel,
- connection: null,
- songs: [],
- volume: 5,
- playing: true
- };
- queue.set(message.guild.id, queueConstruct);
- queueConstruct.songs.push(song);
- try {
- var connection = await voiceChannel.join();
- queueConstruct.connection = connection;
- playSong(message.guild, queueConstruct.songs[0], avatarURL);
- } catch (error) {
- console.error(error);
- embed.setTitle("Error!")
- .setDescription("I could not join this voice channel due to an unknown error.");
- queue.delete(message.guild.id);
- return message.channel.send({embed});
- }
- } else {
- serverQueue.songs.push(song);
- return message.channel.send(`**${song.title}** has been added to the queue.`); // Redo!!! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- }
- return undefined;
- function playSong(guild, song, avatarURL) {
- var embed = new RichEmbed()
- .setAuthor("Song Status", avatarURL)
- .setColor(message.guild.roles.find("name", "Aditi").color);
- const serverQueue = queue.get(guild.id);
- if (!song) {
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- embed.setTitle("There are no more songs remaining in the queue!")
- .setDescription("I'm going to leave this voice channel now.");
- return;
- }
- const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
- .on("end", () => {
- embed.setTitle("Song ended.");
- serverQueue.songs.shift();
- playSong(guild, serverQueue.songs[0], avatarURL);
- message.channel.send({embed});
- }).on("error", error => {
- console.error(error);
- embed.setTitle("Error!")
- .setDescription("I caught an unknown error.");
- message.channel.send({embed});
- });
- dispatcher.setVolumeLogarithmic(5 / 5);
- }
- }
- }
- module.exports = play;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement