Fix rate limit

This commit is contained in:
Supositware 2022-12-18 22:53:23 +01:00
parent 8f9bb6b4f8
commit cfa6a55d39
2 changed files with 34 additions and 24 deletions

View file

@ -1,12 +1,12 @@
import { PermissionFlagsBits, InteractionType } from 'discord.js'; import { PermissionFlagsBits, InteractionType } from 'discord.js';
import db from '../../models/index.js'; import db from '../../models/index.js';
const ratelimit = {};
const { ownerId } = process.env; const { ownerId } = process.env;
export default { export default {
name: 'interactionCreate', name: 'interactionCreate',
async execute(interaction) { async execute(interaction) {
const ratelimit = global.ratelimit;
const client = interaction.client; const client = interaction.client;
if (interaction.type !== InteractionType.ApplicationCommand) return; if (interaction.type !== InteractionType.ApplicationCommand) return;
@ -52,26 +52,31 @@ export default {
*/ */
try { try {
if (!ratelimit[userID]) {
ratelimit[userID] = {};
}
const date = new Date(); const date = new Date();
if (ratelimit[userID]) { if (ratelimit[userID][commandName]) {
if (ratelimit[userID].cooldown) { if (ratelimit[userID][commandName].cooldown) {
if (commandName === ratelimit[userID].command && date > ratelimit[userID].cooldown) { if (date > ratelimit[userID][commandName].cooldown) {
ratelimit[userID].limit = 0; ratelimit[userID][commandName].limit = 0;
ratelimit[userID].cooldown = undefined; ratelimit[userID][commandName].cooldown = undefined;
}
}
if (command.ratelimit === ratelimit[userID][commandName].limit) {
return await interaction.reply({ content: `You are being rate limited. You can try again in ${Math.floor((ratelimit[userID][commandName].cooldown - date) / 1000)} seconds.`, ephemeral: true });
} }
} }
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) { if (command.ratelimit) {
ratelimit[userID] = { command: commandName, limit: ratelimit[userID] ? ratelimit[userID].limit + 1 : 1 }; ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName] ? ratelimit[userID][commandName].limit + 1 : 1 };
if (command.ratelimit === ratelimit[userID].limit) { if (command.ratelimit === ratelimit[userID][commandName].limit) {
date.setSeconds(date.getSeconds() + command.cooldown); date.setSeconds(date.getSeconds() + command.cooldown);
ratelimit[userID] = { command: commandName, limit: ratelimit[userID].limit, cooldown: date }; ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName].limit, cooldown: date };
} }
} }

View file

@ -5,7 +5,6 @@
import { ApplicationCommandOptionType, EmbedBuilder, PermissionFlagsBits } from 'discord.js'; import { ApplicationCommandOptionType, EmbedBuilder, PermissionFlagsBits } from 'discord.js';
import db from '../../models/index.js'; import db from '../../models/index.js';
import { rand } from '../../utils/rand.js'; import { rand } from '../../utils/rand.js';
const ratelimit = {};
const { ownerId, prefix } = process.env; const { ownerId, prefix } = process.env;
const prefixs = prefix.split(','); const prefixs = prefix.split(',');
@ -313,26 +312,32 @@ export default {
} }
try { try {
const ratelimit = global.ratelimit;
if (!ratelimit[userID]) {
ratelimit[userID] = {};
}
const date = new Date(); const date = new Date();
if (ratelimit[userID]) { if (ratelimit[userID][commandName]) {
if (ratelimit[userID].cooldown) { if (ratelimit[userID][commandName].cooldown) {
if (commandName === ratelimit[userID].command && date > ratelimit[userID].cooldown) { if (date > ratelimit[userID][commandName].cooldown) {
ratelimit[userID].limit = 0; ratelimit[userID][commandName].limit = 0;
ratelimit[userID].cooldown = undefined; ratelimit[userID][commandName].cooldown = undefined;
}
}
if (command.ratelimit === ratelimit[userID][commandName].limit) {
return await message.reply({ content: `You are being rate limited. You can try again in ${Math.floor((ratelimit[userID][commandName].cooldown - date) / 1000)} seconds.`, ephemeral: true });
} }
} }
if (commandName === ratelimit[userID].command && command.ratelimit === ratelimit[userID].limit) {
return await message.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) { if (command.ratelimit) {
ratelimit[userID] = { command: commandName, limit: ratelimit[userID] ? ratelimit[userID].limit + 1 : 1 }; ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName] ? ratelimit[userID][commandName].limit + 1 : 1 };
if (command.ratelimit === ratelimit[userID].limit) { if (command.ratelimit === ratelimit[userID][commandName].limit) {
date.setSeconds(date.getSeconds() + command.cooldown); date.setSeconds(date.getSeconds() + command.cooldown);
ratelimit[userID] = { command: commandName, limit: ratelimit[userID].limit, cooldown: date }; ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName].limit, cooldown: date };
} }
} }
message.user = message.author; message.user = message.author;