1
0
Fork 0
mirror of https://github.com/EDeev/chatping_abobot.git synced 2026-10-07 20:49:45 +03:00
chatping_abobot/code/bot.py
Egor Deev e4786d8b68 Статистика «за месяц» обнуляется в начале месяца
Раньше месячные счётчики нигде не сбрасывались — ни в коде, ни на сервере — и копились с
момента создания таблиц. При первом запуске месяц только запоминается (в таблице meta),
обнуление — со следующей смены месяца.
2026-10-06 01:10:52 +00:00

49 lines
No EOL
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
import logging
from datetime import date
from init import bot, dp, db, dm
from handlers import router
def reset_month_if_needed(today=None):
"""Статистика «за месяц» раньше нигде не обнулялась и копилась с момента создания таблиц.
При первом запуске месяц только запоминается — обнуление со следующей смены месяца"""
key = (today or date.today()).strftime("%Y-%m")
saved = db.get_meta("month")
if saved == key:
return False
if saved is not None:
db.reset_month()
dm.reset_all()
logging.info("Месячная статистика обнулена: %s → %s", saved, key)
db.set_meta("month", key)
return saved is not None
async def month_watcher():
while True:
try:
reset_month_if_needed()
except Exception:
logging.exception("Не удалось обнулить месячную статистику")
await asyncio.sleep(600)
async def main() -> None:
dp.include_router(router)
await bot.delete_webhook(drop_pending_updates=True)
watcher = asyncio.create_task(month_watcher())
try:
await dp.start_polling(bot, allowed_updates=dp.resolve_used_update_types())
finally:
watcher.cancel()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
try: asyncio.run(main())
except KeyboardInterrupt:
print("Бот остановлен")
pass