-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlauncher.py
More file actions
95 lines (73 loc) · 2.51 KB
/
Copy pathlauncher.py
File metadata and controls
95 lines (73 loc) · 2.51 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
95
import asyncio
import logging
import os
import signal
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.getMessage():
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=32*1024*1024, # 32 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():
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:
log.exception('Failed to connect to the database')
return
async with Woolinator() as bot:
bot.pool = pool
# Cancel this task on SIGINT/SIGTERM so the `async with` cleanly runs bot.close() once
main_task = asyncio.current_task()
loop = asyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
try:
loop.add_signal_handler(sig, main_task.cancel)
except NotImplementedError:
pass # add_signal_handler is unavailable on Windows
try:
await bot.start()
except asyncio.CancelledError:
log.info('Received exit signal, shutting down')
if __name__ == '__main__':
load_dotenv()
with setup_logging():
asyncio.run(run_bot())