-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlauncher.py
More file actions
78 lines (65 loc) · 1.9 KB
/
launcher.py
File metadata and controls
78 lines (65 loc) · 1.9 KB
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
78
import asyncio
import asyncpg
import logging
import contextlib
import uvloop
import sys
from bot import Cirilla
import config
# Faster asyncio
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
@contextlib.contextmanager
def setup_logging():
logging.getLogger("discord").setLevel(logging.INFO)
logging.getLogger("discord.http").setLevel(logging.WARNING)
log = logging.getLogger()
try:
# __enter__
log.setLevel(logging.INFO)
if config.debugging:
handler = logging.StreamHandler(sys.stdout)
else:
handler = logging.FileHandler(
filename="cirilla.log", encoding="utf-8", mode="w"
)
dt_fmt = "%Y-%m-%d %H:%M:%S"
fmt = logging.Formatter(
"[{asctime}] [{levelname:<7}] {name}: {message}", dt_fmt, style="{"
)
handler.setFormatter(fmt)
log.addHandler(handler)
yield
finally:
# __exit__
handlers = log.handlers[:]
for hdlr in handlers:
hdlr.close()
log.removeHandler(hdlr)
async def run_bot():
log = logging.getLogger()
# async with asyncpg.create_pool(**config.db, command_timeout=60) as pool:
# bot = Cirilla(pool)
# async with bot:
# await bot.start(config.token)
# print("Bot finished running")
try:
pool = await asyncpg.create_pool(**config.db, command_timeout=60)
except:
log.exception("Failed to initialize Postgres")
return
bot = Cirilla(pool)
async with bot:
await bot.start(config.token)
await pool.close()
async def main():
with open("cirilla_errors.log", "w") as f:
with contextlib.redirect_stderr(f):
with setup_logging():
await run_bot()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
except:
raise