2022-12-18 23:30:25 +01:00
const ratelimit = { } ;
2023-12-12 21:20:48 +01:00
const parallelLimit = { } ;
2023-12-14 00:23:49 +01:00
const { ownerId , NODE _ENV } = process . env ;
2023-04-19 17:05:43 +02:00
2023-04-04 01:07:08 +02:00
import db from '../models/index.js' ;
2022-12-18 23:30:25 +01:00
export default {
check ,
2023-12-12 21:20:48 +01:00
addParallel ,
removeParallel ,
checkParallel ,
2022-12-18 23:30:25 +01:00
} ;
function check ( user , commandName , commands ) {
const userID = user . id ;
2023-09-13 20:56:06 +02:00
const userTag = user . username ;
2022-12-18 23:30:25 +01:00
2023-04-19 17:05:43 +02:00
// Don't apply the rate limit to bot owner
if ( userID === ownerId ) {
return false ;
}
2022-12-18 23:30:25 +01:00
if ( ! ratelimit [ userID ] ) {
ratelimit [ userID ] = { } ;
}
const date = new Date ( ) ;
if ( ratelimit [ userID ] [ commandName ] ) {
2022-12-20 19:53:41 +01:00
if ( ratelimit [ userID ] [ commandName ] . cooldown && ratelimit [ userID ] [ commandName ] . limit === commands . ratelimit ) {
2022-12-18 23:30:25 +01:00
if ( date > ratelimit [ userID ] [ commandName ] . cooldown ) {
ratelimit [ userID ] [ commandName ] . limit = 0 ;
ratelimit [ userID ] [ commandName ] . cooldown = undefined ;
}
}
if ( commands . ratelimit === ratelimit [ userID ] [ commandName ] . limit ) {
2022-12-19 05:03:39 +01:00
const seconds = Math . floor ( ( ratelimit [ userID ] [ commandName ] . cooldown - date ) / 1000 ) ;
const minutes = Math . floor ( seconds / 60 ) ;
const hours = Math . floor ( minutes / 60 ) ;
2022-12-20 19:53:41 +01:00
const dateString = ` ${ hours > 0 ? ` ${ Math . floor ( hours ) } hours ` : '' } ${ minutes > 0 ? ` ${ Math . floor ( minutes % 60 ) } minutes ` : '' } ${ seconds > 0 ? ` ${ Math . floor ( seconds % 60 ) } seconds ` : '' } ` ;
2023-04-04 01:07:08 +02:00
const isOptOut = db . optout . findOne ( { where : { userID : userID } } ) ;
if ( isOptOut ) {
console . log ( ` A user is rate limited on \x 1b[33m ${ commandName } \x 1b[0m for ${ dateString } . ` ) ;
}
else {
console . log ( ` \x 1b[33m ${ userTag } ( ${ userID } ) \x 1b[0m is rate limited on \x 1b[33m ${ commandName } \x 1b[0m for ${ dateString } . ` ) ;
}
2022-12-20 19:53:41 +01:00
return ` You are being rate limited. You can try again in ${ dateString } . ` ;
2022-12-18 23:30:25 +01:00
}
}
if ( commands . ratelimit ) {
2022-12-20 19:53:41 +01:00
date . setSeconds ( date . getSeconds ( ) + commands . cooldown ) ;
ratelimit [ userID ] [ commandName ] = { limit : ratelimit [ userID ] [ commandName ] ? ratelimit [ userID ] [ commandName ] . limit + 1 : 1 , cooldown : date } ;
2022-12-18 23:30:25 +01:00
}
2022-12-20 19:53:41 +01:00
2023-12-12 21:20:48 +01:00
return false ;
}
function addParallel ( commandName ) {
2023-12-12 21:40:41 +01:00
// console.log(`[ADD] Adding parallel to ${commandName}`);
2023-12-12 21:20:48 +01:00
if ( ! parallelLimit [ commandName ] ) parallelLimit [ commandName ] = 0 ;
const prevNumber = parallelLimit [ commandName ] ;
2023-12-12 21:40:41 +01:00
// console.log(`[ADD] Previous parallel executions: ${prevNumber}`);
// console.log(`[ADD] Current parallel executions: ${JSON.stringify(parallelLimit)}`);
2023-12-12 21:20:48 +01:00
parallelLimit [ commandName ] = prevNumber + 1 ;
}
function removeParallel ( commandName ) {
2023-12-12 21:40:41 +01:00
// console.log(`[REMOVE] Removing parallel to ${commandName}`);
2023-12-12 21:20:48 +01:00
// This shouldn't be possible
if ( ! parallelLimit [ commandName ] ) parallelLimit [ commandName ] = 0 ;
const prevNumber = parallelLimit [ commandName ] ;
2023-12-12 21:40:41 +01:00
// console.log(`[REMOVE] previous number: ${prevNumber}`);
// console.log(`[REMOVE] previous parallel limit: ${JSON.stringify(parallelLimit)}`);
2023-12-12 21:20:48 +01:00
parallelLimit [ commandName ] = prevNumber - 1 ;
2023-12-12 21:40:41 +01:00
// console.log(`[REMOVE] current parallel limit: ${JSON.stringify(parallelLimit)}`);
2023-12-12 21:20:48 +01:00
}
function checkParallel ( user , commandName , command ) {
// Don't apply the rate limit to bot owner
2023-12-14 00:23:49 +01:00
if ( NODE _ENV !== 'development' ) {
if ( user . id === ownerId ) {
return false ;
}
}
if ( ! parallelLimit [ commandName ] ) parallelLimit [ commandName ] = 0 ;
2023-12-12 21:20:48 +01:00
2023-12-12 21:40:41 +01:00
// console.log(`[CHECK] command limit: ${command.parallelLimit}`);
// console.log(`[CHECK] current parallel executions: ${parallelLimit[commandName]}`);
2023-12-12 21:20:48 +01:00
if ( parallelLimit [ commandName ] >= command . parallelLimit ) {
2023-12-14 00:23:49 +01:00
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 } ) ` } ;
2023-12-12 21:20:48 +01:00
}
2023-12-14 00:23:49 +01:00
return { limited : false , current : parallelLimit [ commandName ] , max : command . parallelLimit } ;
2022-12-18 23:30:25 +01:00
}