Skip to content

Commit

Permalink
Add command cooldowns to prevent glitches when commands are sent too …
Browse files Browse the repository at this point in the history
…quickly
  • Loading branch information
eritislami committed Jun 15, 2020
1 parent b891ce8 commit 6fadd1d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions index.js
Expand Up @@ -12,6 +12,7 @@ client.login(TOKEN);
client.commands = new Collection();
client.prefix = PREFIX;
client.queue = new Map();
const cooldowns = new Collection();

/**
* Client Events
Expand Down Expand Up @@ -46,6 +47,28 @@ client.on("message", async (message) => {

if (!command) return;

if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 1) * 1000;

if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(
`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`
);
}
}

timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

try {
command.execute(message, args);
} catch (error) {
Expand Down

0 comments on commit 6fadd1d

Please sign in to comment.