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 youtube = require('play-dl'); const { SlashCommandBuilder } = require('@discordjs/builders') const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior, createAudioResource, StreamType, AudioPlayerStatus, AudioPlayer, getVoiceConnection } = 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, }, ], }, { name: 'p', description: 'Plays a song', options: [ { name: 'song', type: 'STRING', description: 'The song you want to play', required: true, }, ], }, { name: 'stop', description: 'Stop a certain song', options: [], }, { name: 'skip', description: 'Sjup the current song', options: [], }, ]); 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 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 songName = interaction.options.get('song').value const song = songName; const songInfo = await youtube.search(songName, { limit: 1 }) 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(songName, 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(`[${songInfo[0].title}](${songInfo[0].url}) [${interaction.member}]`) .setThumbnail(songInfo[0].thumbnail.url) .setFooter(`©️ Soundy Studios | All rights reserved!`, `https://cdn.discordapp.com/attachments/861355381774221312/881284765842542592/Soundy_Studios.png`) return interaction.reply({ embeds: [addedEmbed] }) } } if(cmd === 'stop'){ const voiceChannel = interaction.member.voice.channel if (!voiceChannel) return interaction.reply(`You must be in a voice channel to play music!`) const connect = getVoiceConnection(interaction.guild.id) stop(interaction.guild.id, client, interaction, connect) } } catch (error) { if (error) console.error(error); await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } }); async function stop(guild, client, interaction, connection) { const serverQueue = queue.get(guild.id) console.log(serverQueue) if(!serverQueue.songs[0]) return interaction.followUp({ content: 'No song is playing!'}); queue.delete(guild.id) connection.destroy() interaction.followUp({ content: 'Now leaving! Bye.'}) } async function skip(guild, client, interaction, connection) { const serverQueue = queue.get(guild.id) const player = getAudioPlayer({}) } async function play(songName, guild, client, interaction, connection) { const serverQueue = queue.get(guild.id) const player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play, } }) const streamm = await youtube.search(songName, { limit: 1 }) const stre = await youtube.stream(streamm[0].url) const resource = createAudioResource(stre.stream, { inputType: stre.type }) 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(); if(serverQueue.songs[0]){ play(serverQueue.songs[0], guild, client, interaction, connection) }else{ interaction.followUp({content: 'No songs left to play! Leaving voice channel.'}) connection.destroy(); queue.delete(guild.id) } }) player.on(AudioPlayerStatus.Playing, () => { const playEmbed = new Discord.MessageEmbed() .setTitle(`NOW PLAYING`) .setDescription(`[${streamm[0].title}](${streamm[0].url}) [${interaction.member}]`) .setThumbnail(streamm[0].thumbnail.url) .setFooter(`©️ Soundy Studios | All rights reserved!`, `https://cdn.discordapp.com/attachments/861355381774221312/881284765842542592/Soundy_Studios.png`) interaction.followUp({ embeds: [playEmbed] }) }) }