🐛 Fix cooldowns timeout handlers

This commit is contained in:
Androz2091 2021-02-18 22:50:46 +01:00
parent 32e76dc2a1
commit c974d24591

View file

@ -10,7 +10,6 @@ const Util = require('./Util')
const { EventEmitter } = require('events') const { EventEmitter } = require('events')
const Client = new soundcloud.Client() const Client = new soundcloud.Client()
const { VimeoExtractor, DiscordExtractor, FacebookExtractor, ReverbnationExtractor, XVideosExtractor } = require('./Extractors/Extractor') const { VimeoExtractor, DiscordExtractor, FacebookExtractor, ReverbnationExtractor, XVideosExtractor } = require('./Extractors/Extractor')
var timeout
/** /**
* @typedef Filters * @typedef Filters
@ -152,6 +151,12 @@ class Player extends EventEmitter {
* @type {Discord.Collection<string, Discord.Collector>} * @type {Discord.Collection<string, Discord.Collector>}
*/ */
this._resultsCollectors = new Discord.Collection() this._resultsCollectors = new Discord.Collection()
/**
* @private
* @type {Discord.Collection<string, Timeout>}
*/
this._cooldownsTimeout = new Discord.Collection()
} }
/** /**
@ -722,7 +727,11 @@ class Player extends EventEmitter {
* client.player.play(message, "Despacito", true); * client.player.play(message, "Despacito", true);
*/ */
async play (message, query, firstResult = false) { async play (message, query, firstResult = false) {
clearTimeout(timeout) if (this._cooldownsTimeout.has(`end_${message.guild.id}`)) {
clearTimeout(this._cooldownsTimeout.get(`end_${message.guild.id}`))
this._cooldownsTimeout.delete(`end_${message.guild.id}`)
}
if (!query || typeof query !== 'string') throw new Error('Play function requires search query but received none!') if (!query || typeof query !== 'string') throw new Error('Play function requires search query but received none!')
// clean query // clean query
@ -1049,21 +1058,29 @@ class Player extends EventEmitter {
// process leaveOnEmpty checks // process leaveOnEmpty checks
if (!this.options.leaveOnEmpty) return if (!this.options.leaveOnEmpty) return
// If the member leaves a voice channel // If the member joins a voice channel
if (!oldState.channelID || newState.channelID) return if (!oldState.channelID || newState.channelID) {
const emptyTimeout = this._cooldownsTimeout.get(`empty_${oldState.guild.id}`)
// If the channel is not empty const channelEmpty = this.util.isVoiceEmpty(queue.voiceConnection.channel)
if (!this.util.isVoiceEmpty(queue.voiceConnection.channel)) return if (!channelEmpty && emptyTimeout) {
setTimeout(() => { clearTimeout(emptyTimeout)
this._cooldownsTimeout.delete(`empty_${oldState.guild.id}`)
}
} else {
// If the channel is not empty
if (!this.util.isVoiceEmpty(queue.voiceConnection.channel)) return if (!this.util.isVoiceEmpty(queue.voiceConnection.channel)) return
if (!this.queues.has(queue.guildID)) return const timeout = setTimeout(() => {
// Disconnect from the voice channel if (!this.util.isVoiceEmpty(queue.voiceConnection.channel)) return
queue.voiceConnection.channel.leave() if (!this.queues.has(queue.guildID)) return
// Delete the queue // Disconnect from the voice channel
this.queues.delete(queue.guildID) queue.voiceConnection.channel.leave()
// Emit end event // Delete the queue
this.emit('channelEmpty', queue.firstMessage, queue) this.queues.delete(queue.guildID)
}, this.options.leaveOnEmptyCooldown || 0) // Emit end event
this.emit('channelEmpty', queue.firstMessage, queue)
}, this.options.leaveOnEmptyCooldown || 0)
this._cooldownsTimeout.set(`empty_${oldState.guild.id}`, timeout)
}
} }
_playYTDLStream (queue, updateFilter, seek) { _playYTDLStream (queue, updateFilter, seek) {
@ -1140,11 +1157,12 @@ class Player extends EventEmitter {
if (queue.tracks.length === 1 && !queue.repeatMode && !firstPlay) { if (queue.tracks.length === 1 && !queue.repeatMode && !firstPlay) {
// Leave the voice channel // Leave the voice channel
if (this.options.leaveOnEnd && !queue.stopped) { if (this.options.leaveOnEnd && !queue.stopped) {
// Remove the guild from the guilds list // Remove the guild from the guilds list
this.queues.delete(queue.guildID) this.queues.delete(queue.guildID)
timeout = setTimeout(() => { const timeout = setTimeout(() => {
queue.voiceConnection.channel.leave() queue.voiceConnection.channel.leave()
}, this.options.leaveOnEndCooldown || 0) }, this.options.leaveOnEndCooldown || 0)
this._cooldownsTimeout.set(`end_${queue.guildID}`, timeout)
} }
// Remove the guild from the guilds list // Remove the guild from the guilds list
this.queues.delete(queue.guildID) this.queues.delete(queue.guildID)