Answer the question
In order to leave comments, you need to log in
How to make a timer to disable a command?
I want to make it so that after using the command, it cannot be used for 1 hour. How to do it?
Answer the question
In order to leave comments, you need to log in
I found the solution myself.
You can do it like this
1. after the command is triggered, you need to put the current time + 1 hour in the config (in the config, because otherwise you can’t set the initial value of the time when you just started the program and there will be an error when comparing)
2. when using, compare the time from the config with the current time (but it is worth considering that the file from the config will be a string, and to compare times, you need to use a datetime file)
here is an example code
import datetime
time_now = datetime.datetime.now() #получения время сейчас
f = open('your_file.py', 'r') #пример получения значения из конфига и преобразованния в тип datetime
time_hour = f.read()
time_hour_type_new = datetime.datetime.strptime(time_hour, '%Y-%m-%d %H:%M:%S.%f') #тут указан вид datetime который нам нужен
f.close()
#сравнение времён будет таким же как и другое любое сравнение т.е
if time_hour_type_new > time_now:
print('Доступ к команде заблокирован')
else:
print('Вывод команды')
f = open('your_file.py', 'w')#блокировка команды
f.write(str(time_now + datetime.timedelta()))#в значении timedelta указываем на сколько вы хотите заблокировать команду, для часа это будет так timedelta(hours=+1). Для других единиц измерения времени информацию можно посмотреть в конфиге timedata
f.close()
1.After the command is triggered, save the current time + hour (to a variable/file/cell db*)
2.Before starting the command logic, check if the time is indicated in the right place (see paragraph 1) and whether the current time is greater than the specified one.
At the same time, you need to take into account several exceptions: update the time only if the command is executed, if there is no time - allow execution, take into account that the bot can be used on different servers / chats (i.e. the cooldown will be different), etc.
* - The database option is somewhat redundant. The exception is when it is already in use.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question