Add leaveOnEmpty and voiceStateUpdate listener

This commit is contained in:
Androz2091 2020-01-12 18:55:37 +01:00
parent d9f92bcd35
commit ae366e66d9

View file

@ -12,10 +12,12 @@ const Song = require('./Song');
* *
* @property {Boolean} leaveOnEnd Whether the bot should leave the current voice channel when the queue ends. * @property {Boolean} leaveOnEnd Whether the bot should leave the current voice channel when the queue ends.
* @property {Boolean} leaveOnStop Whether the bot should leave the current voice channel when the stop() function is used. * @property {Boolean} leaveOnStop Whether the bot should leave the current voice channel when the stop() function is used.
* @property {Boolean} leaveOnEmpty Whether the bot should leave the voice channel if there is no more member in it.
*/ */
const PlayerOptions = { const PlayerOptions = {
leaveOnEnd: true, leaveOnEnd: true,
leaveOnStop: true leaveOnStop: true,
leaveOnEmpty: true
}; };
class Player { class Player {
@ -53,6 +55,23 @@ class Player {
* @type {PlayerOptions} * @type {PlayerOptions}
*/ */
this.options = mergeOptions(PlayerOptions, options); this.options = mergeOptions(PlayerOptions, options);
// Listener to check if the channel is empty
client.on('voiceStateUpdate', (oldMember, newMember) => {
if(!this.options.leaveOnEmpty) return;
// If the member leaves a voice channel
if(oldMember.voice.channel && !newMember.voice.channel) return;
// Search for a queue for this channel
let queue = this.queues.find((g) => g.connection.channel.id === oldMember.voice.channel.id);
if(queue){
// Disconnect from the voice channel
queue.connection.channel.leave();
// Delete the queue
this.queues = this.queues.filter((g) => g.guildID !== queue.guildID);
// Emit end event
this.emit('channelEmpty');
}
});
} }
/** /**