Skip to content

Instantly share code, notes, and snippets.

@highfeed
Created December 27, 2020 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save highfeed/c0db616fd24842fc2c947d0dfef3d48c to your computer and use it in GitHub Desktop.
Save highfeed/c0db616fd24842fc2c947d0dfef3d48c to your computer and use it in GitHub Desktop.
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