From 50e49db47c30c1476e1c99f20d2c4a6b541f5d13 Mon Sep 17 00:00:00 2001 From: Supositware Date: Thu, 14 Dec 2023 00:23:49 +0100 Subject: [PATCH] show how many execution in parallel are currently running in help --- commands/utility/help.js | 8 ++++++++ events/client/interactionCreate.js | 4 ++-- events/client/messageCreate.js | 6 +++--- utils/ratelimiter.js | 14 ++++++++++---- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/commands/utility/help.js b/commands/utility/help.js index 3f4a6291..62574c65 100644 --- a/commands/utility/help.js +++ b/commands/utility/help.js @@ -1,5 +1,6 @@ import { SlashCommandBuilder, EmbedBuilder, AttachmentBuilder, PermissionsBitField } from 'discord.js'; import fs from 'node:fs'; +import ratelimiter from '../../utils/ratelimiter.js'; const { ownerId, prefix } = process.env; const prefixs = prefix.split(','); @@ -113,6 +114,13 @@ export default { embed.addFields({ name: 'Bot permission', value: `\`${perm.join('` `')}\``, inline: true }); } + if (command.parallelLimit) { + const paralellimit = ratelimiter.checkParallel(interaction.user, command.data.name, command); + + embed.addFields({ name: 'Current number of executions', value: `\`${paralellimit.current}\``, inline: false }); + embed.addFields({ name: 'Maximum number of executions', value: `\`${command.parallelLimit}\``, inline: true }); + } + if (fs.existsSync(`./asset/img/command/${command.category}/${command.data.name}.png`)) { const file = new AttachmentBuilder(`./asset/img/command/${command.category}/${command.data.name}.png`); embed.setImage(`attachment://${command.data.name}.png`); diff --git a/events/client/interactionCreate.js b/events/client/interactionCreate.js index 9485744f..9e9abf9a 100644 --- a/events/client/interactionCreate.js +++ b/events/client/interactionCreate.js @@ -71,8 +71,8 @@ export default { if (command.parallelLimit) { console.log('Command has a parallel limit'); const doParallelLimit = ratelimiter.checkParallel(interaction.user, commandName, command); - console.log(doParallelLimit); - if (doParallelLimit) { + console.log(doParallelLimit.limited); + if (doParallelLimit.limited) { return await interaction.reply({ content: doParallelLimit, ephemeral: true }); } diff --git a/events/client/messageCreate.js b/events/client/messageCreate.js index bf8bfb3f..d6e5e7b5 100644 --- a/events/client/messageCreate.js +++ b/events/client/messageCreate.js @@ -334,8 +334,8 @@ export default { if (command.parallelLimit) { console.log('Command has a parallel limit'); const doParallelLimit = ratelimiter.checkParallel(message.author, commandName, command); - console.log(doParallelLimit); - if (doParallelLimit) { + console.log(doParallelLimit.limited); + if (doParallelLimit.limited) { return await message.reply({ content: doParallelLimit, ephemeral: true }); } @@ -426,7 +426,7 @@ export default { } if (!isOptOut && argsLength > 0) { - console.log(`[${timestamp.toISOString()}]\x1b[33m⤷\x1b[0m with args ${JSON.stringify(args)}`); + console.log(`[${timestamp.toISOString()}] \x1b[33m⤷\x1b[0m with args ${JSON.stringify(args)}`); } await command.execute(message, args, client) diff --git a/utils/ratelimiter.js b/utils/ratelimiter.js index 0f47bbe1..e4210620 100644 --- a/utils/ratelimiter.js +++ b/utils/ratelimiter.js @@ -1,6 +1,6 @@ const ratelimit = {}; const parallelLimit = {}; -const { ownerId } = process.env; +const { ownerId, NODE_ENV } = process.env; import db from '../models/index.js'; @@ -85,13 +85,19 @@ function removeParallel(commandName) { function checkParallel(user, commandName, command) { // Don't apply the rate limit to bot owner - if (user.id === ownerId) return false; + if (NODE_ENV !== 'development') { + if (user.id === ownerId) { + return false; + } + } + + if (!parallelLimit[commandName]) parallelLimit[commandName] = 0; // console.log(`[CHECK] command limit: ${command.parallelLimit}`); // console.log(`[CHECK] current parallel executions: ${parallelLimit[commandName]}`); if (parallelLimit[commandName] >= command.parallelLimit) { - return 'There are currently too many parallel execution of this command, please wait before retrying.'; + return { limited: true, current: parallelLimit[commandName], max: command.parallelLimit, msg: `There are currently too many parallel execution of this command, please wait before retrying. (${parallelLimit[commandName]}/${command.parallelLimit})` }; } - return false; + return { limited: false, current: parallelLimit[commandName], max: command.parallelLimit }; } \ No newline at end of file