如何解决JAVASCRIPT - Telegraf |我如何为这个动作添加冷却时间?
我一直试图为这部分代码添加冷却时间,因为它太垃圾了,以至于我不得不禁止某些人使用它。所以我研究了冷却时间,尝试了不同的方法但都无济于事,所以我想在这里问一下是否有人对此有一些提示或可能的解决方案。提前致谢。
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 秒(5000 毫秒)),请跳过回复。如果超过您的阈值,请使用您当前的时间戳更新您保存的时间戳并让机器人回复。
完成猜测
我无法运行代码,我不确定它是如何工作的,但我的猜测是这样的
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')
}
})/**/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。