Add ratelimit and cooldowns

This commit is contained in:
Supositware 2022-08-14 23:28:24 +02:00
parent a97d41a43d
commit 4b0fcc7b36
2 changed files with 30 additions and 2 deletions

View file

@ -25,6 +25,8 @@ export default {
option.setName('image') option.setName('image')
.setDescription('Optional attachment (Image only.)') .setDescription('Optional attachment (Image only.)')
.setRequired(false)), .setRequired(false)),
ratelimit: 3,
cooldown: 3600,
async execute(interaction) { async execute(interaction) {
await interaction.deferReply({ ephemeral: false }); await interaction.deferReply({ ephemeral: false });
const client = interaction.client; const client = interaction.client;

View file

@ -1,4 +1,5 @@
import db from '../../models/index.js'; import db from '../../models/index.js';
const ratelimit = {};
export default { export default {
name: 'interactionCreate', name: 'interactionCreate',
async execute(interaction) { async execute(interaction) {
@ -14,13 +15,38 @@ export default {
return interaction.reply({ content: 'You are blacklisted.', ephemeral: true }); return interaction.reply({ content: 'You are blacklisted.', ephemeral: true });
} }
const command = client.commands.get(interaction.commandName); const userTag = interaction.user.tag;
const userID = interaction.user.id;
const commandName = interaction.commandName;
console.log(`\x1b[33m${interaction.user.tag} (${interaction.user.id})\x1b[0m launched command \x1b[33m${interaction.commandName}\x1b[0m`); const command = client.commands.get(commandName);
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m launched command \x1b[33m${commandName}\x1b[0m`);
if (!command) return; if (!command) return;
try { try {
const date = new Date();
if (ratelimit[userID]) {
if (ratelimit[userID].cooldown) {
if (commandName === ratelimit[userID].command && date > ratelimit[userID].cooldown) {
ratelimit[userID].limit = 0;
ratelimit[userID].cooldown = undefined;
}
}
if (commandName === ratelimit[userID].command && command.ratelimit === ratelimit[userID].limit) {
return await interaction.reply({ content: `You are being rate limited. You can try again in ${Math.floor((ratelimit[userID].cooldown - date) / 1000)} seconds.`, ephemeral: true });
}
}
if (command.ratelimit) {
ratelimit[userID] = { command: commandName, limit: ratelimit[userID] ? ratelimit[userID].limit + 1 : 1 };
if (command.ratelimit === ratelimit[userID].limit) {
date.setSeconds(date.getSeconds() + command.cooldown);
ratelimit[userID] = { command: commandName, limit: ratelimit[userID].limit, cooldown: date };
}
}
await command.execute(interaction); await command.execute(interaction);
} }
catch (error) { catch (error) {