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()