如何在命令之间添加冷却时间,以便用户无法使用命令向我的机器人发送垃圾邮件



如标题所示。我需要让用户在命令之间等待15或30秒。因此,如果他们再次运行它,它会告诉他们需要等待多长时间。

我通过引用下面的代码找到了它:

from time import time
MAX_USAGE = 5

async def callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
count = context.user_data.get("usageCount", 0)
restrict_since = context.user_data.get("restrictSince", 0)
if restrict_since:
if (time() - restrict_since) >= 60 * 5: # 5 minutes
del context.user_data["restrictSince"]
del context.user_data["usageCount"]
await update.effective_message.reply_text("I have unrestricted you. Please behave well.")
else:
await update.effective_message.reply_text("Back off! Wait for your restriction to expire...")
raise ApplicationHandlerStop
else:
if count == MAX_USAGE:
context.user_data["restrictSince"] = time()
await update.effective_message.reply_text("Stop flooding! Don't bother me for 5 minutes...")
raise ApplicationHandlerStop
else:
context.user_data["usageCount"] = count + 1

相关内容

最新更新