Skip to content

Commit

Permalink
Improvement to cooldown system (faster, only send one cooldown message)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaaz committed Nov 27, 2018
1 parent 608e435 commit f408291
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/Structures/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Command extends Base {
* Handle Cooldown
* User ID => Cooldown
*/
this._cooldown = {};
this._cooldown = new Map();

/**
* Shortcut - Reusable class
Expand Down Expand Up @@ -237,9 +237,13 @@ class Command extends Base {
}

/** Test for Cooldown - Send Cooldown message */
const cd = this._shouldCooldown(msg);
if (typeof (cd) === 'number') {
return this.sendCooldown(msg.channel, cd);
const [cd, cooldown] = this._shouldCooldown(msg);
if (cd) {
if (cooldown.post) {
cooldown.post = false;
return this.sendCooldown(msg.channel, cd);
}
return Promise.resolve();
}

if (this.options.deleteCommand) { // delete input
Expand All @@ -250,13 +254,13 @@ class Command extends Base {
if (args.length < this.options.argsMin && this.options.invalidUsage) {
return this.sendHelp({ msg, args, guildConf })
.then(() => {
this._cooldown[msg.author.id] = Date.now();
this._cooldown.set(msg.author.id, { time: Date.now(), post: true });
});
}

return msg.command.execute(message)
.then(() => {
this._cooldown[msg.author.id] = Date.now();
this._cooldown.set(msg.author.id, { time: Date.now(), post: true });
});
}

Expand Down Expand Up @@ -308,16 +312,17 @@ class Command extends Base {
}

/** Test for Cooldown - Send Cooldown message */
const cd = this._shouldCooldown(msg);
if (typeof (cd) === 'number') {
const [cd, cooldown] = this._shouldCooldown(msg);
if (cd && cooldown.post) {
cooldown.post = false;
return this.sendCooldown(msg.channel, cd);
}

/** Sends invalid usage message in case of invalid usage (not enough argument) [option enabled] */
if (args.length < this.options.argsMin && this.options.invalidUsage && !this.options.hidden) {
return this.sendHelp({ msg, args })
.then(() => {
this._cooldown[msg.author.id] = Date.now();
this._cooldown.set(msg.author.id, { time: Date.now(), post: true });
});
}

Expand All @@ -327,7 +332,7 @@ class Command extends Base {

return msg.command.execute(message)
.then(() => {
this._cooldown[msg.author.id] = Date.now();
this._cooldown.set(msg.author.id, { time: Date.now(), post: true });
});
}

Expand Down Expand Up @@ -420,23 +425,23 @@ class Command extends Base {
* @memberof Command
*/
_shouldCooldown(msg) {
const cooldown = this._cooldown[msg.author.id];
const cooldown = this._cooldown.get(msg.author.id);

// No cooldown registered yet
if (!cooldown) {
return false; // doesn't cooldown
return []; // doesn't cooldown
}

// Time spent since last uses <= cooldown chose for that command
const curCD = Date.now() - cooldown;
const curCD = Date.now() - cooldown.time;
if (curCD <= this.options.cooldown) {
return curCD; // Return time left (does cooldown)
return [curCD, cooldown]; // Return time left (does cooldown)
}

// Delete current time for this user.
delete this._cooldown[msg.author.id];
this._cooldown.delete(msg.author.id);

return false; // Doesn't cooldown
return []; // Doesn't cooldown
}

/**
Expand Down

0 comments on commit f408291

Please sign in to comment.