app.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import config
  2. import atexit
  3. from loader import dp, bot, connect_db, logger
  4. import time
  5. import datetime
  6. import asyncio
  7. async def on_startup():
  8. config.START_TIME = time.time()
  9. await connect_db()
  10. logger.info('Bot online')
  11. await dp.start_polling(bot)
  12. @atexit.register
  13. def on_exit():
  14. _start_time = config.START_TIME
  15. cur_time = int(time.time())
  16. online_time_row = str(datetime.timedelta(seconds=cur_time - _start_time)).split(':')
  17. day_hours = 0
  18. if 'day' in online_time_row[0] or 'days' in online_time_row[0]:
  19. day_hours = int(online_time_row[0].split(' ')[0]) * 24
  20. online_time_row[0] = int(online_time_row[0].split(' ')[2])
  21. online_time_h = str(int(online_time_row[0]) + day_hours) + 'h ' if int(online_time_row[0]) != 0 else ''
  22. online_time_m = str(int(float(online_time_row[1]))) + 'm ' if float(online_time_row[1]) != 0 else ''
  23. online_time_s = str(int(float(online_time_row[2]))) + 's ' if float(online_time_row[2]) != 0 else ''
  24. logger.info(f'Bot offline. Online time: {online_time_h}{online_time_m}{online_time_s}')
  25. if __name__ == "__main__":
  26. try:
  27. import handlers.users
  28. dp.include_router(handlers.users.router)
  29. asyncio.run(on_startup())
  30. except Exception as ex:
  31. logger.critical(f"critical error while bot polling (errmsg: {ex})", exc_info=True)