Skip to content

Commit

Permalink
Added Cooldowns
Browse files Browse the repository at this point in the history
Added Cooldowns to Slash Commands
  • Loading branch information
Elitex07 authored Jan 29, 2023
1 parent 22303f1 commit 80fafd7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Events/Guild/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const client = require('../../index');
const { EmbedBuilder, Collection, PermissionsBitField } = require(`discord.js`);
const cooldowns = new Map();
const ms = require('ms');

client.on('interactionCreate', async interaction => {
if (interaction.isContextMenuCommand()){
Expand Down Expand Up @@ -44,6 +46,38 @@ client.on('interactionCreate', async interaction => {
}

if(!interaction.member) interaction.member = await interaction.guild.members.fetch(interaction.user.id);

const command = cmd;
if(command){
//registering command in map to get cooldowns
if(!cooldowns.has(command.name)){
cooldowns.set(command.name, new Collection());
}

const current_time = Date.now();
const time_stamps = cooldowns.get(command.name);
const cooldown_amount = (command.cooldown) * 1000;

//If time_stamps has a key with the author's id then check the expiration time to send a message to a user.
if(time_stamps.has(interaction.user.id)){
const expiration_time = time_stamps.get(interaction.user.id) + cooldown_amount;

if(current_time < expiration_time){
const time_left = (expiration_time - current_time);

return interaction.reply({embeds: [
new EmbedBuilder()
.setDescription(`**You are on a cooldown of \`${ms(cooldown_amount)}\`.\n\nCooldown Ends: <t:${((Date.now() + time_left)/1000).toFixed(0)}:R>**`)
.setColor(interaction.guild.members.me.displayHexColor)
]});
}
}

//If the author's id is not in time_stamps then add them with the current time.
time_stamps.set(interaction.user.id, current_time);
//Delete the user's id once the cooldown is over.
setTimeout(() => time_stamps.delete(interaction.user.id), cooldown_amount);
}

cmd.execute(client, interaction, args);
});

0 comments on commit 80fafd7

Please sign in to comment.