C
C
cenox2020-12-28 18:46:14
Python
cenox, 2020-12-28 18:46:14

How to make an account until the next message?

There is a bot that makes demotivators from text and pictures taken from the conversation, when you enter the "info" command, it issues a report.
How to make it count how much is left until the next message?
Example:
5fe9fc999168a571707302.jpeg
It should read messages from the database to find out how much is left until the next generation, the code is below:
"Generation when reaching 20 messages and 1 photo in the database - if text_lines >= 20 and pic_count >= 1 and text_lines % 20 == 0: "

@bot.on.chat_message()
async def chat_message_handler(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"
    if (
        message.text.startswith("http://")
        or message.text.startswith("https://")
        or message.text.startswith("@")
        or message.text.startswith("vk.com")
        or message.text.startswith("https://vk.com/")
        or message.text.startswith("[id")
        or message.text.startswith("[club")
        or message.text.startswith("/")
    ):
        return
    if 0 < text_length <= 50 and message.from_id > 0:
        with open(chat_path, "a", encoding="utf8") as f:
            f.write(message.text + "\n")

        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 >= 20 and pic_count >= 1 and text_lines % 20 == 0:
            print(message.from_id)
            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)

            dem_filename = f"result_{random.randint(0, 10000000000000000000000000)}.jpg"
            random_text_len = len(random_text)
            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


info command:
@bot.on.chat_message(lower=True, text=["инфо", "info", "i", "/info"])
async def info(message: Message):
    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_count = len(f.readlines())
    await message(
        f"айди чата: {message.peer_id - 2000000000}\nсохранено {text_count} строк и {pic_count} фото (если числа не меняются, проверьте, выдали ли вы боту админку)"
    )

What it looks like:
5fe9fd88f2926189773602.jpeg

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