-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (51 loc) · 2.09 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Main module"""
import asyncio
import logging
import os
import colorama
from src.db.funcs import create_db_tables_startup
if __name__ == "__main__":
# Set a different level for imported modules
# See: https://stackoverflow.com/a/51529172
logging.getLogger("aiogram").setLevel(logging.WARNING)
logging.getLogger("pytube").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
logging.getLogger("apify_client").setLevel(logging.WARNING)
logging.getLogger("apscheduler").setLevel(logging.INFO)
from aiogram import Dispatcher
# from aiogram.enums import ParseMode https://github.com/aiogram/aiogram/blob/v3.0.0/examples/echo_bot.py
import config
from src.helper.helper import parse_arguments, color_logging
from src.scheduler.funcs import start_scheduler_as_task
# This is a hacky way to parse the variables and save the user's preference of DEBUG etc
if __name__ == "__main__":
args = parse_arguments()
DEBUG, TEST = args.debug, args.test
config.DEBUG, config.TEST = DEBUG, TEST
os.environ["dbpass"] = args.dbpass
# These need to be here, otherwise the imports are messed up!
from src.bot.bot_functions import dp, bot
from src.bot.middleware import UserUpdateMiddleware
async def main(debug: bool, dp: Dispatcher) -> None:
level = logging.DEBUG if debug else logging.INFO
console = color_logging(level=level)
logging.basicConfig(
level=level,
force=True,
handlers=[console],
) # Force is needed here to re config logging
# Init should be here so as the colors be rendered properly in fly.io
colorama.init(convert=True)
logging.info(f"DEBUG: {debug}, TEST: {TEST}")
# Middleware needs to be registered here, otherwise it won't work!
dp.message.outer_middleware(UserUpdateMiddleware())
await start_scheduler_as_task()
await create_db_tables_startup(pool=None)
await dp.start_polling(
bot,
timeout=20,
skip_updates=False,
)
if __name__ == "__main__":
asyncio.run(main(debug=config.DEBUG, dp=dp))