-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
38 lines (31 loc) · 1.17 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
import logging
import os
import asyncio
from dotenv import load_dotenv
from bot.bot import main, config
logging_data = config.get("logging", {})
if not isinstance(logging_data, dict):
logging_data = dict()
if not os.environ.get("TOKEN"):
load_dotenv()
if level := logging_data.get("level"):
handlers = []
formatter = logging.Formatter("%(asctime)s %(name)-16s %(levelname)-8s %(message)s")
if filename := logging_data.get("filename"):
if not filename.endswith(".log"):
filename += ".log"
file_handler = logging.FileHandler(filename)
file_handler.setFormatter(formatter)
handlers.append(file_handler)
if logging_data.get("console"):
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
handlers.append(stream_handler)
logging.basicConfig(level=getattr(logging, level.upper()), handlers=handlers)
if __name__ == "__main__":
if TOKEN := os.environ.get("TOKEN"):
logging.info("Starting bot")
asyncio.run(main(TOKEN))
else:
logging.error("No TOKEN in .env")
logging.error("Generate an example .env file using cp .env-template .env")