@ -1,4 +1,4 @@
const ytdl = require ( "ytdl-core ") ,
const ytdl = require ( "ytdl-core -discord ") ,
ypi = require ( 'youtube-playlist-info' ) ,
yttl = require ( 'get-youtube-title' ) ,
config = require ( '../../config.json' ) ,
@ -11,7 +11,7 @@ const ytdl = require("ytdl-core"),
* The Music Player class is used to handle music playing tasks on Discord Servers ( Guilds ) .
* @ type { MusicPlayer }
* /
class MusicPlayer extends xevents . ExtendedEventEmitter {
class MusicPlayer extends xevents . ExtendedEventEmitter {
/ * *
* Constructor
* @ param [ voiceChannel ] { Discord . VoiceChannel }
@ -24,13 +24,12 @@ class MusicPlayer extends xevents.ExtendedEventEmitter{
this . playing = false ;
this . current = null ;
this . repeat = false ;
this . volume = 0.5 ;
this . voiceChannel = voiceChannel ;
this . exitTimeout = null ;
this . _logger = new logging . Logger ( this ) ;
this . _logger . silly ( 'Initialized Music Player' ) ;
config . music ? this . quality = config . music . quality || 'lowest' : this . quality = 'lowest' ;
config . music ? this . liveBuffer = config . music . liveBuffer || 10000 : 10000 ;
config . music ? this . quality = config . music . quality || 'lowest' : this . quality = 'lowest' ;
config . music ? this . liveBuffer = config . music . liveBuffer || 10000 : 10000 ;
}
/ * *
@ -132,7 +131,7 @@ class MusicPlayer extends xevents.ExtendedEventEmitter{
* If the url is a playlist , the videos of the playlist are fetched and put
* in the queue . For each song the title is saved in the queue too .
* @ param url { String }
* @ param playnext { Boolean }
* @ param [ playnext ] { Boolean }
* /
async playYouTube ( url , playnext ) {
let plist = utils . YouTube . getPlaylistIdFromUrl ( url ) ;
@ -143,7 +142,7 @@ class MusicPlayer extends xevents.ExtendedEventEmitter{
let firstSongTitle = null ;
try {
firstSongTitle = await this . getVideoName ( firstSong ) ;
} catch ( err ) {
} catch ( err ) {
if ( err . message !== 'Not found' ) {
this . _logger . warn ( err . message ) ;
this . _logger . debug ( err . stack ) ;
@ -172,32 +171,37 @@ class MusicPlayer extends xevents.ExtendedEventEmitter{
this . current = ( { 'url' : url , 'title' : await this . getVideoName ( url ) } ) ;
if ( this . repeat )
this . queue . push ( this . current ) ;
let toggleNext = ( ) => {
if ( this . queue . length > 0 ) {
this . current = this . queue . shift ( ) ;
this . emit ( 'next' , this . current ) ;
this . playYouTube ( this . current . url ) . catch ( ( err ) => this . _logger . warn ( err . message ) ) ;
} else {
this . stop ( ) ;
}
} ;
try {
this . disp = this . conn . playStream ( ytdl ( url ,
{ filter : 'audioonly' , quality : this . quality , liveBuffer : this . liveBuffer } ) ,
{ volume : this . volume } ) ;
this . disp . on ( 'error' , ( err ) => {
this . _logger . error ( err . message ) ;
this . _logger . debug ( err . stack ) ;
} ) ;
this . disp = this . conn . playOpusStream ( await ytdl ( url ,
{ filter : 'audioonly' , quality : this . quality , liveBuffer : this . liveBuffer } ) ) ;
this . disp . on ( 'error' , ( err ) => {
this . _logger . error ( err . message ) ;
this . _logger . debug ( err . stack ) ;
} ) ;
this . disp . on ( 'end' , ( reason ) => { // end event triggers the next song to play when the reason is not stop
if ( reason !== 'stop' ) {
this . playing = false ;
this . current = null ;
if ( this . queue . length > 0 ) {
this . current = this . queue . shift ( ) ;
if ( this . repeat ) // listen on repeat
this . queue . push ( this . current ) ;
this . emit ( 'next' , this . current ) ;
this . playYouTube ( this . current . url ) . catch ( ( err ) => this . _logger . warn ( err . message ) ) ;
} else {
this . stop ( ) ;
this . disp . on ( 'end' , ( reason ) => { // end event triggers the next song to play when the reason is not stop
if ( reason !== 'stop' ) {
this . playing = false ;
this . current = null ;
toggleNext ( ) ;
}
}
} ) ;
this . playing = true ;
} ) ;
this . playing = true ;
} catch ( err ) {
this . _logger . verbose ( err . message ) ;
this . _logger . silly ( err . stack ) ;
toggleNext ( ) ;
}
} else {
this . _logger . debug ( ` Added ${ url } to the queue ` ) ;
if ( playnext )
@ -226,17 +230,6 @@ class MusicPlayer extends xevents.ExtendedEventEmitter{
} ) ;
}
/ * *
* Sets the volume of the dispatcher to the given value
* @ param percentage { Number }
* /
setVolume ( percentage ) {
this . _logger . verbose ( ` Setting volume to ${ percentage } ` ) ;
this . volume = percentage ;
if ( this . disp !== null )
this . disp . setVolume ( percentage ) ;
}
/ * *
* Pauses if a dispatcher exists
* /
@ -244,7 +237,7 @@ class MusicPlayer extends xevents.ExtendedEventEmitter{
this . _logger . verbose ( "Pausing music..." ) ;
if ( this . disp !== null )
this . disp . pause ( ) ;
else
else
this . _logger . warn ( "No dispatcher found" ) ;
}
@ -256,7 +249,7 @@ class MusicPlayer extends xevents.ExtendedEventEmitter{
this . _logger . verbose ( "Resuming music..." ) ;
if ( this . disp !== null )
this . disp . resume ( ) ;
else
else
this . _logger . warn ( "No dispatcher found" ) ;
}