const Discord = require("discord.js"); const { SlashCommandBuilder, EmbedBuilder } = require("discord.js") const fs = require("fs") const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior, createAudioResource, StreamType, AudioPlayerStatus, AudioPlayer, getVoiceConnection } = require('@discordjs/voice'); const youtube = require('play-dl'); const ffmpeg = require('ffmpeg-static') const path = require('path'); const Spotify = require('spotify-get') const queue = new Map(); const sclient = new Spotify({ consumer: { key: "74c0fe0446ac418fa60a3febad66bb32", secret: "6f7d405c3b5c4d5e9dbb6ca3cb67d15f" } }) function missingPerms(perm) { const missingPermsembed = new EmbedBuilder() .setTitle('🚨MISSING PERMISSIONS🚨') .setColor('Red') .setDescription(perm) .setFooter({ text: '© | Made by julianrs#3253', iconURL: 'https://cdn.discordapp.com/avatars/928010022624043049/7b546bec88f401d1eadcb1ae881f8412.png' }); return missingPermsembed; } module.exports = { data: new SlashCommandBuilder() .setName("play") .setDescription("Play a song.") .addStringOption(option => option.setName('listener') .setDescription('The song platform.') .setRequired(true)) .addStringOption(option => option.setName('song') .setDescription('The song you want to play.') .setRequired(true)), async execute(i, client) { const listener = i.options.getString('listener') const song = i.options.getString('song') const vc = i.member.voice.channel; if (!vc) return i.reply({ content: 'Join a voice channel before using this command.', ephemeral: true }) const serverQueue = queue.get(i.guild.id); let queueContruct; if (!serverQueue) { queueContruct = { textChannel: i.channel, voiceChannel: vc, connection: null, songs: [], volume: 5, playing: true, } } queue.set(i.guild.id, queueContruct) queueContruct.songs.push(song) if (listener == 'spotify') { const songInfoSpotify = await sclient.search({ q: `${song}`, type: `track`, limit: 5 }) try { if (!vc.joinable) return i.reply({ embeds: [missingPerms('Can\'t join voice channel')], ephemeral: true }) var connection = joinVoiceChannel({ channelId: vc.id, guildId: i.guild.id, adapterCreator: i.guild.voiceAdapterCreator }) await i.reply({ content: 'Looking for the song on the internet!' }) playSpotify(song, client, i, connection) } catch (e) { i.reply('**🚨ERROR🚨** CHECK CONSOLE') console.log(e) } } else if (listener == 'yt' || listener == 'youtube') { const songInfo = await youtube.search(song, { limit: 1 }) try { if (!vc.joinable) return i.reply({ embeds: [missingPerms('Can\'t join voice channel')], ephemeral: true }) var connection = joinVoiceChannel({ channelId: vc.id, guildId: i.guild.id, adapterCreator: i.guild.voiceAdapterCreator }) await i.reply({ content: 'Looking for the song on the internet!' }) playYt(song, client, i, connection) } catch (e) { i.reply('**🚨ERROR🚨** CHECK CONSOLE') console.log(e) } } } } async function playSpotify(songName, client, i, connection) { const guild = i.guild; try { let queueArray; for (var f of queue) { if (guild.id = f.guildid) { queueArray = f; } } const serverQueue = queue.get(guild.id); const player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play, } }) const search = await sclient.search({ q: `${songName}`, type: `track`, limit: 1 }) const toPlay = search.tracks.items[0] console.log(toPlay) const resource = createAudioResource(toPlay, { inputType: toPlay.type }) player.play(resource) connection.subscribe(player) player.on('error', async error => { try { i.channel.send({ content: `An error occured while playing ${search[0].title}, switching song.` }) serverQueue.songs.shift(); if (serverQueue.songs[0]) { playYt(serverQueue.songs[0], client, i, connection) } else { if (connection.state.status === 'destroyed') return i.channel.send({ content: 'Connection is already destroyed.' }) i.channel.send({ content: 'No songs left to play, bye. 👋' }); connection.destroy(); queue.delete(guild.id); } } catch (e) { i.channel.send({ content: '**🚨ERROR🚨** CHECK CONSOLE' }) console.log(e) } }) player.on(AudioPlayerStatus.Idle, async () => { try { serverQueue.songs.shift(); if (serverQueue.songs[0]) { playYt(serverQueue.songs[0], client, i, connection) } else { if (connection.state.status === 'destroyed') return i.channel.send({ content: 'Connection is already destroyed.' }) i.channel.send({ content: 'No songs left to play, bye. 👋' }); connection.destroy(); queue.delete(guild.id); } } catch (e) { i.channel.send({ content: '**🚨ERROR🚨** CHECK CONSOLE' }) console.log(e) } }) player.on(AudioPlayerStatus.Playing, async () => { try { const playYtEmbed = new EmbedBuilder() .setTitle('Now playing') .setColor('Aqua') .setDescription(`[${toPlay.name}](${search[0].url}) [${i.member}]`) .setThumbnail(search[0].thumbnails[0].url) .setFooter({ text: '© | Made by julianrs#3253', iconURL: 'https://cdn.discordapp.com/avatars/928010022624043049/7b546bec88f401d1eadcb1ae881f8412.png' }); i.channel.send({ embeds: [playYtEmbed] }) } catch (e) { i.channel.send('**🚨ERROR🚨** CHECK CONSOLE') console.log(e) } }) } catch (e) { i.channel.send('**🚨ERROR🚨** CHECK CONSOLE') console.log(e) } } async function playYt(songName, client, i, connection) { const guild = i.guild; try { let queueArray; for (var f of queue) { if (guild.id = f.guildid) { queueArray = f; } } const serverQueue = queue.get(guild.id); const player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play, } }) const search = await youtube.search(songName, { limit: 1 }) const toPlay = await youtube.stream(search[0].url) const resource = createAudioResource(toPlay.href, { inputType: 'opus' }) console.log(toPlay.type) player.play(resource) connection.subscribe(player) player.on('error', async error => { try { i.channel.send({ content: `An error occured while playing ${search[0].title}, switching song.` }) serverQueue.songs.shift(); if (serverQueue.songs[0]) { playYt(serverQueue.songs[0], client, i, connection) } else { if (connection.state.status === 'destroyed') return i.channel.send({ content: 'Connection is already destroyed.' }) i.channel.send({ content: 'No songs left to play, bye. 👋' }); connection.destroy(); queue.delete(guild.id); } } catch (e) { i.channel.send({ content: '**🚨ERROR🚨** CHECK CONSOLE' }) console.log(e) } }) player.on(AudioPlayerStatus.Idle, async () => { try { serverQueue.songs.shift(); if (serverQueue.songs[0]) { playYt(serverQueue.songs[0], client, i, connection) } else { if (connection.state.status === 'destroyed') return i.channel.send({ content: 'Connection is already destroyed.' }) i.channel.send({ content: 'No songs left to play, bye. 👋' }); connection.destroy(); queue.delete(guild.id); } } catch (e) { i.channel.send({ content: '**🚨ERROR🚨** CHECK CONSOLE' }) console.log(e) } }) player.on(AudioPlayerStatus.Playing, async () => { try { const playYtEmbed = new EmbedBuilder() .setTitle('Now playing') .setColor('Aqua') .setDescription(`[${search[0].title}](${search[0].url}) [${i.member}]`) .setThumbnail(search[0].thumbnails[0].url) .setFooter({ text: '© | Made by julianrs#3253', iconURL: 'https://cdn.discordapp.com/avatars/928010022624043049/7b546bec88f401d1eadcb1ae881f8412.png' }); i.channel.send({ embeds: [playYtEmbed] }) } catch (e) { i.channel.send('**🚨ERROR🚨** CHECK CONSOLE') console.log(e) } }) } catch (e) { i.channel.send('**🚨ERROR🚨** CHECK CONSOLE') console.log(e) } }