const Discord = require('discord.js'); const { Util } = require('discord.js'); const ytdl = require('ytdl-core'); const ytSearch = require('yt-search'); const { AudioPlayerStatus, StreamType, createAudioPlayer, createAudioResource, joinVoiceChannel, } = require('@discordjs/voice'); module.exports = { name: 'play', description: "Play music", aliases: ["p"], async execute(message, args) { //--------------Warnings--------------\\ const joinEmbed = new Discord.MessageEmbed() .setColor('RED') .setDescription('Please join a voice channel first') const { channel } = message.member.voice if(!channel) return message.channel.send({embeds:[joinEmbed]}); //-------------------------------------\\ const serverQueue = message.client.queue.get(message.guild.id); const songInfo = await ytdl.getInfo(args[0]) const song = { title: Util.escapeMarkdown(songInfo.videoDetails.title), url: songInfo.videoDetails.video_url }; if(serverQueue){ serverQueue.songs.push(song) } else{ const queueConstruct = { textChannel: message.channel, voiceChannel: channel, connection: null, songs: [], volume: 1.3, playing: true }; message.client.queue.set(message.guild.id, queueConstruct); queueConstruct.songs.push(song); const connection = joinVoiceChannel({ channelId: queueConstruct.voiceChannel.id, guildId: message.guild.id, adapterCreator: message.guild.voiceAdapterCreator, }); const stream = ytdl(queueConstruct.songs[0].url, { filter: 'audioonly' }); const resource = createAudioResource(stream, { inputType: StreamType.Arbitrary, inlineVolume: true }); resource.volume.setVolume(queueConstruct.volume); const player = createAudioPlayer(); player.play(resource); connection.subscribe(player); player.on(AudioPlayerStatus.Idle, () => { queueConstruct.songs.shift() const stream = ytdl(queueConstruct.songs[0].url, { filter: 'audioonly' }); const resource = createAudioResource(stream, { inputType: StreamType.Arbitrary, inlineVolume: true }); resource.volume.setVolume(queueConstruct.volume); const player = createAudioPlayer(); player.play(resource); connection.subscribe(player); }); } } }