2019-10-13 17:52:51 +00:00
const YouTube = require ( "simple-youtube-api" ) ;
const he = require ( 'he' ) ;
2019-10-12 14:40:00 +00:00
module . exports = {
name : 'playlist' ,
usage : '[option]' ,
description : 'Save and load queues' ,
cooldown : 10 ,
async execute ( message , args , client , Discord , prefix ) {
2019-10-12 15:01:13 +00:00
const permissions = message . channel . permissionsFor ( message . author ) ;
2019-10-12 14:40:00 +00:00
const serverQueue = client . queue . get ( message . guild . id ) ;
2019-10-12 15:01:13 +00:00
if ( message . author . id !== '360363051792203779' ) {
2019-10-13 07:33:59 +00:00
if ( client . global . db . guilds [ message . guild . id ] . dj ) {
if ( ! message . member . roles . has ( client . global . db . guilds [ message . guild . id ] . djrole ) ) return message . channel . send ( ':x: You need the `DJ` role to modify or play the playlist!' ) ;
} else if ( ! permissions . has ( 'MANAGE_GUILD' ) ) return message . channel . send ( ':x: You need the `MANAGE_SERVER` permission to modify the playlist!' ) ;
2019-10-12 15:01:13 +00:00
}
2019-10-12 14:40:00 +00:00
if ( client . global . db . guilds [ message . guild . id ] . premium ) {
if ( args [ 1 ] === 'play' ) {
2019-10-13 13:01:46 +00:00
const voiceChannel = message . member . voiceChannel ;
2019-10-20 18:23:35 +00:00
if ( ! voiceChannel ) return message . channel . send ( ':x: I\'m sorry but you need to be in a voice channel to play music!' ) ;
2019-10-13 17:52:51 +00:00
const permissions = voiceChannel . permissionsFor ( message . client . user ) ;
if ( ! permissions . has ( 'CONNECT' ) ) {
return message . channel . send ( ':x: I cannot connect to your voice channel, make sure I have the proper permissions!' ) ;
}
if ( ! permissions . has ( 'SPEAK' ) ) {
return message . channel . send ( ':x: I cannot speak in your voice channel, make sure I have the proper permissions!' ) ;
}
let songs ;
2019-10-13 12:58:46 +00:00
if ( ! voiceChannel ) return message . channel . send ( ':x: I\'m sorry but you need to be in a voice channel to play music!' ) ;
2019-10-13 17:52:51 +00:00
if ( args [ 2 ] ) {
if ( client . global . db . guilds [ args [ 2 ] ] . premium && client . global . db . playlists [ args [ 2 ] ] . saved ) {
songs = client . global . db . playlists [ args [ 2 ] ] . songs ;
} else return message . channel . send ( ':x: There is no queue saved for this guild!' )
} else {
songs = client . global . db . playlists [ message . guild . id ] . songs ;
}
2019-10-12 14:40:00 +00:00
if ( client . global . db . playlists [ message . guild . id ] . saved ) {
if ( ! serverQueue ) {
const construct = {
textChannel : message . channel ,
voiceChannel : message . member . voiceChannel ,
connection : null ,
2019-10-13 17:52:51 +00:00
songs : [ ... songs ] ,
2019-10-12 14:40:00 +00:00
volume : client . global . db . guilds [ message . guild . id ] . defaultVolume ,
playing : true ,
looping : false
} ;
client . queue . set ( message . guild . id , construct ) ;
message . channel . send ( ":white_check_mark: Queue set!" ) ;
try {
var connection = await message . member . voiceChannel . join ( ) ;
construct . connection = connection ;
2019-10-24 17:11:08 +00:00
client . funcs . play ( message . guild , construct . songs [ 0 ] , client , message , 0 , false ) ;
2019-10-12 14:40:00 +00:00
} catch ( error ) {
client . queue . delete ( message . guild . id ) ;
return message . channel . send ( ` :x: An error occured: ${ error } ` ) ;
}
} else {
serverQueue . connection . dispatcher . end ( "queue set" ) ;
serverQueue . songs = [ ... client . global . db . playlists [ message . guild . id ] . songs ] ;
message . channel . send ( ":white_check_mark: Queue set!" ) ;
}
} else return message . channel . send ( ':x: There is no queue set for this server!' )
} else if ( args [ 1 ] === 'save' ) {
2019-10-13 10:59:41 +00:00
if ( ! serverQueue ) return message . channel . send ( ':x: There is nothing playing!' ) ;
2019-10-12 14:40:00 +00:00
client . global . db . playlists [ message . guild . id ] = {
songs : serverQueue . songs ,
firstSong : serverQueue . songs [ 0 ] ,
saved : true ,
} ;
message . channel . send ( ":white_check_mark: Queue saved!" ) ;
2019-10-13 17:52:51 +00:00
} else if ( args [ 1 ] === 'add' ) {
2019-10-24 17:11:08 +00:00
if ( client . global . db . playlists [ message . guild . id ] . saved ) {
2019-10-23 17:45:22 +00:00
const youtube = new YouTube ( client . config . apikey ) ;
const searchString = args . slice ( 2 ) . join ( " " ) ;
const url = args [ 2 ] ? args [ 2 ] . replace ( /<(.+)>/g , "$1" ) : "" ;
if ( ! args [ 2 ] ) return message . channel . send ( ':x: You need to use a link or search for a song!' ) ;
2019-10-13 17:52:51 +00:00
try {
2019-10-23 17:45:22 +00:00
var video = await youtube . getVideo ( url ) ;
} catch ( error ) {
2019-10-13 17:52:51 +00:00
try {
2019-10-23 17:45:22 +00:00
var videos = await youtube . searchVideos ( searchString , 10 ) ;
let index = 0 ;
const embed = new Discord . RichEmbed ( )
. setTitle ( "__Song Selection__" )
. setDescription ( ` ${ videos . map ( video2 => ` ** ${ ++ index } ** ${ he . decode ( video2 . title ) } ` ) . join ( '\n' ) } ` )
. setFooter ( "Please provide a number ranging from 1-10 to select one of the search results." )
. setColor ( "#b50002" )
message . channel . send ( embed ) ;
try {
var response = await message . channel . awaitMessages ( message2 => message2 . content > 0 && message2 . content < 11 , {
maxMatches : 1 ,
time : 10000 ,
errors : [ 'time' ]
} ) ;
} catch ( err ) {
console . error ( err ) ;
return message . channel . send ( ':x: Cancelling video selection' ) ;
}
const videoIndex = parseInt ( response . first ( ) . content ) ;
var video = await youtube . getVideoByID ( videos [ videoIndex - 1 ] . id ) ;
2019-10-13 17:52:51 +00:00
} catch ( err ) {
console . error ( err ) ;
2019-10-23 17:45:22 +00:00
return message . channel . send ( ':x: I could not obtain any search results!' ) ;
2019-10-13 17:52:51 +00:00
}
}
2019-10-23 17:45:22 +00:00
let song = {
id : video . id ,
title : Discord . Util . escapeMarkdown ( video . title ) ,
url : ` https://www.youtube.com/watch?v= ${ video . id } `
}
client . global . db . playlists [ message . guild . id ] . songs . push ( song ) ;
message . channel . send ( ` :white_check_mark: ${ song . title } added to the playlist! ` ) ;
} else return message . channel . send ( ':x: There is no playlist saved! Start by using the save option!' )
2019-10-24 17:11:08 +00:00
} else if ( args [ 1 ] === 'remove' ) {
if ( client . global . db . playlists [ message . guild . id ] . saved ) {
if ( ! args [ 2 ] ) return message . channel . send ( ':x: Please provide a number on the position of the song that you wan\'t to remove!' ) ;
const songNum = parseInt ( args [ 2 ] ) - 1 ;
if ( isNaN ( songNum ) ) return message . channel . send ( ':x: You need to enter a __number__!' ) ;
if ( songNum === 0 ) return message . channel . send ( ':x: You can not remove the currently playing song!' ) ;
if ( parseInt ( songNum ) > client . global . db . playlists [ message . guild . id ] . songs . size ) return message . channel . send ( ` :x: There is only ${ serverQueue . songs . size } amount of songs in the queue! ` ) ;
message . channel . send ( ` 🗑️ removed \` ${ client . global . db . playlists [ message . guild . id ] . songs [ songNum ] . title } \` from the queue! ` ) ;
return client . global . db . playlists [ message . guild . id ] . songs . splice ( songNum , 1 ) ;
} else return message . channel . send ( ':x: There is no playlist saved! Start by using the save option!' )
} else if ( args [ 1 ] === 'list' ) {
if ( args [ 2 ] ) {
if ( isNaN ( args [ 2 ] ) ) return msg . channel . send ( ':x: I\'m sorry, But you need to enter a valid __number__.' ) ;
}
let page = parseInt ( args [ 2 ] ) ;
if ( ! page ) page = 1 ;
let pagetext = ` :page_facing_up: Page: ${ page } :page_facing_up: `
let queuesongs = client . global . db . playlists [ message . guild . id ] . songs . slice ( ( page - 1 ) * 20 , page * 20 ) ;
let queuemessage = ` ${ queuesongs . map ( song => ` **#** ${ song . title } ` ) . join ( '\n' ) } `
const hashs = queuemessage . split ( '**#**' ) . length ;
for ( let i = 0 ; i < hashs ; i ++ ) {
queuemessage = queuemessage . replace ( '**#**' , ` ** ${ i + 1 } ** ` ) ;
}
const embed = new Discord . RichEmbed ( )
. setTitle ( "__playlist queue__" )
. setDescription ( ` ${ pagetext } \n ${ queuemessage } ` )
. setColor ( "#b50002" )
return message . channel . send ( embed ) ;
2019-10-12 14:40:00 +00:00
} else {
const embed = new Discord . RichEmbed ( )
. setTitle ( 'Options for playlist!' )
. addField ( 'play' , 'Play the guild specific queue.' , true )
2019-10-23 17:45:22 +00:00
. addField ( 'save' , 'Save the currently playing queue. Note that this will overwrite the currently saved queue!' , true )
2019-10-13 17:52:51 +00:00
. addField ( 'add' , 'Add songs to the playlist. Like song selection' , true )
2019-10-24 17:11:08 +00:00
. addField ( 'remove' , 'Remove songs from the playlist.' , true )
. addField ( 'list' , 'Display the playlist.' , true )
2019-10-13 17:52:51 +00:00
. setFooter ( ` how to use: ${ prefix } playlist <Option> <Optional option> ` )
2019-10-12 14:40:00 +00:00
. setAuthor ( client . user . username , client . user . displayAvatarURL )
. setColor ( '#b50002' )
return message . channel . send ( embed ) ;
}
} else return message . channel . send ( ":x: This is not a premium guild!" ) ;
} ,
} ;