Created
December 27, 2020 10:19
-
-
Save highfeed/c0db616fd24842fc2c947d0dfef3d48c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {getMentionUserString} = require('../functions/utils') | |
const gamesRates = { | |
'roll-dice-even-odd': 3000, | |
'roll-dice-three': 3000, | |
'spin-slot-machine-four': 1800, | |
} | |
const RATE_LIMITS = {} | |
module.exports = (ctx, next) => { | |
const userId = ctx.from.id | |
const gamesActions = Object.keys(gamesRates) | |
const callbackData = ctx.callbackQuery && ctx.callbackQuery.data | |
const isGameCallback = callbackData && gamesActions.includes(callbackData) | |
const rateLimitMs = gamesRates[callbackData] | |
const timePassed = new Date() - RATE_LIMITS[userId] | |
if ( | |
isGameCallback && | |
RATE_LIMITS[userId] && | |
timePassed < rateLimitMs | |
) { | |
ctx.log.warn(`[${callbackData}] blocked double action - ${timePassed} ms`) | |
ctx.log.telegram.common(`🚨 ${getMentionUserString(ctx.from)} flood - ${callbackData} - ${timePassed} ms`) | |
return ctx.answerCbQuery(ctx.i18n.t('games.blockedDoubleAction', {timePassed})) | |
} | |
return next().then(() => { | |
if (isGameCallback) { | |
RATE_LIMITS[userId] = new Date() | |
} | |
return true | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment