-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
77 lines (61 loc) · 2.37 KB
/
runner.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import asyncio
import logging
import subprocess
import sys
import time
import coloredlogs
from tortoise import Tortoise
from bot import Neria
from persistence.models import Event
from utils.config_manager import ConfigManager
ConfigManager.load()
logger = logging.getLogger("Neria")
log_format = "%(name)s[%(process)d] | %(asctime)s,%(msecs)03d | %(levelname)s | %(message)s"
file_hdlr = logging.FileHandler("log.txt")
file_hdlr.setFormatter(logging.Formatter(fmt=log_format))
file_hdlr.setLevel(logging.WARNING)
discord_logger = logging.getLogger("discord")
discord_file_hdlr = logging.FileHandler("discord_log.txt")
discord_file_hdlr.setFormatter(logging.Formatter(fmt=log_format))
logger.handlers.append(file_hdlr)
discord_logger.handlers.append(discord_file_hdlr)
coloredlogs.install("WARNING", logger=discord_logger, fmt=log_format)
coloredlogs.install(ConfigManager.get_setting("loglvl"), logger=logger, fmt=log_format)
def migrate():
logger.info("Start migrating database")
pw = ConfigManager.get_secret("db_password")
usr = ConfigManager.get_secret("db_user")
db = ConfigManager.get_secret("db_name")
host = ConfigManager.get_secret("db_host")
result = subprocess.run(
[sys.executable, "-m", "yoyo", "apply", "--database", f"postgresql://{usr}:{pw}@{host}/{db}"],
stdout=sys.stdout)
if result.returncode != 0:
raise Exception("DB migrations failed")
logger.info("Migration finished")
async def init_tortoise():
logger.info("Creating Database connection...")
await Tortoise.init(config=ConfigManager.load_tortoise_config())
x = await Event.all()
if "create_schemas" in sys.argv:
logger.info("Creating Database schema...")
await Tortoise.generate_schemas()
async def run_app(neria: Neria):
await init_tortoise()
logger.info("Logging into Discord")
await neria.login(ConfigManager.get_secret("bot_token"))
logger.info("Connecting to gateway...")
await neria.connect(reconnect=True)
if __name__ == '__main__':
migrate()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
bot = Neria()
try:
logger.info(f"Starting Bot on timestamp {time.time()}")
loop.run_until_complete(run_app(bot))
except KeyboardInterrupt:
logger.info("Keyboard interrupt detected. Terminating bot.")
pass
finally:
loop.run_until_complete(bot.close())