Compare commits
No commits in common. "e6dca692ed35861e3e6bf6b51d10a37c305a5982" and "ceafee82878e30e8a69d23a7f9e14cd2654b2cd7" have entirely different histories.
e6dca692ed
...
ceafee8287
3 changed files with 28 additions and 24 deletions
|
@ -71,16 +71,18 @@ export default {
|
||||||
|
|
||||||
// Check if the limit of parallel execution has been reached
|
// Check if the limit of parallel execution has been reached
|
||||||
if (command.parallelLimit) {
|
if (command.parallelLimit) {
|
||||||
const doParallelLimit = await ratelimiter.checkParallel(interaction.user, commandName, command);
|
console.log('Command has a parallel limit');
|
||||||
|
const doParallelLimit = ratelimiter.checkParallel(interaction.user, commandName, command);
|
||||||
|
console.log(doParallelLimit.limited);
|
||||||
if (doParallelLimit.limited) {
|
if (doParallelLimit.limited) {
|
||||||
return await interaction.reply({ content: doParallelLimit.msg, ephemeral: true });
|
return await interaction.reply({ content: doParallelLimit, ephemeral: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
ratelimiter.addParallel(commandName);
|
ratelimiter.addParallel(commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the ratelimit
|
// Check the ratelimit
|
||||||
const doRateLimit = await ratelimiter.check(interaction.user, commandName, command);
|
const doRateLimit = ratelimiter.check(interaction.user, commandName, command);
|
||||||
if (doRateLimit) {
|
if (doRateLimit) {
|
||||||
return interaction.reply({ content: doRateLimit, ephemeral: true });
|
return interaction.reply({ content: doRateLimit, ephemeral: true });
|
||||||
|
|
||||||
|
@ -107,8 +109,8 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
await command.execute(interaction, args, client)
|
await command.execute(interaction, args, client)
|
||||||
.then(async () => {
|
.then(() => {
|
||||||
const hasPrallelLimit = await ratelimiter.checkParallel(interaction.user, commandName, command);
|
const hasPrallelLimit = ratelimiter.checkParallel(interaction.user, commandName, command);
|
||||||
if (hasPrallelLimit) ratelimiter.removeParallel(commandName);
|
if (hasPrallelLimit) ratelimiter.removeParallel(commandName);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -303,7 +303,7 @@ export default {
|
||||||
const isOptOut = await db.optout.findOne({ where: { userID: message.author.id } });
|
const isOptOut = await db.optout.findOne({ where: { userID: message.author.id } });
|
||||||
|
|
||||||
const timestamp = new Date();
|
const timestamp = new Date();
|
||||||
console.log(`[${timestamp.toISOString()}] \x1b[33m${ isOptOut ? 'A user' : `${userTag} (${userID})`}\x1b[0m launched command \x1b[33m${commandName}\x1b[0m using prefix`);
|
console.log(`[${timestamp.toISOString()}] \x1b[33m${ isOptOut ? 'A user' : `${userTag} (${userID})`}\x1b[0m launched command \x1b[33m${commandName}\x1b[0m using slash`);
|
||||||
|
|
||||||
|
|
||||||
// Owner only check
|
// Owner only check
|
||||||
|
@ -333,16 +333,18 @@ export default {
|
||||||
|
|
||||||
// Check if the limit of parallel execution has been reached
|
// Check if the limit of parallel execution has been reached
|
||||||
if (command.parallelLimit) {
|
if (command.parallelLimit) {
|
||||||
const doParallelLimit = await ratelimiter.checkParallel(message.author, commandName, command);
|
console.log('Command has a parallel limit');
|
||||||
|
const doParallelLimit = ratelimiter.checkParallel(message.author, commandName, command);
|
||||||
|
console.log(doParallelLimit.limited);
|
||||||
if (doParallelLimit.limited) {
|
if (doParallelLimit.limited) {
|
||||||
return await message.reply({ content: doParallelLimit.msg, ephemeral: true });
|
return await message.reply({ content: doParallelLimit, ephemeral: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
ratelimiter.addParallel(commandName);
|
ratelimiter.addParallel(commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the ratelimit
|
// Check the ratelimit
|
||||||
const doRateLimit = await ratelimiter.check(message.author, commandName, command);
|
const doRateLimit = ratelimiter.check(message.author, commandName, command);
|
||||||
if (doRateLimit) {
|
if (doRateLimit) {
|
||||||
return message.reply({ content: doRateLimit, ephemeral: true });
|
return message.reply({ content: doRateLimit, ephemeral: true });
|
||||||
|
|
||||||
|
@ -432,8 +434,8 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
await command.execute(message, args, client)
|
await command.execute(message, args, client)
|
||||||
.then(async () => {
|
.then(() => {
|
||||||
const hasPrallelLimit = await ratelimiter.checkParallel(message.author, commandName, command);
|
const hasPrallelLimit = ratelimiter.checkParallel(message.author, commandName, command);
|
||||||
if (hasPrallelLimit) ratelimiter.removeParallel(commandName);
|
if (hasPrallelLimit) ratelimiter.removeParallel(commandName);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,15 +10,13 @@ export default {
|
||||||
removeParallel,
|
removeParallel,
|
||||||
checkParallel,
|
checkParallel,
|
||||||
};
|
};
|
||||||
async function check(user, commandName, commands) {
|
function check(user, commandName, commands) {
|
||||||
const userID = user.id;
|
const userID = user.id;
|
||||||
const userTag = user.username;
|
const userTag = user.username;
|
||||||
|
|
||||||
// Don't apply the rate limit to bot owner
|
// Don't apply the rate limit to bot owner
|
||||||
if (NODE_ENV !== 'development') {
|
if (userID === ownerId) {
|
||||||
if (user.id === ownerId) {
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ratelimit[userID]) {
|
if (!ratelimit[userID]) {
|
||||||
|
@ -40,11 +38,13 @@ async function check(user, commandName, commands) {
|
||||||
const hours = Math.floor(minutes / 60);
|
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 dateString = `${hours > 0 ? ` ${Math.floor(hours)} hours` : ''}${minutes > 0 ? ` ${Math.floor(minutes % 60)} minutes` : ''}${seconds > 0 ? ` ${Math.floor(seconds % 60)} seconds` : ''}`;
|
||||||
|
|
||||||
const isOptOut = await db.optout.findOne({ where: { userID: userID } });
|
const isOptOut = db.optout.findOne({ where: { userID: userID } });
|
||||||
|
if (isOptOut) {
|
||||||
const timestamp = new Date();
|
console.log(`A user is rate limited on \x1b[33m${commandName}\x1b[0m for${dateString}.`);
|
||||||
console.log(`[${timestamp.toISOString()}] \x1b[33m${ isOptOut ? 'A user' : `${userTag} (${userID})`}\x1b[0m is rate limited on \x1b[33m${commandName}\x1b[0m for${dateString}.`);
|
}
|
||||||
|
else {
|
||||||
|
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m is rate limited on \x1b[33m${commandName}\x1b[0m for${dateString}.`);
|
||||||
|
}
|
||||||
return `You are being rate limited. You can try again in${dateString}.`;
|
return `You are being rate limited. You can try again in${dateString}.`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ async function check(user, commandName, commands) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addParallel(commandName) {
|
function addParallel(commandName) {
|
||||||
// console.log(`[ADD] Adding parallel to ${commandName}`);
|
// console.log(`[ADD] Adding parallel to ${commandName}`);
|
||||||
if (!parallelLimit[commandName]) parallelLimit[commandName] = 0;
|
if (!parallelLimit[commandName]) parallelLimit[commandName] = 0;
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ async function addParallel(commandName) {
|
||||||
parallelLimit[commandName] = prevNumber + 1;
|
parallelLimit[commandName] = prevNumber + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function removeParallel(commandName) {
|
function removeParallel(commandName) {
|
||||||
// console.log(`[REMOVE] Removing parallel to ${commandName}`);
|
// console.log(`[REMOVE] Removing parallel to ${commandName}`);
|
||||||
|
|
||||||
// This shouldn't be possible
|
// This shouldn't be possible
|
||||||
|
@ -83,7 +83,7 @@ async function removeParallel(commandName) {
|
||||||
// console.log(`[REMOVE] current parallel limit: ${JSON.stringify(parallelLimit)}`);
|
// console.log(`[REMOVE] current parallel limit: ${JSON.stringify(parallelLimit)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkParallel(user, commandName, command) {
|
function checkParallel(user, commandName, command) {
|
||||||
// Don't apply the rate limit to bot owner
|
// Don't apply the rate limit to bot owner
|
||||||
if (NODE_ENV !== 'development') {
|
if (NODE_ENV !== 'development') {
|
||||||
if (user.id === ownerId) {
|
if (user.id === ownerId) {
|
||||||
|
|
Loading…
Reference in a new issue