loader.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import json
  2. import logging
  3. import logging.config
  4. import config
  5. logging.config.dictConfig(config.LOGGING_SETUP)
  6. logging.basicConfig(format=u'[%(asctime)s][%(levelname)s] ::: %(filename)s(%(lineno)d) -> %(message)s',
  7. level="INFO", filename='logs/bot.log')
  8. logger = logging.getLogger('logger')
  9. try:
  10. import asyncio
  11. import sys
  12. import os
  13. import atexit
  14. from aiogram import Bot, Dispatcher, Router, types
  15. from aiogram.enums import ParseMode
  16. from aiogram.utils.markdown import hbold
  17. from aiogram.filters import CommandStart
  18. from aiogram.types import Message
  19. from aiogram.fsm.storage.memory import MemoryStorage
  20. import time
  21. import datetime
  22. import pdb
  23. from config import DB_INFO
  24. import asyncpg as apg
  25. except Exception as ex:
  26. logger.error("Ошибка при загрузке модулей. Попробуйте 'pip3 install -r requirements.txt' для установки "
  27. "необходимых зависимостей. (errmsg:" + str(ex) + ")", exc_info=True)
  28. os._exit(1)
  29. TOKEN = config.TOKEN
  30. storage = MemoryStorage()
  31. dp = Dispatcher(storage=storage)
  32. bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
  33. with open('logs/bot.log', 'a') as log:
  34. log.write(f'=============================\nNew bot session\n[{datetime.datetime.now()}]\n=============================\n')
  35. with open('logs/err_bot.log', 'a') as log:
  36. log.write(f'=============================\nNew bot session\n[{datetime.datetime.now()}]\n=============================\n')
  37. async def connect_db():
  38. try:
  39. await db.connect()
  40. await db.check_db_structure()
  41. except Exception as ex:
  42. logger.error("error while connecting to bot db. (errmsg:" + str(ex) + ")", exc_info=True)
  43. os._exit(2)
  44. try:
  45. db = pdb.PostSQLDB(DB_INFO[0], DB_INFO[1], DB_INFO[2], DB_INFO[3])
  46. except Exception as ex:
  47. logger.error("error while connecting to bot db. (errmsg:" + str(ex) + ")", exc_info=True)
  48. os._exit(2)