const Discord = require('discord.js'); const fs = require('fs'); const { REST } = require('@discordjs/rest') const { Routes } = require('discord-api-types/v9') const { Intents, Client, Collection } = require("discord.js"); const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES], partials: ["CHANNEL", "MESSAGE", "REACTION", "GUILD_MEMBER", "USER"] }) const ytdl = require('ytdl-core'); const { SlashCommandBuilder } = require('@discordjs/builders') const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior, createAudioResource, StreamType, AudioPlayerStatus, AudioPlayer } = require('@discordjs/voice'); const ffmpeg = require('ffmpeg-static') const queue = new Map(); const { prefix, token } = require('./config.json') client.login(token) client.commands = new Collection() const eventsFolder = fs.readdirSync("./Events").filter(f => f.endsWith(".js")) for (const fil of eventsFolder) { const file = require(`./Events/${fil}`) if (file.once) { client.once(file.name, (...args) => file.execute(...args, client)) } else { client.on(file.name, (...args) => file.execute(...args, client)) } } const commandFiles = fs.readdirSync('./Commands').filter(f => f.endsWith('.js')) const commands = []; const clientId = '694131404316999721' const guildId = '849957133646954536' for (const file of commandFiles) { const command = require(`./Commands/${file}`); client.commands.set(command.data.name, command); commands.push(command.data.toJSON()) } const rest = new REST({ version: '9' }).setToken(token); (async () => { try { console.log('Succesfully refreshing application (/) commands.') await rest.put( Routes.applicationGuildCommands(clientId, guildId), { body: commands } ) console.log('Succesfully reloaded application (/) commands.') } catch (err) { console.error(err) } })(); client.on('messageCreate', async msg => { if(!msg.guild) return; if(!client.application?.owner) await client.application?.fetch() if(msg.content.startsWith('!deploy') && msg.author.id === client.application?.owner?.id) { await msg.guild.commands.set([ { name: 'play', description: 'Plays a song', options: [ { name: 'song', type: 'STRING', description: 'The song you want to play', required: true, }, ], }, ]); await msg.reply('Deployed!') } }) client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; const cmd = interaction.commandName try { if (cmd === 'play' || cmd === 'p') { await interaction.defer() const songToPlay = interaction.options.get('song').value 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 }) interaction.reply({content: 'Looking for the song!'}) play(song, interaction.guild, client, interaction, connection) } 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); 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] }) } } } catch (error) { if (error) console.error(error); await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } }); function play(song, guild, client, interaction, connection) { const serverQueue = queue.get(guild.id) if(!song) { connection.destroy(); queue.delete(guild.id) } const player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Pause, } }) const stream = ytdl(song.url, { filter: 'audioonly' }) const resource = createAudioResource(stream, { inputType: StreamType.Arbitrary }) player.play(resource) connection.subscribe(player) player.on('error', async error => { interaction.followUp({ content: 'An error occured playing that song, skipping song!'}) }) player.on(AudioPlayerStatus.Idle, async () => { serverQueue.songs.shift(); play(serverQueue.songs[0], guild, client, interaction, connection) }) player.on(AudioPlayerStatus.Playing, () => { 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`) interaction.followUp({ embeds: [playEmbed] }) }) }