Add setRepeatMode function

This commit is contained in:
Androz2091 2020-01-18 11:03:22 +01:00
parent de86f7ffd3
commit 7b834cc9f2
3 changed files with 72 additions and 3 deletions

View file

@ -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}...`);
}
});
}

View file

@ -258,6 +258,23 @@ class Player {
});
}
/**
* Enable or disable the repeat mode
* @param {Boolean} enabled Whether the repeat mode should be enabled
* @returns {Promise<Void>}
*/
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

View file

@ -51,6 +51,11 @@ class Queue extends EventEmitter {
* @type {Boolean}
*/
this.playing = true;
/**
* Whether the repeat mode is enabled.
* @type {Boolean}
*/
this.repeatMode = false;
}
};