-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
62 lines (54 loc) · 2.12 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
import asyncio
from global_stuff import connect_to_google_sheets, load_mjs_account_manager
import logging
from threading import Thread
async def main() -> None:
# start importing as early as possible
background_imports_task = asyncio.create_task(_background_imports())
# setup logging
# INFO level captures all except DEBUG log messages.
# the FileHandler by default appends to the given file
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(name)s: %(message)s',
datefmt='%m-%d %H:%M:%S',
handlers=[
logging.FileHandler("log.txt"),
logging.StreamHandler()
]
)
# load environmental variables
import dotenv
dotenv.load_dotenv("config.env")
# load the bot
from bot import setup_bot
load_bot_task = asyncio.create_task(setup_bot())
# load global stuff
gs_task = asyncio.create_task(connect_to_google_sheets())
mjs_task = asyncio.create_task(load_mjs_account_manager())
bot = (await asyncio.gather(load_bot_task, gs_task, mjs_task))[0]
from global_stuff import assert_getenv
DISCORD_TOKEN = assert_getenv("bot_token")
await bot.start(DISCORD_TOKEN)
async def _background_imports() -> None:
"""Cache some imports we might need later in an async thread"""
pkgs = ["discord", "gspread", "json", "numpy", "matplotlib.pyplot", "google.protobuf",
"requests", "aiohttp", "websockets", "urllib3",
"functools", "itertools", "hashlib", "hmac", "struct", "uuid", "re",
"modules.InjusticeJudge.injustice_judge.classes2"]
import time
time_elapsed: float = 0
for pkg in pkgs:
start_time = time.time()
try:
__import__(pkg)
await asyncio.sleep(0) # yield thread
except ImportError:
logging.error(f"_background_imports: failed to import package {pkg}")
continue
time_elapsed += time.time() - start_time
if time_elapsed >= 1:
time_elapsed = 0
await asyncio.sleep(0) # yield
logging.info("Done with async background imports")
asyncio.run(main())