-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
59 lines (42 loc) · 1.65 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
import asyncio
import logging
import sys
from logging.handlers import RotatingFileHandler
import settings
from database.migrations import ensure_latest_migration
from didier import Didier
async def run_bot():
"""Run Didier"""
didier = Didier()
# Schedules are quite heavy - do this once before connecting
await didier.load_schedules()
try:
await didier.start(settings.DISCORD_TOKEN)
finally:
await didier.http_session.close()
def setup_logging():
"""Configure custom loggers"""
max_log_size = 32 * 1024 * 1024
# Configure Didier handler
didier_log = logging.getLogger(__name__)
didier_handler = RotatingFileHandler(settings.LOGFILE, mode="a", maxBytes=max_log_size, backupCount=5)
didier_handler.setFormatter(logging.Formatter("[%(asctime)s] [%(levelname)s]: %(message)s"))
didier_log.addHandler(didier_handler)
didier_log.setLevel(logging.INFO)
# Configure discord handler
discord_log = logging.getLogger("discord")
discord_handler: logging.StreamHandler
# Make dev print to stderr instead, so you don't have to watch the file
if settings.SANDBOX:
discord_handler = logging.StreamHandler(sys.stderr)
else:
discord_handler = RotatingFileHandler("discord.log", mode="a", maxBytes=max_log_size, backupCount=5)
discord_handler.setFormatter(logging.Formatter("[%(asctime)s] [%(levelname)s] %(name)s: %(message)s"))
discord_log.addHandler(discord_handler)
async def main():
"""Do some setup & checks, and then run the bot"""
setup_logging()
await ensure_latest_migration()
await run_bot()
if __name__ == "__main__":
asyncio.run(main())