2019-08-02 08:32:00 +00:00
const YouTube = require ( "simple-youtube-api" ) ;
2019-10-11 18:02:37 +00:00
const he = require ( 'he' ) ;
2024-02-09 09:53:30 +00:00
const { EmbedBuilder , PermissionFlagsBits } = require ( "discord.js" ) ;
2019-08-02 08:32:00 +00:00
module . exports = {
name : 'play' ,
description : 'Play command.' ,
usage : '[song name]' ,
2019-10-31 18:29:26 +00:00
alias : 'p' ,
2019-08-02 08:32:00 +00:00
args : true ,
cooldown : 3 ,
2024-02-09 09:53:30 +00:00
async execute ( message , args , client , prefix ) {
const youtube = new YouTube ( client . config . youtube _api _key ) ;
2019-08-02 08:32:00 +00:00
const searchString = args . slice ( 1 ) . join ( " " ) ;
const url = args [ 1 ] ? args [ 1 ] . replace ( /<(.+)>/g , "$1" ) : "" ;
const serverQueue = client . queue . get ( message . guild . id ) ;
2024-02-09 09:53:30 +00:00
const voiceChannel = message . member . voice . channel ;
2019-10-10 13:43:04 +00:00
if ( ! serverQueue ) {
if ( ! voiceChannel ) return message . channel . send ( ':x: I\'m sorry but you need to be in a voice channel to play music!' ) ;
} else {
2019-10-18 18:44:15 +00:00
if ( voiceChannel !== serverQueue . voiceChannel ) return message . channel . send ( ':x: I\'m sorry but you need to be in the same voice channel as Musix to play music!' ) ;
2019-10-10 13:43:04 +00:00
}
2019-08-03 06:46:34 +00:00
if ( ! args [ 1 ] ) return message . channel . send ( ':x: You need to use a link or search for a song!' ) ;
2019-08-02 08:32:00 +00:00
const permissions = voiceChannel . permissionsFor ( message . client . user ) ;
2024-02-09 09:53:30 +00:00
if ( ! permissions . has ( PermissionFlagsBits . Connect ) ) {
2019-08-02 08:32:00 +00:00
return message . channel . send ( ':x: I cannot connect to your voice channel, make sure I have the proper permissions!' ) ;
}
2024-02-09 09:53:30 +00:00
if ( ! permissions . has ( PermissionFlagsBits . Speak ) ) {
2019-10-10 13:43:04 +00:00
return message . channel . send ( ':x: I cannot speak in your voice channel, make sure I have the proper permissions!' ) ;
2019-08-02 08:32:00 +00:00
}
if ( url . match ( /^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/ ) ) {
const playlist = await youtube . getPlaylist ( url ) ;
const videos = await playlist . getVideos ( ) ;
for ( const video of Object . values ( videos ) ) {
const video2 = await youtube . getVideoByID ( video . id ) ;
2019-10-10 13:43:04 +00:00
await client . funcs . handleVideo ( video2 , message , voiceChannel , client , true ) ;
2019-08-02 08:32:00 +00:00
}
return message . channel . send ( ` :white_check_mark: Playlist: ** ${ playlist . title } ** has been added to the queue! ` ) ;
2019-11-14 17:56:01 +00:00
} else if ( client . global . db . guilds [ message . guild . id ] . songSelection ) {
2019-08-02 08:32:00 +00:00
try {
var video = await youtube . getVideo ( url ) ;
} catch ( error ) {
try {
var videos = await youtube . searchVideos ( searchString , 10 ) ;
let index = 0 ;
2024-02-09 09:53:30 +00:00
const embed = new EmbedBuilder ( )
2019-08-02 08:32:00 +00:00
. setTitle ( "__Song Selection__" )
2019-10-11 18:02:37 +00:00
. setDescription ( ` ${ videos . map ( video2 => ` ** ${ ++ index } ** ${ he . decode ( video2 . title ) } ` ) . join ( '\n' ) } ` )
2024-02-09 09:53:30 +00:00
. setFooter ( { text : "Please provide a number ranging from 1-10 to select one of the search results." } )
2019-12-05 13:17:15 +00:00
. setColor ( client . config . embedColor )
2024-02-09 09:53:30 +00:00
message . channel . send ( { embeds : [ embed ] } ) ;
2019-08-02 08:32:00 +00:00
try {
2024-02-09 09:53:30 +00:00
var response = await message . channel . awaitMessages ( {
filter : message2 => message2 . content > 0 && message2 . content < 11 && message2 . author === message . author ,
max : 1 ,
2019-08-02 08:32:00 +00:00
time : 10000 ,
errors : [ 'time' ]
} ) ;
} catch ( err ) {
console . error ( err ) ;
2019-10-11 17:50:49 +00:00
return message . channel . send ( ':x: Cancelling video selection' ) ;
2019-08-02 08:32:00 +00:00
}
const videoIndex = parseInt ( response . first ( ) . content ) ;
var video = await youtube . getVideoByID ( videos [ videoIndex - 1 ] . id ) ;
} catch ( err ) {
console . error ( err ) ;
2019-10-11 17:50:49 +00:00
return message . channel . send ( ':x: I could not obtain any search results!' ) ;
2019-08-02 08:32:00 +00:00
}
}
2019-10-10 13:43:04 +00:00
return client . funcs . handleVideo ( video , message , voiceChannel , client , false ) ;
2019-11-14 17:56:01 +00:00
} else {
try {
var video = await youtube . getVideo ( url ) ;
} catch ( error ) {
try {
var videos = await youtube . searchVideos ( searchString , 1 ) ;
var video = await youtube . getVideoByID ( videos [ 0 ] . id ) ;
} catch ( err ) {
console . error ( err ) ;
return message . channel . send ( ':x: I could not obtain any search results!' ) ;
}
}
return client . funcs . handleVideo ( video , message , voiceChannel , client , false ) ;
2019-08-02 08:32:00 +00:00
}
}
2019-10-11 18:02:37 +00:00
} ;