2019-04-06 20:45:12 +02:00
const { Command } = require ( 'discord-akairo' ) ;
const ytdl = require ( 'ytdl-core' ) ;
class playCommand extends Command {
constructor ( ) {
super ( 'play' , {
aliases : [ 'play' ] ,
category : 'utility' ,
args : [
{
id : 'ytblink' ,
type : 'string' ,
match : 'rest' ,
}
] ,
description : {
2019-04-06 21:34:39 +02:00
content : 'play music from the link you send ( This command might or might not stay in the bot, as it is basic, if people abuse it e.g: making play 10 hours song just for the purpose of making the bot slow, i will remove this command! )' ,
2019-04-06 20:45:12 +02:00
usage : '[youtube link]' ,
examples : [ 'https://www.youtube.com/watch?v=mzHWLLn5Z4A' ]
}
} ) ;
}
async exec ( message , args ) {
const voiceChannel = message . member . voice . channel ;
// If not in voice channel ask user to join
if ( ! voiceChannel ) {
return message . reply ( 'please join a voice channel first!' ) ;
} else
// If user say "stop" make the bot leave voice channel
if ( args . ytblink == 'stop' ) {
voiceChannel . leave ( ) ;
2019-04-06 21:05:46 +02:00
return message . channel . send ( 'I left the channel' ) ;
2019-04-06 20:45:12 +02:00
} else
voiceChannel . join ( ) . then ( connection => {
const stream = ytdl ( args . ytblink , { filter : 'audioonly' } ) ;
const dispatcher = connection . play ( stream ) ;
2019-04-06 21:05:46 +02:00
message . channel . send ( 'Playing it now!' ) ;
2019-04-06 20:45:12 +02:00
// End at then end of the audio stream
2019-04-06 21:05:46 +02:00
dispatcher . on ( 'end' , ( ) => {
voiceChannel . leave ( ) ;
return message . channel . send ( 'Music ended, i left the channel' ) ;
} ) ;
2019-04-06 20:45:12 +02:00
} ) ;
}
}
module . exports = playCommand ;