From 7b834cc9f2a5992f38344114b4145580c437cc6d Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Sat, 18 Jan 2020 11:03:22 +0100 Subject: [PATCH] Add setRepeatMode function --- README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/Player.js | 19 ++++++++++++++++++- src/Queue.js | 5 +++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index edb6943..9585442 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,10 @@ client.player.resume(guildID); client.player.stop(guildID); // Check if music is playing in a guild client.player.isPlaying(guildID); +// Current song will be repeated indefinitely +client.player.setRepeatMode(true); +// Current song will no longer be repeated indefinitely +client.player.setRepeatMode(false); ``` ### Event messages @@ -349,6 +353,45 @@ client.on('message', async (message) => { }); ``` +### Repeat + +To repeat the current song, use the `client.player.setRepeatMode()` function. + +**Usage:** + +```js +client.player.setRepeatMode(boolean); +``` + +**Example**: + +```js +client.on('message', async (message) => { + + const args = message.content.slice(settings.prefix.length).trim().split(/ +/g); + const command = args.shift().toLowerCase(); + + if(command === 'enable-repeat'){ + // Enable repeat mode + client.player.setRepeatMode(true); + // Get the current song + let queue = await client.player.getQueue(guildID); + let song = queue.songs[0]; + message.channel.send(`${song.name} will be repeated indefinitely!`); + } + + if(command === 'disable-repeat'){ + // Disable repeat mode + client.player.setRepeatMode(false); + // Get the current song + let queue = await client.player.getQueue(guildID); + let song = queue.songs[0]; + message.channel.send(`${song.name} will no longer be repeated indefinitely!`); + } + +}); +``` + ## Info Messages You can send a message when the queue ends or when the song changes: @@ -363,8 +406,12 @@ client.on('message', (message) => { song.queue.on('end', () => { message.channel.send('The queue is empty, please add new songs!'); }); - song.queue.on('songChanged', (oldSong, newSong) => { - message.channel.send(`Now playing ${newSong}...`); + song.queue.on('songChanged', (oldSong, newSong, skipped, repeatMode) => { + if(repeatMode){ + message.channel.send(`Playing ${newSong} again...`); + } else { + message.channel.send(`Now playing ${newSong}...`); + } }); } diff --git a/src/Player.js b/src/Player.js index 6947a03..2b9b873 100644 --- a/src/Player.js +++ b/src/Player.js @@ -258,6 +258,23 @@ class Player { }); } + /** + * Enable or disable the repeat mode + * @param {Boolean} enabled Whether the repeat mode should be enabled + * @returns {Promise} + */ + setRepeatMode(enabled) { + return new Promise(async(resolve, reject) => { + // Gets guild queue + let queue = this.queues.find((g) => g.guildID === guildID); + if(!queue) reject('Not playing'); + // Enable/Disable repeat mode + queue.repeatMode = enabled; + // Resolve + resolve(); + }); + } + /** * Start playing songs in a guild. * @ignore @@ -282,7 +299,7 @@ class Player { return queue.emit('end'); } // Emit songChanged event - if(!firstPlay) queue.emit('songChanged', queue.songs.shift(), queue.songs[0], queue.skipped); + if(!firstPlay) queue.emit('songChanged', (!queue.repeatMode ? queue.songs.shift() : queue.songs[0]), queue.songs[0], queue.skipped, queue.repeatMode); queue.skipped = false; let song = queue.songs[0]; // Download the song diff --git a/src/Queue.js b/src/Queue.js index df8ab50..dea9924 100644 --- a/src/Queue.js +++ b/src/Queue.js @@ -51,6 +51,11 @@ class Queue extends EventEmitter { * @type {Boolean} */ this.playing = true; + /** + * Whether the repeat mode is enabled. + * @type {Boolean} + */ + this.repeatMode = false; } };