Haha-Yes/utils/ratelimiter.js

35 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-12-18 23:20:53 +01:00
export default {
check,
};
function check(userID, commandName, commands) {
const ratelimit = global.ratelimit;
if (!ratelimit[userID]) {
ratelimit[userID] = {};
}
const date = new Date();
if (ratelimit[userID][commandName]) {
if (ratelimit[userID][commandName].cooldown) {
if (date > ratelimit[userID][commandName].cooldown) {
ratelimit[userID][commandName].limit = 0;
ratelimit[userID][commandName].cooldown = undefined;
}
}
if (commands.ratelimit === ratelimit[userID][commandName].limit) {
return `You are being rate limited. You can try again in ${Math.floor((ratelimit[userID][commandName].cooldown - date) / 1000)} seconds.`;
}
}
if (commands.ratelimit) {
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName] ? ratelimit[userID][commandName].limit + 1 : 1 };
if (commands.ratelimit === ratelimit[userID][commandName].limit) {
date.setSeconds(date.getSeconds() + commands.cooldown);
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName].limit, cooldown: date };
}
}
return false;
}