forked from Murgeye/teamspeak3-python-bot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·83 lines (65 loc) · 2.13 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
#!/usr/bin/env python3
# standard imports
import logging
import os
import sys
import threading
# third-party imports
from ts3API.utilities import TS3ConnectionClosedException
# local imports
import teamspeak_bot
LOGGER = None
BOT = None
def exception_handler(exctype, value, exception_tb):
"""
Exception handler to prevent any exceptions from printing to stdout. Logs all exceptions to the LOGGER.
:param exctype: Exception type
:param value: Exception value
:param exception_tb: Exception traceback
:return:
"""
LOGGER.error("Uncaught exception.", exc_info=(exctype, value, exception_tb))
def restart_program():
"""
Restarts the current program.
Note: this function does not return. Any cleanup action (like
saving data) must be done before calling this function.
"""
python = sys.executable
os.execl(python, python, *sys.argv)
def main():
"""
Start the BOT, set up LOGGER and set exception hook.
:return:
"""
run_old = threading.Thread.run
def run(*args, **kwargs):
try:
run_old(*args, **kwargs)
except (KeyboardInterrupt, SystemExit, TS3ConnectionClosedException):
raise
except BaseException:
sys.excepthook(*sys.exc_info())
threading.Thread.run = run
try:
os.makedirs(os.path.realpath("logs"))
except FileExistsError:
pass
global BOT, LOGGER
LOGGER = logging.getLogger("BOT")
if not LOGGER.hasHandlers():
class_name = "Bot"
LOGGER = logging.getLogger(class_name)
LOGGER.propagate = 0
LOGGER.setLevel(logging.INFO)
file_handler = logging.FileHandler(f"logs/{class_name.lower()}.log", mode="a+")
formatter = logging.Formatter("%(asctime)s: %(levelname)s: %(message)s")
file_handler.setFormatter(formatter)
LOGGER.addHandler(file_handler)
LOGGER.info("Configured %s logger", str(class_name))
LOGGER.propagate = 0
sys.excepthook = exception_handler
config = teamspeak_bot.Ts3Bot.parse_config(LOGGER)
BOT = teamspeak_bot.Ts3Bot.bot_from_config(config)
if __name__ == "__main__":
main()