Answer the question
In order to leave comments, you need to log in
How to make a cooldown for using a command in pytelegrambotapi?
How to make a cooldown for using a command in pytelegrambotapi?
Let's say that you can milk a cow once an hour.
Answer the question
In order to leave comments, you need to log in
Primitive, but gets the job done
import time
def korova():
start = 10
while True:
time.sleep(1)
print('Ждите')
start = start - 1
if int(start) == 0:
print('Доим корову')
else:
continue
korova()
All you need is a place to store the last date the command was used and add one check to the handler.
Here is a small example:
import datetime
context = {}
@bot.message_handler(commands=["cow"])
def cow_command(message):
last_command_use_time = context.get(message.user_id)
if not last_command_use_time or (datetime.datetime.now() - last_command_use_time).hour > 1:
# Логика команды /cow
context[message.user_id] = datetime.datetime.now()
else:
# Сообщение об ограничениях использования команды
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question