From ae366e66d9420e21d37439b815ae087df5a3ce2a Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Sun, 12 Jan 2020 18:55:37 +0100 Subject: [PATCH] Add leaveOnEmpty and voiceStateUpdate listener --- src/Player.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Player.js b/src/Player.js index 8ffd070..a262231 100644 --- a/src/Player.js +++ b/src/Player.js @@ -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} 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 = { leaveOnEnd: true, - leaveOnStop: true + leaveOnStop: true, + leaveOnEmpty: true }; class Player { @@ -53,6 +55,23 @@ class Player { * @type {PlayerOptions} */ 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'); + } + }); } /**