const ytdl = require('ytdl-core'); const ytSearch = require('yt-search'); const Discord = require('discord.js'); const {joinVoiceChannel} = require('@discordjs/voice') module.exports = { name: 'play', description: 'Joins and plays video audio from youtube', async execute(message, args) { const connection = joinVoiceChannel({ channelId: message.member.voice.channelId, guildId: message.guild.id, selfDeaf: false, selfMute: false, adapterCreator: message.channel.guild.voiceAdapterCreator }); const voiceChannel = message.member.voice.channel; if (!voiceChannel) { let noChannel = new Discord.MessageEmbed() .setDescription(`You need to be in a voice channel to use this command.`) .setColor('#000000'); return message.channel.send({ embeds: [noChannel] }); } const permissions = voiceChannel.permissionsFor(message.client.user); if (!permissions.has('CONNECT') || !voiceChannel.joinable) { let noJoin = new Discord.MessageEmbed() .setDescription(`I lack the permissions to connect to your voice channel`) .setColor('#000000'); return message.channel.send({ embeds: [noJoin] }); } if (!permissions.has('SPEAK')) { let noSpeak = new Discord.MessageEmbed() .setDescription(`I lack the permissions to speak in your voice channel`) .setColor('#000000'); return message.channel.send({ embeds: [noSpeak] }); } if (!args.length) { let noCommand = new Discord.MessageEmbed() .setTitle(`Command: !o play`) .addField('Example:', `!o play https://www.youtube.com/watch?v=dQw4w9WgXcQ\n!o play minecraft ost`) .setColor('#000000'); return message.channel.send({ embeds: [noCommand] }); } const validURL = (str) => { var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/; if (!regex.test(str)) { return false; } else { return true; } }; if (validURL(args[0])) { await joinVoiceChannel({voiceChannel}); const stream = ytdl(args[0], { filter: 'audioonly' }); const resource = createAudioResource(stream, { inputType: StreamType.Arbitrary }); const player = createAudioPlayer(); player.play(resource); connection.subscribe(player); player.on(AudioPlayerStatus.Idle, () => connection.destroy({ timeout: 120000 })); // connection.play(stream, { seek: 0, volume: 1 }).on('finish', () => { // connection.destroy({ timeout: 120000 }); // }); ytdl.getInfo(`${args[0]}`).then((info) => { const playingURL = new Discord.MessageEmbed() .setDescription(`Now playing ***${info.videoDetails.title}***`) .setColor('#000000'); message.channel.send({ embeds: [playingURL] }); return; }); return; } const videoFinder = async (query) => { const videoResult = await ytSearch(query); return videoResult.videos.length > 1 ? videoResult.videos[0] : null; }; const video = await videoFinder(args.join(' ')); if (video) { const stream = ytdl(video.url, { filter: 'audioonly' }); const resource = createAudioResource(stream, { inputType: StreamType.Arbitrary }); const player = createAudioPlayer(); player.play(resource); connection.subscribe(player); player.on(AudioPlayerStatus.Idle, () => connection.destroy({ timeout: 120000 })); const playingName = new Discord.MessageEmbed() .setDescription(`Now playing ***${video.title}***`) .setColor('#000000'); await message.channel.send({ embeds: [playingName] }); } else { let noResults = new Discord.MessageEmbed().setDescription('No video results found.').setColor('#000000'); return message.channel.send({ embeds: [noResults] }); } } };