C
C
cenox2020-12-28 19:41:14
Python
cenox, 2020-12-28 19:41:14

How to add a timer to the VK command?

How can I add a timer to a command?
For example, the command "gen" is entered, the bot sends a message, and the next time the command "gen" is sent, the bot replies: Wait another 30 seconds before using this command
The command itself:

@bot.on.chat_message(lower=True, text=["g", "d", "gen", "г", "/ген", "dem", "/g", "ген", "/gen", "д", "дем"])
async def gen(message: Message):
    text_length = len(message.text)
    chat_path = f"{dir_to_txt}{message.peer_id}.txt"
    picture_path = f"{dir_to_pic}{message.peer_id}.txt"

    with open(picture_path, encoding="utf8") as f:
        pic_count = len(f.readlines())
    with open(chat_path, encoding="utf8") as f:
        text_lines = len(f.readlines())

    if text_lines >= 10 and pic_count >= 1:
        print("ГЕНЕРИРУЕМ ПО КОМАНДЕ")
        with open(chat_path, encoding="utf8") as file:
            texts = file.read().splitlines()
        random_text = random.choice(texts)
        random_bottom_text = random.choice(texts) + " " + random.choice(texts)
        with open(picture_path, encoding="utf8") as file:
            pictures = file.read().splitlines()
        random_picture = random.choice(pictures)
        response = requests.get(random_picture)

        random_filename = f"randomimg_{random.randint(0, 10000000000000000000000000)}.jpg"
        with open(random_filename, "wb") as f:
            f.write(response.content)

        random_text_len = len(random_text)

        dem_filename = f"result_{random.randint(0, 10000000000000000000000000)}.jpg"
        if random_text_len <= 50:
            dem = demcreate(random_text, random_bottom_text)
            dem.makeImage(random_filename.rstrip(".jpg"))
            add_watermark_and_rename(result_filename=dem_filename)
            photo = await photo_uploader.upload_message_photo(dem_filename)
            await message(attachment=photo)
            os.remove(dem_filename)
            os.remove(random_filename)
            return
        else:
            random_bottom_text_short = random_text[:50]
            dem = demcreate(random_text, random_bottom_text_short)
            dem.makeImage("randomimg")
            add_watermark_and_rename(result_filename=dem_filename)
            photo = await photo_uploader.upload_message_photo(dem_filename)
            await message(attachment=photo)
            os.remove(dem_filename)
            os.remove(random_filename)
            return
    else:
        await message(
            "Недостаточно сообщений для генерации.\nНужное количество: 10 сообщений и 1 фотография"
        )

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question