-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlauncher.py
More file actions
94 lines (70 loc) · 2.32 KB
/
launcher.py
File metadata and controls
94 lines (70 loc) · 2.32 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import asyncio
import logging
import os
import signal
import sys
import contextlib
from logging.handlers import RotatingFileHandler
import asyncmy
import discord
from dotenv import load_dotenv
from bot import Woolinator
log = logging.getLogger(__name__)
class RemoveNoise(logging.Filter):
def __init__(self):
super().__init__(name='discord.state')
def filter(self, record: logging.LogRecord) -> bool:
if record.levelname == 'WARNING' and 'referencing an unknown' in record.msg:
return False
return True
@contextlib.contextmanager
def setup_logging():
log = logging.getLogger()
try:
discord.utils.setup_logging()
logging.getLogger('discord').setLevel(logging.INFO)
logging.getLogger('discord.http').setLevel(logging.WARNING)
logging.getLogger('discord.state').addFilter(RemoveNoise())
log.setLevel(logging.INFO)
handler = RotatingFileHandler(
filename='woolinator.log',
encoding='utf-8',
mode='w',
maxBytes=64*1024*1024, # 64 MiB
backupCount=5
)
fmt = logging.Formatter('[{asctime}] [{levelname:<7}] {name}: {message}', '%Y-%m-%d %H:%M:%S', style='{')
handler.setFormatter(fmt)
log.addHandler(handler)
yield
finally:
handlers = log.handlers[:]
for hdlr in handlers:
hdlr.close()
log.removeHandler(hdlr)
async def run_bot():
log = logging.getLogger()
try:
pool = await asyncmy.create_pool(
user=os.getenv('MARIADB_USERNAME'),
password=os.getenv('MARIADB_PASSWORD'),
host=os.getenv('MARIADB_HOST'),
db='Woolinator',
autocommit=True,
)
except Exception as e:
log.exception('Failed to connect to the database', exc_info=e)
return
async with Woolinator() as bot:
bot.pool = pool
# Catch CTRL+C (SIGINT) to exit gracefully
def signal_handler(signal, frame):
log.info('Received exit signal %s:', signal)
asyncio.create_task(bot.close())
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
await bot.start()
if __name__ == '__main__':
load_dotenv()
with setup_logging():
asyncio.run(run_bot())