From e6dca692ed35861e3e6bf6b51d10a37c305a5982 Mon Sep 17 00:00:00 2001 From: Supositware Date: Sun, 4 Feb 2024 01:51:17 +0100 Subject: [PATCH] make ratelimiter async (also actually fix message) --- events/client/interactionCreate.js | 10 +++++----- events/client/messageCreate.js | 10 +++++----- utils/ratelimiter.js | 16 +++++++++------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/events/client/interactionCreate.js b/events/client/interactionCreate.js index ebec13c..25a44e2 100644 --- a/events/client/interactionCreate.js +++ b/events/client/interactionCreate.js @@ -71,16 +71,16 @@ export default { // Check if the limit of parallel execution has been reached if (command.parallelLimit) { - const doParallelLimit = ratelimiter.checkParallel(interaction.user, commandName, command); + const doParallelLimit = await ratelimiter.checkParallel(interaction.user, commandName, command); if (doParallelLimit.limited) { - return await interaction.reply({ content: doParallelLimit, ephemeral: true }); + return await interaction.reply({ content: doParallelLimit.msg, ephemeral: true }); } ratelimiter.addParallel(commandName); } // Check the ratelimit - const doRateLimit = ratelimiter.check(interaction.user, commandName, command); + const doRateLimit = await ratelimiter.check(interaction.user, commandName, command); if (doRateLimit) { return interaction.reply({ content: doRateLimit, ephemeral: true }); @@ -107,8 +107,8 @@ export default { } await command.execute(interaction, args, client) - .then(() => { - const hasPrallelLimit = ratelimiter.checkParallel(interaction.user, commandName, command); + .then(async () => { + const hasPrallelLimit = await ratelimiter.checkParallel(interaction.user, commandName, command); if (hasPrallelLimit) ratelimiter.removeParallel(commandName); }); } diff --git a/events/client/messageCreate.js b/events/client/messageCreate.js index 4e12855..38a1896 100644 --- a/events/client/messageCreate.js +++ b/events/client/messageCreate.js @@ -333,16 +333,16 @@ export default { // Check if the limit of parallel execution has been reached if (command.parallelLimit) { - const doParallelLimit = ratelimiter.checkParallel(message.author, commandName, command); + const doParallelLimit = await ratelimiter.checkParallel(message.author, commandName, command); if (doParallelLimit.limited) { - return await message.reply({ content: doParallelLimit, ephemeral: true }); + return await message.reply({ content: doParallelLimit.msg, ephemeral: true }); } ratelimiter.addParallel(commandName); } // Check the ratelimit - const doRateLimit = ratelimiter.check(message.author, commandName, command); + const doRateLimit = await ratelimiter.check(message.author, commandName, command); if (doRateLimit) { return message.reply({ content: doRateLimit, ephemeral: true }); @@ -432,8 +432,8 @@ export default { } await command.execute(message, args, client) - .then(() => { - const hasPrallelLimit = ratelimiter.checkParallel(message.author, commandName, command); + .then(async () => { + const hasPrallelLimit = await ratelimiter.checkParallel(message.author, commandName, command); if (hasPrallelLimit) ratelimiter.removeParallel(commandName); }); } diff --git a/utils/ratelimiter.js b/utils/ratelimiter.js index 7861902..cdd98d7 100644 --- a/utils/ratelimiter.js +++ b/utils/ratelimiter.js @@ -10,13 +10,15 @@ export default { removeParallel, checkParallel, }; -function check(user, commandName, commands) { +async function check(user, commandName, commands) { const userID = user.id; const userTag = user.username; // Don't apply the rate limit to bot owner - if (userID === ownerId) { - return false; + if (NODE_ENV !== 'development') { + if (user.id === ownerId) { + return false; + } } if (!ratelimit[userID]) { @@ -38,7 +40,7 @@ function check(user, commandName, commands) { const hours = Math.floor(minutes / 60); const dateString = `${hours > 0 ? ` ${Math.floor(hours)} hours` : ''}${minutes > 0 ? ` ${Math.floor(minutes % 60)} minutes` : ''}${seconds > 0 ? ` ${Math.floor(seconds % 60)} seconds` : ''}`; - const isOptOut = db.optout.findOne({ where: { userID: userID } }); + const isOptOut = await db.optout.findOne({ where: { userID: userID } }); const timestamp = new Date(); console.log(`[${timestamp.toISOString()}] \x1b[33m${ isOptOut ? 'A user' : `${userTag} (${userID})`}\x1b[0m is rate limited on \x1b[33m${commandName}\x1b[0m for${dateString}.`); @@ -56,7 +58,7 @@ function check(user, commandName, commands) { return false; } -function addParallel(commandName) { +async function addParallel(commandName) { // console.log(`[ADD] Adding parallel to ${commandName}`); if (!parallelLimit[commandName]) parallelLimit[commandName] = 0; @@ -67,7 +69,7 @@ function addParallel(commandName) { parallelLimit[commandName] = prevNumber + 1; } -function removeParallel(commandName) { +async function removeParallel(commandName) { // console.log(`[REMOVE] Removing parallel to ${commandName}`); // This shouldn't be possible @@ -81,7 +83,7 @@ function removeParallel(commandName) { // console.log(`[REMOVE] current parallel limit: ${JSON.stringify(parallelLimit)}`); } -function checkParallel(user, commandName, command) { +async function checkParallel(user, commandName, command) { // Don't apply the rate limit to bot owner if (NODE_ENV !== 'development') { if (user.id === ownerId) {