const Command = require('../Structures/Command.js') const Discord = require('discord.js') const ytdl = require('ytdl-core') const { ytApiKey } = require('../config.json') const axios = require('axios').default; module.exports = new Command({ name: "play", description: "Play a song!", async run(message, args, client) { const url = args[1] var videoId = url.split("v=")[1] const { getVoiceConnection, joinVoiceChannel, AudioPlayerStatus, createAudioResource, getNextResource, createAudioPlayer, NoSubscriberBehavior, StreamType } = require('@discordjs/voice'); //Input Command in here const res = await axios.get("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + videoId + "&key=" + ytApiKey) if (message.member.voice.channel == null){ return message.reply(`You are not in a voice channel!`) } if (!args[1]) { message.reply(`You have to specify a song for me to play!`) } const connection = joinVoiceChannel({ channelId: message.member.voice.channel.id, guildId: message.guild.id, adapterCreator: message.guild.voiceAdapterCreator, }) const stream = ytdl(args[1], { filter: 'audioonly' }) const resource = createAudioResource(stream, { inputType: StreamType.Arbitrary }) const player = createAudioPlayer() const subscription = connection.subscribe(player) player.play(resource) message.reply(`Now playing: **${res.data.items[0].snippet.title}**`) player.on(AudioPlayerStatus.Idle, () => { player.stop() connection.destroy() message.channel.send(`Song's over, im outta here.`) }) player.on('error', error => { console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`); player.play(getNextResource()) }) } })