Add support for command cooldowns
This commit is contained in:
parent
e5df703c26
commit
5ca044b735
|
@ -1,4 +1,4 @@
|
||||||
const { Events } = require('discord.js');
|
const { Events, Collection } = require('discord.js');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: Events.InteractionCreate,
|
name: Events.InteractionCreate,
|
||||||
|
@ -14,6 +14,29 @@ module.exports = {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { cooldowns } = interaction.client;
|
||||||
|
|
||||||
|
if (!cooldowns.has(command.data.name)) {
|
||||||
|
cooldowns.set(command.data.name, new Collection());
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
|
const timestamps = cooldowns.get(command.data.name);
|
||||||
|
const defaultCooldownDuration = 3;
|
||||||
|
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;
|
||||||
|
|
||||||
|
if (timestamps.has(interaction.user.id)) {
|
||||||
|
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
|
||||||
|
|
||||||
|
if (now < expirationTime) {
|
||||||
|
const expiredTimestamp = Math.round(expirationTime / 1000);
|
||||||
|
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, ephemeral: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
timestamps.set(interaction.user.id, now);
|
||||||
|
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await command.execute(interaction);
|
await command.execute(interaction);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
Loading…
Reference in New Issue