-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.py
92 lines (71 loc) · 2.28 KB
/
launcher.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# LICENSE: https://github.com/Rapptz/RoboDanny/blob/rewrite/LICENSE.txt
# This file includes portions from RoboDanny, licensed under MPL-2.0
# Modifications and additions copyright (c) 2024-present Rayyan
from __future__ import annotations
import asyncio
import contextlib
import logging
import os
from logging.handlers import RotatingFileHandler
import discord
from dotenv import load_dotenv
from bot import PhantomGuard
try:
import uvloop # type: ignore
except ImportError:
pass
else:
ev_policy = (
asyncio.WindowsProactorEventLoopPolicy()
if os.name == "nt"
else uvloop.EventLoopPolicy
)
asyncio.set_event_loop_policy(policy=ev_policy)
load_dotenv()
os.environ["JISHAKU_NO_UNDERSCORE"] = "True"
os.environ["JISHAKU_NO_DM_TRACEBACK"] = "True"
os.environ["JISHAKU_HIDE"] = "True"
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()
# __enter__
max_bytes = 32 * 1024 * 1024 # 32 MiB
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="logs/console.log",
encoding="utf-8",
mode="w",
maxBytes=max_bytes,
backupCount=5,
)
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():
async with PhantomGuard() as bot:
await bot.start()
if __name__ == "__main__":
with setup_logging():
asyncio.run(run_bot())