JAVASCRIPT - 电报 |我怎样才能为这个动作添加冷却时间?

JAVASCRIPT - Telegraf | How can I add a cooldown to this action?

我一直在尝试为我的这部分代码添加一个冷却时间,因为它过于垃圾邮件以至于我不得不禁止某些人使用它。所以我研究了冷却时间,尝试了不同的方法都无济于事,所以我想在这里问问是否有人对此有一些提示或可能的解决方案。提前致谢。

const Telegraf = require('telegraf');
const Telegram = require('telegraf/telegram')
const bot = new Telegraf('******')

bot.hears(['oe', 'Oe', 'OE'], (ctx) => {
    if (ctx.message.reply_to_message && ctx.message.reply_to_message.from.username != 'a username' && ctx.from.id != '****') {   // Check if you're replying and if you're someone banned to use this command

        var a = ctx.message.reply_to_message.from.username
        ctx.reply('¿@ este usuario?', {
            reply_markup: {
                inline_keyboard: [
                    [{ text: "Si", callback_data: "AD" }, { text: "Sin't", callback_data: "ADNT" }]
                ]
            }
        })
        bot.action('AD', (ctx) => {
            ctx.deleteMessage()
            ctx.reply('@' + a + ' oe')
            ctx.replyWithSticker('CAACAgEAAxkBAAOsX9WKywuspdVls5VSf9xV6ZLHrqAAAg8AA5390hUNDOUjryN26R4E')
        })
        bot.action('ADNT', (ctx) => {
            ctx.deleteMessage()
        })

    } else {
        console.log('No reply_to_message found! or the user is banned from doing this command')
    }
})/**/ ```

时间戳

Date.now 会给你一个以毫秒为单位的时间戳。当机器人触发时,查看它是否有时间戳。如果是,则获取当前时间戳并获取时间戳之间的差异。如果它低于您的阈值(比如 5 秒 (5000ms)),请跳过回复。如果它超过了您的阈值,请使用您当前的时间戳更新您保存的时间戳并让机器人回复。

完成猜想

我无法 运行 代码,我不确定它是如何工作的,但我的猜测是这样的

const Telegraf = require('telegraf');
const Telegram = require('telegraf/telegram')
const bot = new Telegraf('******')

let timestamp;

bot.hears(['oe', 'Oe', 'OE'], (ctx) => {

    const rightNow = Date.now();
    if(timestamp) {
        // we have a timestamp from the last time we triggered
        // so let's see if we've waited long enough...

        const difference = rightNow - timestamp;
        if(difference >= (5 * 1000) {
            // enough time has elapsed, go ahead and do our thing
            // but first, reset the timestamp for next time
            timestamp = rightNow;
        } else {
             // nope, haven't waited long enough, abort, abort!
             return;
        }
    } else {
        // no timestamp available so this must be the first time
        timestamp = rightNow;
    }

    if (ctx.message.reply_to_message && ctx.message.reply_to_message.from.username != 'a username' && ctx.from.id != '****') {   // Check if you're replying and if you're someone banned to use this command

        var a = ctx.message.reply_to_message.from.username
        ctx.reply('¿@ este usuario?', {
            reply_markup: {
                inline_keyboard: [
                    [{ text: "Si", callback_data: "AD" }, { text: "Sin't", callback_data: "ADNT" }]
                ]
            }
        })
        bot.action('AD', (ctx) => {
            ctx.deleteMessage()
            ctx.reply('@' + a + ' oe')
            ctx.replyWithSticker('CAACAgEAAxkBAAOsX9WKywuspdVls5VSf9xV6ZLHrqAAAg8AA5390hUNDOUjryN26R4E')
        })
        bot.action('ADNT', (ctx) => {
            ctx.deleteMessage()
        })

    } else {
        console.log('No reply_to_message found! or the user is banned from doing this command')
    }
})/**/