const { Inhibitor } = require('../../../interfaces/'); const { stripIndents } = require('common-tags'); class Throttle extends Inhibitor { constructor(client) { super(client, { name: 'throttle', priority: 20 }); } execute(message, command) { if (message.author.developer) return super._succeed(); const throttle = this.throttleCommand(message, command); if(throttle) { throttle.usages++; if(throttle.usages > command.throttling.usages) { const remaining = (throttle.start + (command.throttling.duration*1000) - Date.now()) / 1000; //eslint-disable-line no-extra-parens return super._fail({ remaining: remaining.toFixed(2) }); } } return super._succeed(); } throttleCommand(message, command) { if(!command.throttling) return undefined; let throttle = command._throttles.get(message.author.id); if(!throttle) { throttle = { start: Date.now(), usages: 0, timeout: this.client.setTimeout(() => { command._throttles.delete(message.author.id); }, command.throttling.duration*1000) }; command._throttles.set(message.author.id, throttle); } return throttle; } } module.exports = Throttle;