From 6388760ebb9da5493772643868ebf565f46f77ea Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Wed, 20 Aug 2025 20:04:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D1=81=D0=B1=D1=80=D0=BE=D1=81=20=D1=81?= =?UTF-8?q?=D1=87=D0=B5=D1=82=D1=87=D0=B8=D0=BA=D0=BE=D0=B2=20=D1=81=D0=BE?= =?UTF-8?q?=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index dfd5191..f8fba7a 100644 --- a/bot.py +++ b/bot.py @@ -1,7 +1,8 @@ +import asyncio import calendar +import datetime import time -from vkbottle import GroupTypes, GroupEventType from vkbottle.bot import Bot, Message import config @@ -155,5 +156,46 @@ async def any_message_handler(message: Message): # if user_id == bot.api.users.get() -print("Bot started.") +async def wait_until(target_time: datetime.datetime): + now = datetime.datetime.now(target_time.tzinfo) + if now >= target_time: + return + + delay_seconds = (target_time - now).total_seconds() + await asyncio.sleep(delay_seconds) + + +async def counters_reset_task(): + tz = datetime.timezone(datetime.timedelta(hours=3), name="MSK") + + target_time = datetime.time(6, 0, 0, tzinfo=tz) + now = datetime.datetime.now(tz) + if now.hour > target_time.hour or now.hour == target_time.hour and now.minute > target_time.minute: + target_date = now.date() + datetime.timedelta(days=1) + else: + target_date = now.date() + target_datetime = datetime.datetime.combine(target_date, target_time) + + await wait_until(target_datetime) + + print('Resetting daily counters...') + for chat_id in config.DB['chats']: + for user_id in config.DB['chats'][chat_id]['users']: + config.DB['chats'][chat_id]['users'][user_id]['messages_today'] = 0 + + if target_datetime.day == 1: + print('Resetting monthly counters...') + for chat_id in config.DB['chats']: + for user_id in config.DB['chats'][chat_id]['users']: + config.DB['chats'][chat_id]['users'][user_id]['messages_month'] = 0 + + db_save() + + +async def startup_task(): + print("Bot started.") + + +bot.loop_wrapper.on_startup.append(startup_task()) +bot.loop_wrapper.add_task(counters_reset_task()) bot.run_forever()