const Discord = require('discord.js'); const ytdl = require('ytdl-core'); const { SlashCommandBuilder } = require('@discordjs/builders') const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior } = require('@discordjs/voice'); const { execute } = require('../Events/ready'); const queue = new Map(); function play(song, guild, client){ const player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Pause, } }) player.play(song.url) } async function run(interaction, client) { const songToPlay = interaction.options.getString('song'); const voiceChannel = interaction.member.voice.channel const serverQueue = queue.get(interaction.guild.id); if (!voiceChannel) return interaction.reply(`You must be in a voice channel to play music!`) const songInfo = await ytdl.getInfo(songToPlay); const song = { title: songInfo.videoDetails.title, url: songInfo.videoDetails.video_url, thumbnail: songInfo.videoDetails.thumbnails, } if (!serverQueue) { const queueContruct = { textChannel: interaction.channel, voiceChannel: voiceChannel, connection: null, songs: [], volume: 5, playing: true, } queue.set(interaction.guild.id, queueContruct) queueContruct.songs.push(song) try { var connection = joinVoiceChannel({ channelId: voiceChannel.id, guildId: interaction.guild.id, adapterCreator: interaction.guild.voiceAdapterCreator }) play(song, interaction.guild, client) const playEmbed = new Discord.MessageEmbed() .setTitle(`NOW PLAYING`) .setDescription(`[${song.title}](${song.url}) [${interaction.member}]`) .setThumbnail(song.thumbnail[0].url) .setFooter(`©️ Soundy Studios | All rights reserved!`, `https://cdn.discordapp.com/attachments/861355381774221312/881284765842542592/Soundy_Studios.png`) return interaction.reply({ embeds: [playEmbed] }) } catch (err) { console.error(err) queue.delete(interaction.guild.id) return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }) } } else { serverQueue.songs.push(song); console.log(serverQueue.songs) const addedEmbed = new Discord.MessageEmbed() .setTitle(`ADDED TO QUEUE`) .setDescription(`[${song.title}](${song.url}) [${interaction.member}]`) .setThumbnail(song.thumbnail[0].url) .setFooter(`©️ Soundy Studios | All rights reserved!`, `https://cdn.discordapp.com/attachments/861355381774221312/881284765842542592/Soundy_Studios.png`) return interaction.reply({ embeds: [addedEmbed] }) } } module.exports = { data: new SlashCommandBuilder() .setName('play') .setDescription('Play a music!') .addStringOption(option => option.setName('song') .setDescription('The song you want to play') .setRequired(true)), execute: async (interaction, client) => { await run(interaction, client) } }