-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.py
110 lines (84 loc) · 2.46 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# SPDX-FileCopyrightText: 2023 Joonas Rautiola <[email protected]>
# SPDX-License-Identifier: MPL-2.0
# https://git.joinemm.dev/miso-bot
import logging # noqa: F401
import os
import signal
import sys
import discord
import discord.http
import uvloop
from dotenv import load_dotenv
from loguru import logger
# dotenv has to be loaded before importing MisoBot
load_dotenv()
from modules.misobot import MisoBot # noqa: E402
class InterceptHandler(logging.Handler):
def emit(self, record):
# Get corresponding Loguru level if it exists
try:
level = logger.level(record.levelname).name
except ValueError:
level = record.levelno
# Find caller from where originated the logged message
frame, depth = logging.currentframe(), 2
while frame and frame.f_code.co_filename == logging.__file__:
frame = frame.f_back
depth += 1
logger.opt(depth=depth, exception=record.exc_info).log(
level, record.getMessage()
)
logging.basicConfig(handlers=[InterceptHandler()], level=logging.INFO)
uvloop.install()
developer_mode = "dev" in sys.argv
if developer_mode:
logger.info("Developer mode is ON")
TOKEN = os.environ["MISO_BOT_TOKEN_BETA"]
prefix = "<"
else:
TOKEN = os.environ["MISO_BOT_TOKEN"]
prefix = ">"
logger.info(f'Launching with default prefix "{prefix}"')
extensions = [
"errorhandler",
"events",
"configuration",
"customcommands",
"fishy",
"information",
"roles",
"mod",
"owner",
"notifications",
"misc",
"media",
"lastfm",
"user",
"utility",
"typings",
"kpop",
"prometheus",
]
infrastructure_extensions = [
"webserver",
]
def main():
if "DISCORD_PROXY" in os.environ:
discord.http.Route.BASE = (
f'{os.environ["DISCORD_PROXY"]}/api/v{discord.http.INTERNAL_API_VERSION}'
)
bot: MisoBot = MisoBot(
extensions=extensions + ([] if developer_mode else infrastructure_extensions),
default_prefix=prefix,
)
if developer_mode:
bot.debug = True
bot.run(TOKEN, log_handler=None)
# Docker by default sends a SIGTERM to a container
# and waits 10 seconds for it to stop before killing it with a SIGKILL.
# This makes ctrl-c work as normal even in a docker container.
def handle_sigterm(*args):
raise KeyboardInterrupt(*args)
if __name__ == "__main__":
signal.signal(signal.SIGTERM, handle_sigterm)
main()